Documentation
¶
Overview ¶
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.
Index ¶
- 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)deprecated
- func RedirectTrace(w io.Writer)deprecated
- func SetFormat(f Format)deprecated
- 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
Constants ¶
const ( // LevelTrace can be set to enable tracing LevelTrace slog.Level = -10 )
Variables ¶
This section is empty.
Functions ¶
func Configure ¶ added in v1.1.0
func Configure(setting ...ConfigSetting) error
Configure sets or changes attributes of either the normal or trace loggers
func RedirectStandard
deprecated
func RedirectTrace
deprecated
func SetFormat
deprecated
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 SetTraceIds ¶
func SetTraceIds(ids ...string)
SetTraceIds registers identifiers for future tracing
func TraceID ¶
TraceID emits one JSON-formatted log entry if tracing is enabled for the requested ID
Types ¶
type ConfigSetting ¶ added in v1.1.0
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
}
ConfigSetting is an argument to Configure()
type LogLevel ¶
type LogLevel int
LogLevel is the level of logging
func (*LogLevel) UnmarshalJSON ¶
UnmarshalJSON is a convenience method for Kong
type LogLevelFlag ¶
LogLevelFlag is useful for using a LogLevel as a command-line flag in CLI applications
type SettingKey ¶ added in v1.1.0
type SettingKey int
SettingKey defines a logger setting that can be set or changed via the Configure function
const ( DestinationSetting SettingKey = iota // Output writer / destination for a logger FormatSetting // Format of log entries OmitTimeSetting // Whether a timestamp is included in log entries )
func (SettingKey) String ¶ added in v1.1.0
func (i SettingKey) String() string