Documentation
¶
Overview ¶
Package lgr provides a simple logger with some extras. Primary way to log is Logf method. The logger's output can be customized in 2 ways:
- by setting individual formatting flags, i.e. lgr.New(lgr.Msec, lgr.CallerFunc)
- by passing formatting template, i.e. lgr.New(lgr.Format(lgr.Short))
Leveled output works for messages based on text prefix, i.e. Logf("INFO some message") means INFO level. Debug and trace levels can be filtered based on lgr.Trace and lgr.Debug options. ERROR, FATAL and PANIC levels send to err as well. FATAL terminate caller application with os.Exit(1) and PANIC also prints stack trace.
Index ¶
- Constants
- Variables
- func AddSecrets(vals ...string)
- func CallerFile(l *Logger)
- func CallerFunc(l *Logger)
- func CallerPkg(l *Logger)
- func Debug(l *Logger)
- func Fatalf(format string, args ...interface{})
- func LevelBraces(l *Logger)
- func Msec(l *Logger)
- func Print(line string)
- func Printf(format string, args ...interface{})
- func RemoveSecrets(vals ...string)
- func Setup(opts ...Option)
- func SetupStdLogger(opts ...Option)
- func SetupWithSlog(logger *slog.Logger)
- func StackTraceOnError(l *Logger)
- func ToSlogHandler(l L) slog.Handler
- func ToStdLogger(l L, level string) *log.Logger
- func Trace(l *Logger)
- type Func
- type L
- type Logger
- type Mapper
- type Option
- type Writer
Constants ¶
const ( // Short logging format Short = `{{.DT.Format "2006/01/02 15:04:05"}} {{.Level}} {{.Message}}` // WithMsec is a logging format with milliseconds WithMsec = `{{.DT.Format "2006/01/02 15:04:05.000"}} {{.Level}} {{.Message}}` // WithPkg is WithMsec logging format with caller package WithPkg = `{{.DT.Format "2006/01/02 15:04:05.000"}} {{.Level}} ({{.CallerPkg}}) {{.Message}}` // ShortDebug is WithMsec logging format with caller file and line ShortDebug = `{{.DT.Format "2006/01/02 15:04:05.000"}} {{.Level}} ({{.CallerFile}}:{{.CallerLine}}) {{.Message}}` // FuncDebug is WithMsec logging format with caller function FuncDebug = `{{.DT.Format "2006/01/02 15:04:05.000"}} {{.Level}} ({{.CallerFunc}}) {{.Message}}` // FullDebug is WithMsec logging format with caller file, line and function FullDebug = `{{.DT.Format "2006/01/02 15:04:05.000"}} {{.Level}} ({{.CallerFile}}:{{.CallerLine}} {{.CallerFunc}}) {{.Message}}` )
Variables ¶
var NoOp = Func(func(format string, args ...interface{}) {}) //nolint:revive
NoOp logger
var Std = Func(func(format string, args ...interface{}) { stdlog.Printf(format, args...) })
Std logger sends to std default logger directly
Functions ¶
func AddSecrets ¶ added in v0.13.0
func AddSecrets(vals ...string)
AddSecrets adds values to hide in the output of the default logger and the std logger made by SetupStdLogger. They stay in effect after Setup calls. Loggers made by New are not affected.
func CallerFile ¶ added in v0.1.4
func CallerFile(l *Logger)
CallerFile adds caller info with file, and line number. Ignored if Format option used. Note: This option only affects lgr's native text format and is ignored when using SlogHandler.
func CallerFunc ¶ added in v0.1.4
func CallerFunc(l *Logger)
CallerFunc adds caller info with function name. Ignored if Format option used. Note: This option only affects lgr's native text format and is ignored when using SlogHandler.
func CallerPkg ¶ added in v0.3.0
func CallerPkg(l *Logger)
CallerPkg adds caller's package name. Ignored if Format option used. Note: This option only affects lgr's native text format and is ignored when using SlogHandler.
func Fatalf ¶ added in v0.2.0
func Fatalf(format string, args ...interface{})
Fatalf logs the message and exits with os.Exit(1). A message without a level prefix is logged at FATAL, so lgr's own output also writes it to the err writer; a message with a prefix is logged at that level.
func LevelBraces ¶ added in v0.1.4
func LevelBraces(l *Logger)
LevelBraces surrounds level with [], i.e. [INFO]. Ignored if Format option used.
func Msec ¶ added in v0.2.0
func Msec(l *Logger)
Msec adds .msec to timestamp. Ignored if Format option used.
func Printf ¶
func Printf(format string, args ...interface{})
Printf simplifies replacement of std logger
func RemoveSecrets ¶ added in v0.13.0
func RemoveSecrets(vals ...string)
RemoveSecrets stops hiding values added by AddSecrets. A value a logger got with Secret option stays hidden by that logger.
func SetupStdLogger ¶ added in v0.8.0
func SetupStdLogger(opts ...Option)
SetupStdLogger makes the default std logger with lgr.L
func SetupWithSlog ¶ added in v0.12.0
SetupWithSlog sets up the global logger with a slog logger. Debug or Trace is turned on when the handler accepts slog.LevelDebug or slog.LevelDebug-4.
func StackTraceOnError ¶ added in v0.10.0
func StackTraceOnError(l *Logger)
StackTraceOnError turns on stack trace for ERROR level.
func ToSlogHandler ¶ added in v0.12.0
ToSlogHandler converts lgr.L to slog.Handler. Attributes are written as key=value pairs after the message, string values quoted, with group names joined to keys by dots as the slog text handler does. For *Logger and types embedding it, secrets in string values are hidden before quoting. For *Logger, the caller reported by the Caller* options is the one in the slog record, so CallerDepth is not needed.
func ToStdLogger ¶ added in v0.5.0
ToStdLogger makes standard logger
Types ¶
type Func ¶
type Func func(format string, args ...interface{})
Func type is an adapter to allow the use of ordinary functions as Logger.
type L ¶
type L interface {
Logf(format string, args ...interface{})
}
L defines minimal interface used to log things
func Default ¶ added in v0.1.1
func Default() L
Default returns pre-constructed def logger (debug off, callers disabled)
func FromSlogHandler ¶ added in v0.12.0
FromSlogHandler creates lgr.L wrapper around slog.Handler. Records below the level the handler accepts are dropped. FATAL and PANIC are passed as ERROR records and then exit with os.Exit(1), as Logger does; PANIC writes a fixed heading and the stacks of all goroutines to stderr first; the message goes to the handler only.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger provided simple logger with basic support of levels. Thread safe
func New ¶
New makes new leveled logger. By default writes to stdout/stderr. default format: 2018/01/07 13:02:34 INFO some message 123
func (*Logger) AddSecrets ¶ added in v0.13.0
AddSecrets adds values to hide in this logger's output. Safe to call while other goroutines log. Empty and whitespace-only values are ignored.
func (*Logger) Logf ¶
Logf implements L interface to output with printf style. DEBUG and TRACE filtered out by dbg and trace flags. ERROR, FATAL and PANIC also send the line to err writer, except with SlogHandler, where only PANIC writes its heading and dump there. FATAL and PANIC call os.Exit(1), PANIC dumps all goroutine stacks to err first.
func (*Logger) RemoveSecrets ¶ added in v0.13.0
RemoveSecrets stops hiding values in this logger's output, the ones set by Secret option too. Package-level secrets are not affected.
type Mapper ¶ added in v0.9.0
type Mapper struct {
MessageFunc mapFunc // message mapper on all levels, runs before the level's function
ErrorFunc mapFunc // level and message mapper on ERROR, PANIC and FATAL levels
WarnFunc mapFunc // level and message mapper on WARN level
InfoFunc mapFunc // level and message mapper on INFO level
DebugFunc mapFunc // level and message mapper on DEBUG and TRACE levels
CallerFunc mapFunc // caller mapper, all levels
TimeFunc mapFunc // time mapper, all levels
}
Mapper defines optional functions to change elements of the logged message for each part, based on levels. Only some mapFunc can be defined, by default does nothing. Can be used to alter the output, for example making some part of the output colorful.
type Option ¶
type Option func(l *Logger)
Option func type
func CallerDepth ¶ added in v0.5.0
CallerDepth sets number of stack frame skipped for caller reporting, 0 by default
func Format ¶ added in v0.6.0
Format sets output layout, overwrites all options for individual parts, i.e. Caller*, Msec, LevelBraces and Map
func Map ¶ added in v0.9.0
Map sets mapper functions to change elements of the logged message based on levels.
func Secret ¶ added in v0.7.0
Secret sets list of substring to be hidden, i.e. replaced by "******" Useful to prevent passwords or other sensitive tokens to be logged. Empty values are ignored. Longer values are replaced first, so a secret containing another one is hidden as a whole.
func SlogHandler ¶ added in v0.12.0
SlogHandler sets slog.Handler to delegate logging to. When using this option, the output format will be controlled by the slog.Handler provided, not by lgr's format options. lgr's Debug and Trace filter runs first, then the handler drops the messages below its own level.
IMPORTANT: When using lgr.SlogHandler:
To get caller information in JSON output, you must create the handler with slog.HandlerOptions{AddSource: true}.
The lgr caller info options (lgr.CallerFile, lgr.CallerFunc) do NOT affect JSON output from slog handlers. They only work with lgr's native text format.
Example of correct setup for JSON with caller info:
// create handler with AddSource enabled
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))
For text format with caller info, use lgr's native caller options:
logger := lgr.New(lgr.CallerFile, lgr.CallerFunc)