Documentation
¶
Overview ¶
Package log provides simple, fast, structured and level registration in Go.
This package offers several functions that allow you to trace controlled log errors in the files and functions where they occur, as well as useful programmer log comments in your code. This package will help you identify and segment the different types of errors log or unique circumstances during the execution of your program using criteria according to the level of impact on the business rules of its development. The package prints the useful information for the programmer in a human readable format.
Installation ¶
Run command in terminal:
go get -u https://github.com/jgolang/log
Quick Start ¶
This is a simple example of how the package is implemented with a basic function.
package main import "github.com/jgolang/log" func main(){ log.Println("My info....") }
Terminal output:
2020/07/05 13:32:03 INFO /dir/file.go:10 (function) My info...
Configuration Modes ¶
You can configure the package depending on your needs to display certain types of log by defining an environment variable on the system that runs your program.
[user@ /home]# export MODE="DEV"
Allowed modes
PROD | DEV
Note: Error logs are printed regardless of this setting.
Index ¶
- func Debug(args ...any)
- func DebugC(ctx context.Context, args ...any)
- func Error(args ...any)
- func ErrorC(ctx context.Context, args ...interface{})
- func Fatal(args ...any)
- func FatalC(ctx context.Context, args ...any)
- func Info(args ...interface{})
- func InfoC(ctx context.Context, args ...interface{})
- func Level() slog.Level
- func NewJSONHandler()
- func NewTextHandler()
- func Panic(args ...any)
- func PanicC(ctx context.Context, args ...any)
- func Print(args ...interface{})
- func PrintC(ctx context.Context, args ...interface{})
- func SetCalldepth(calldepth int)
- func SetLevel(newLevel slog.Level) (oldLevel slog.Level)
- func StackTrace() slog.Attr
- func Warn(args ...any)
- func WarnC(ctx context.Context, args ...interface{})
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Debug ¶
func Debug(args ...any)
Debug logs a debug-level message using the global logger. msg: The message to log. args: Additional arguments to format the message.
func DebugC ¶ added in v1.3.0
DebugC logs a debug-level message with context using the global logger. ctx: The context for the log entry. msg: The message to log. args: Additional arguments to format the message.
func ErrorC ¶ added in v1.3.0
ErrorC logs an error-level message with context using the global logger. ctx: The context for the log entry..
func Fatal ¶
func Fatal(args ...any)
Fatal logs a fatal-level message using the global logger and then calls os.Exit(1).
func FatalC ¶ added in v1.3.0
FatalC logs a fatal-level message with context using the global logger and then calls os.Exit(1). ctx: The context for the log entry.
func Info ¶
func Info(args ...interface{})
Example (Info) ¶
package main import ( "github.com/jgolang/log" ) func main() { // Use this function to see the trace of execution of the sentence. // This function is useful for tracking where errors are generated. log.Info("Hello world!") }
Output: {"level":"info","ts":"1599292300.656843","flags":"","caller":"hello.com/package/file.go:10","msg":"Hello world!"}
func NewJSONHandler ¶ added in v1.3.0
func NewJSONHandler()
func NewTextHandler ¶ added in v1.3.0
func NewTextHandler()
func Panic ¶
func Panic(args ...any)
Panic logs a panic-level message using the global logger and then panics.
func PanicC ¶ added in v1.3.0
PanicC logs a panic-level message with context using the global logger and then panics. ctx: The context for the log entry.
func SetCalldepth ¶ added in v1.1.2
func SetCalldepth(calldepth int)
SetCalldepth configure the number of stack frames to ascend, with 0 identifying the caller of Caller for default loggin
func SetLevel ¶ added in v1.3.0
SetLevel sets the logging level for the Logger instance. This method updates the log level to the specified level and returns the previous log level.
Parameters:
level (slog.Level) - The new log level to be set. This determines the severity of the logs that will be captured. Common log levels include DEBUG, INFO, WARN, and ERROR.
Returns:
oldLevel (slog.Level) - The previous log level before it was updated. This can be used to restore the previous log level if needed.
Example usage:
logger := &Logger{} oldLevel := logger.SetLevel(slog.INFO) // The log level is now set to INFO // You can restore the old level if needed logger.SetLevel(oldLevel)
func StackTrace ¶
StackTrace allows you to view the exact place where the error or incident originated within the code. Shows a trace of up to 10 layers from where the error or incident was generated.
Types ¶
This section is empty.