go get github.com/go-pkgz/lgr
l := lgr.New(lgr.Msec, lgr.Debug, lgr.CallerFile, lgr.CallerFunc) // allow debug and caller info, timestamp with milliseconds
l.Logf("INFO some important message, %v", err)
l.Logf("DEBUG some less important message, %v", err)output looks like this:
2018/01/07 13:02:34.000 INFO {svc/handler.go:101 h.MyFunc1} some important message, can't open file myfile.xyz
2018/01/07 13:02:34.015 DEBUG {svc/handler.go:155 h.MyFunc2} some less important message, file is too small
Without lgr.Caller* it will drop {caller} part
lgrpackage provides a single interfacelgr.Lwith a single methodLogf(format string, args ...interface{}). Function wrapperlgr.Funcallows makinglgr.Lfrom a function directly.- Default logger functionality can be used without
lgr.New(see "global logger") - Two predefined loggers available:
lgr.NoOp(do-nothing logger) andlgr.Std(passing directly to stdlib log)
lgr.New call accepts functional options:
lgr.Debug- turn debug mode on to allow messages with "DEBUG" level (filtered otherwise)lgr.Trace- turn trace mode on to allow messages with "TRACE" and "DEBUG" levels both (filtered otherwise)lgr.Out(io.Writer)- sets the output writer, defaultos.Stdoutlgr.Err(io.Writer)- sets the error writer, defaultos.Stderrlgr.CallerFile- adds the caller file info (only affects lgr's native text format, not slog output)lgr.CallerFunc- adds the caller function info (only affects lgr's native text format, not slog output)lgr.CallerPkg- adds the caller package (only affects lgr's native text format, not slog output)lgr.CallerDepth(n)- skips n more stack frames when reporting the caller; use it when lgr is called through a wrapper (1 forlgr.ToWriter, 3 forlgr.ToStdLogger).lgr.SetupStdLoggersets 3 itself, and algr.CallerDepthpassed to it replaces that value.lgr.ToSlogHandlerwrapping a*lgr.Loggertakes the caller from the slog record and needs none; a type that embeds*lgr.Loggerand logs through its promotedLogfneeds 3 when aslog.Loggercalls the handler, and a handler or bridge in between adds frames of its own.lgr.LevelBraces- wraps levels with "[" and "]"lgr.Msec- adds milliseconds to timestamplgr.Format- sets a custom template, overwrites all other formatting modifiers and the mapper set withlgr.Map. A template that fails to parse, or to execute with empty values, is replaced bylgr.Short, and a notice about it is printed to the process stdout, not to thelgr.Outwriter.lgr.Secret(secret ...)- sets list of the secrets to hide from the logging outputs, see "secrets".lgr.Map(mapper)- sets mapper functions to change elements of the logging output based on levels.lgr.StackTraceOnError- turns on stack trace for ERROR level. The trace is written to the out writer after the line, not to the err writer.lgr.SlogHandler(h slog.Handler)- delegates logging to the provided slog handler. Of the other options, only these still apply:lgr.Debugandlgr.Trace(lgr filters messages before the handler applies its own level),lgr.Secretfor the message,lgr.CallerDepthfor the source position passed to the handler, and the exit afterFATALandPANIC, withPANICalso writing*** PANIC: <message>and the goroutine dump to the err writer. All other options are ignored, andERRORandFATALlines are not copied to the err writer.
example: l := lgr.New(lgr.Debug, lgr.Msec)
Several predefined templates provided and can be passed directly to lgr.Format, i.e. lgr.Format(lgr.WithMsec)
Short = `{{.DT.Format "2006/01/02 15:04:05"}} {{.Level}} {{.Message}}`
WithMsec = `{{.DT.Format "2006/01/02 15:04:05.000"}} {{.Level}} {{.Message}}`
WithPkg = `{{.DT.Format "2006/01/02 15:04:05.000"}} {{.Level}} ({{.CallerPkg}}) {{.Message}}`
ShortDebug = `{{.DT.Format "2006/01/02 15:04:05.000"}} {{.Level}} ({{.CallerFile}}:{{.CallerLine}}) {{.Message}}`
FuncDebug = `{{.DT.Format "2006/01/02 15:04:05.000"}} {{.Level}} ({{.CallerFunc}}) {{.Message}}`
FullDebug = `{{.DT.Format "2006/01/02 15:04:05.000"}} {{.Level}} ({{.CallerFile}}:{{.CallerLine}} {{.CallerFunc}}) {{.Message}}`
User can make a custom template and pass it directly to lgr.Format. For example:
lgr.Format(`{{.Level}} - {{.DT.Format "2006-01-02T15:04:05Z07:00"}} - {{.CallerPkg}} - {{.Message}}`)Note: formatter (predefined or custom) adds measurable overhead - the cost will depend on the version of Go. You can validate this in your environment via benchmarks: go test -bench=. -run=Bench
lgr.Secret(secret ...) sets the values to replace with ****** when the logger is created. The list can be changed later, while the logger is in use and other goroutines log:
l.AddSecrets(secret ...)andl.RemoveSecrets(secret ...)change the list of a logger made bylgr.New.lgr.AddSecrets(secret ...)andlgr.RemoveSecrets(secret ...)change the package-level list. It is hidden by the global logger and by the std logger set withlgr.SetupStdLogger, in addition to the secrets each of them got withlgr.Secret, and it stays in effect afterlgr.Setup,lgr.SetupStdLoggerandlgr.SetupWithSlogcalls. Loggers made bylgr.Newdon't use the package-level list.
lgr.Setup(lgr.Secret(dbPassword))
lgr.SetupStdLogger(lgr.Secret(dbPassword))
// later, for example on config reload
lgr.AddSecrets(newToken, newPassword) // hidden in both lgr.Printf and std log.Printf outputThings to know:
- Empty and whitespace-only values are ignored. Adding a value twice is the same as adding it once, and a single remove drops it.
- The list of a logger and the package-level list are independent. A value present in both stays hidden until removed from both.
- A line being logged at the moment of the change may still be written with the previous list.
- Longer values are replaced first, so a secret containing another secret is hidden as a whole. Secrets that only partially overlap are replaced one after another, and a part of one of them may stay visible.
- Secrets are hidden in the message passed to the logger. With
lgr.ToSlogHandlerwrapping a*lgr.Logger, or a type that embeds one, attributes are part of that message, and string values are masked before they are quoted. Any otherlgr.L, such as anlgr.Funcaround a masking logger, gets string values already quoted, so a secret that Go quoting changes, such as one containing a quote, a backslash, a newline or a tab, may not be masked there.lgr.FromSlogHandleradds no masking of its own: the wrapped handler can redact the record itself, for example withReplaceAttr, or pass it to a masking logger. Attributes added by a slog handler set withlgr.SlogHandlerare not processed.
Logf recognizes prefixes like INFO or [INFO] as levels. A bare level counts only when followed by whitespace (space, tab, CR or LF), a colon or the end of the line, so WARNING: disk full is not WARN; a bracketed level may be followed by anything. A line without a recognized prefix is INFO. The full list of supported levels - TRACE, DEBUG, INFO, WARN, ERROR, PANIC and FATAL.
TRACEwill be filtered unlesslgr.Traceoption definedDEBUGwill be filtered unlesslgr.Debugorlgr.Traceoptions definedINFOandWARNdon't have any special behavior attachedERRORsends messages to both out and err writers, once when both are the same file or writerFATALsends messages to both out and err writers and callsos.Exit(1)PANICdoes the same asFATALbut in addition sends dump of callers and runtime info to err.
Elements of the output can be altered with a set of user defined function passed as lgr.Map options. Such a mapper changes
the value of an element (i.e. timestamp, level, message, caller) and has separate functions for each level. Note: both level
and messages elements handled by the same function for a given level.
MessageFuncmaps the message on every level, before the function of the level.TRACEusesDebugFunc,PANICandFATALuseErrorFunc.- The mapper is ignored when
lgr.Formatorlgr.SlogHandleris set. - A second
lgr.Mapoption replaces the first one.
A typical use-case is to produce colorful output with a user-defined colorization library.
example with fatih/color:
colorizer := lgr.Mapper{
ErrorFunc: func(s string) string { return color.New(color.FgHiRed).Sprint(s) },
WarnFunc: func(s string) string { return color.New(color.FgHiYellow).Sprint(s) },
InfoFunc: func(s string) string { return color.New(color.FgHiWhite).Sprint(s) },
DebugFunc: func(s string) string { return color.New(color.FgWhite).Sprint(s) },
CallerFunc: func(s string) string { return color.New(color.FgBlue).Sprint(s) },
TimeFunc: func(s string) string { return color.New(color.FgCyan).Sprint(s) },
}
logOpts := []lgr.Option{lgr.Msec, lgr.LevelBraces, lgr.Map(colorizer)}lgr logger can be converted to io.Writer, *log.Logger, or slog.Handler
lgr.ToWriter(l lgr.L, level string) *lgr.Writer- makes io.Writer forwarding write ops to underlyinglgr.Llgr.ToStdLogger(l lgr.L, level string) *log.Logger- makes standard logger on top oflgr.Llgr.ToSlogHandler(l lgr.L) slog.Handler- converts lgr.L to a slog.Handler for use with slog. Attributes are written askey=valuepairs after the message, with group names joined to the keys by dots, as the slog text handler does. Whenlis a*lgr.Logger, theCaller*options report the code that called the slog method.
level parameter is optional, if defined (non-empty) will enforce the level.
lgr.SetupStdLogger(opts ...Option)initializes std global logger (log.std) with lgr logger and given options. All standard methods likelog.Print,log.Println,log.Fataland so on will be forwarded to lgr. Their lines are leveled by prefix like any other, solog.Fatalf("no prefix")is logged atINFObefore the program exits.lgr.SetupWithSlog(logger *slog.Logger)sets up the global logger with a slog logger. It turns onlgr.Debugorlgr.Tracewhen the handler acceptsslog.LevelDebugorslog.LevelDebug-4.
In addition to the standard logger interface, lgr provides seamless integration with Go's log/slog package:
// Create lgr logger
lgrLogger := lgr.New(lgr.Debug, lgr.Msec)
// Convert to slog handler and create slog logger
handler := lgr.ToSlogHandler(lgrLogger)
logger := slog.New(handler)
// Use standard slog API with lgr formatting
logger.Info("message", "key1", "value1")
// Output: 2023/09/15 10:34:56.789 INFO message key1="value1" // Create slog handler
jsonHandler := slog.NewJSONHandler(os.Stdout, nil)
// Wrap it with lgr interface
logger := lgr.FromSlogHandler(jsonHandler)
// Use lgr API with slog backend
logger.Logf("INFO message with %s", "structured data")
// Output: {"time":"2023-09-15T10:34:56.789Z","level":"INFO","msg":"message with structured data"}Messages below the handler's level are dropped; with nil options, as above, DEBUG and TRACE are not written. FATAL and PANIC are passed to the handler as ERROR records, and then the program exits with status 1, as it does with lgr.New; a handler that does not accept ERROR drops the record, and the program exits without logging the message. PANIC writes a heading and the stacks of all goroutines to stderr first, leaving the message to the handler. Levels are parsed as described in "levels"; only the space or tab after the level prefix is removed, and the rest of the text reaches the handler as it is, including the newline the standard log package adds when it writes through lgr.ToStdLogger, so a handler that redacts the message sees the whole value. As with lgr.New, Logf called without arguments uses the text as it is, so %% stays %%.
// Create a logger that uses slog directly
jsonHandler := slog.NewJSONHandler(os.Stdout, nil)
logger := lgr.New(lgr.SlogHandler(jsonHandler))
// Use lgr API with slog backend
logger.Logf("INFO message")
// Output: {"time":"2023-09-15T10:34:56.789Z","level":"INFO","msg":"message"}lgr's own level filter runs before the handler: pass lgr.Debug or lgr.Trace to lgr.New as well to let DEBUG or TRACE messages reach it. Then the handler drops the messages below its own level.
To get caller information in JSON output when using slog handlers, create the handler with AddSource: true:
// Create JSON handler with source information (caller info)
jsonHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
AddSource: true, // This enables caller information in JSON output
})
// Use handler with lgr
logger := lgr.New(lgr.SlogHandler(jsonHandler))
logger.Logf("INFO message with caller info")
// Output will include source file, line and function in JSONNote: The lgr caller options (lgr.CallerFile, lgr.CallerFunc, lgr.CallerPkg) only work with lgr's native text format
and don't affect JSON output from slog handlers. To include caller information in JSON logs:
- For slog JSON handlers: Create the handler with
AddSource: trueas shown above - For text-based logs: Use lgr's native caller options without slog integration
This behavior is designed to respect each logging system's conventions for representing caller information.
Users should avoid global logger and pass the concrete logger as a dependency. However, in some cases a global logger may be needed, for example migration from stdlib log to lgr. For such cases log "github.com/go-pkgz/lgr" can be imported instead of log package, as long as the code calls only the three functions the global logger provides: Printf, Print, which takes a single string, and Fatalf. For code using the rest of the log API, such as log.Println or log.Fatal, keep the standard log import and call lgr.SetupStdLogger to forward its output to lgr.
Global logger provides lgr.Printf, lgr.Print(line string) and lgr.Fatalf functions. lgr.Fatalf logs a message without a level prefix at FATAL, and exits after any message. User can customize the logger by calling lgr.Setup(options ...). The instance of this logger can be retrieved with lgr.Default()