import "github.com/bruceesmith/logger"Package logger supports logging and tracing based on the standard library package log/slog.
Debug, Error, Info and Warn operate like their package slog equivalents, with the level of logging modifiable using SetLevel.
A custom logging level (LevelTrace) can be supplied to SetLevel to enable tracing. Tracing can be unconditional when calling Trace, or only enabled for pre-defined identifiers when calling TraceID. Identifiers for TraceID are registered by calling SetTraceIDs.
By default, all debug, error, info and warn messages go to Stdout, and traces go to Stderr; these destinations can be changed by calling RedirectNormal and RedirectTrace respectively.
A number of settings can be changed for one or both of the normal (non-trace) and trace loggers by calling Configure - the format of log records, their destination, and whether each record contains a timestamp.
When used in cli applications, a cli.Flag representing a LogLevel can be provided using the LogLevelFlag type.
- Constants
- func Configure(setting ...ConfigSetting) error
- func Debug(msg string, args ...any)
- func Error(msg string, args ...any)
- func Info(msg string, args ...any)
- func Level() string
- func RedirectStandard(w io.Writer)
- func RedirectTrace(w io.Writer)
- func SetFormat(f Format)
- func SetLevel(l slog.Level)
- func SetTraceIds(ids ...string)
- func Trace(msg string, args ...any)
- func TraceID(id string, msg string, args ...any)
- func TraceIDs() []string
- func Warn(msg string, args ...any)
- type ConfigSetting
- type Format
- type LogID
- type LogLevel
- type LogLevelFlag
- type SettingKey
- type Traces
const (
// LevelTrace can be set to enable tracing
LevelTrace slog.Level = -10
)func Configure(setting ...ConfigSetting) errorConfigure sets or changes attributes of either the normal or trace loggers
func Debug(msg string, args ...any)Debug emits a debug log
func Error(msg string, args ...any)Error emits an error log
func Info(msg string, args ...any)Info emits an info log
func Level() stringLevel returns the current logging level as a string
func RedirectStandard(w io.Writer)RedirectStandard changes the destination for normal (non-trace) logsDestinationSetting argument
Deprecated: RedirectStandard() should be replaced by a call to Configure() with a DestinationSetting argument
func RedirectTrace(w io.Writer)RedirectTrace changes the destination for normal (non-trace) logs
Deprecated: RedirectTrace() should be replaced by a call to Configure() with a DestinationSetting argument
func SetFormat(f Format)SetFormat changes the format of log entries
Deprecated: SetFormat() should be replaced by calls to Configure() with a FormatSetting argument. An advantage of Configure() is that the format of the standard logger can be configured differently to that of the Trace logger
func SetLevel(l slog.Level)SetLevel sets the default level of logging
func SetTraceIds(ids ...string)SetTraceIds registers identifiers for future tracing
func Trace(msg string, args ...any)Trace emits one JSON-formatted log entry if trace level logging is enabled
func TraceID(id string, msg string, args ...any)TraceID emits one JSON-formatted log entry if tracing is enabled for the requested ID
func TraceIDs() []stringTraceIDs returns the list of enabled trace IDs
func Warn(msg string, args ...any)Warn emits a warning log
ConfigSetting is an argument to Configure()
type ConfigSetting struct {
AppliesTo LogID // Logger whose setting is set/changed
Key SettingKey // Attribute of the logger that is srt/changed
Value any // New value for this attribute
}Format determines the format of each log entry
type Format stringconst (
Text Format = "text" // Text format logs
JSON Format = "json" // JSON format logs
)LogID defines the identifier of a logger
type LogID intconst (
Norm LogID = iota // The normal logger
Tracy // The trace logger
)func (i LogID) String() stringLogLevel is the level of logging
type LogLevel intfunc (ll *LogLevel) Set(ls string) (err error)Set is a convenience method for pflag.Value
func (ll *LogLevel) String() (s string)String is a convenience method for pflag.Value
func (ll *LogLevel) Type() stringType is a conveniene method for pflag.Value
func (ll *LogLevel) UnmarshalJSON(jason []byte) (err error)UnmarshalJSON is a convenience method for Kong
LogLevelFlag is useful for using a LogLevel as a command-line flag in CLI applications
type LogLevelFlag = cli.FlagBase[LogLevel, cli.NoConfig, logLevelValue]SettingKey defines a logger setting that can be set or changed via the Configure function
type SettingKey intconst (
DestinationSetting SettingKey = iota // Output writer / destination for a logger
FormatSetting // Format of log entries
OmitTimeSetting // Whether a timestamp is included in log entries
)func (i SettingKey) String() stringTraces is the list of trace IDs enabled
type Traces []stringfunc (t *Traces) Set(ts string) (err error)Set is a convenience method for pflag.Value
func (t *Traces) String() (s string)String is a convenience method for pflag.Value
func (t *Traces) Type() stringType is a conveniene method for pflag.Value
Generated by gomarkdoc