Documentation
¶
Overview ¶
Package log provides access to log/slog structured logging with opinionated defaults and integration with flags.
The Logger is a context service, which works like the codec provider in the marshal extension: it has a default action, accumulates Option values which initialize it, and is added to and retrieved from the context. Unlike that provider, loggers are named, which allows an app to provide more than one. The default logger has the empty string as its name and is retrieved with FromContext(ctx, "").
The log functions in this package delegate to the default logger. Those which take a context obtain it from the context; those which don't bridge to the current app in order to find it. When there is no default logger, they fall back to the slog default.
Index ¶
- Constants
- func ContextValue(l *Logger) cli.Action
- func Debug(msg string, args ...any)
- func DebugContext(ctx context.Context, msg string, args ...any)
- func Error(msg string, args ...any)
- func ErrorContext(ctx context.Context, msg string, args ...any)
- func FlagsAndArgs() cli.Action
- func Info(msg string, args ...any)
- func InfoContext(ctx context.Context, msg string, args ...any)
- func Log(ctx context.Context, level Level, msg string, args ...any)
- func LogAttrs(ctx context.Context, level Level, msg string, attrs ...slog.Attr)
- func SetAddSource(name string, v ...bool) cli.Action
- func SetLevel(name string, v ...Level) cli.Action
- func SetLogFormat(name string, v ...LogFormat) cli.Action
- func Warn(msg string, args ...any)
- func WarnContext(ctx context.Context, msg string, args ...any)
- func With(args ...any) *slog.Logger
- type ContextServices
- type Level
- type LogFormat
- type Logger
- func (l *Logger) Apply(opts ...Option)
- func (l *Logger) Debug(msg string, args ...any)
- func (l *Logger) DebugContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) Error(msg string, args ...any)
- func (l *Logger) ErrorContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) Info(msg string, args ...any)
- func (l *Logger) InfoContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) Log(ctx context.Context, level Level, msg string, args ...any)
- func (l *Logger) LogAttrs(ctx context.Context, level Level, msg string, attrs ...slog.Attr)
- func (l *Logger) Logger() *slog.Logger
- func (l *Logger) Name() string
- func (l *Logger) Pipeline() cli.Action
- func (l *Logger) Warn(msg string, args ...any)
- func (l *Logger) WarnContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) With(args ...any) *slog.Logger
- type Option
Constants ¶
const ( LevelDebug = slog.LevelDebug LevelInfo = slog.LevelInfo LevelWarn = slog.LevelWarn LevelError = slog.LevelError )
The names of the commonly used levels
Variables ¶
This section is empty.
Functions ¶
func ContextValue ¶ added in v0.20.0
ContextValue provides an action that registers the given logger with its name and sets it into the context.
func DebugContext ¶
DebugContext logs a message with the given arguments at the Debug level, using the specified context
func ErrorContext ¶
ErrorContext logs a message with the given arguments at the Error level, using the specified context
func FlagsAndArgs ¶ added in v0.20.0
FlagsAndArgs is an action which provides the default flags for the default logger to the application. Despite its name, which is conventional, this action provides no args.
func InfoContext ¶
InfoContext logs a message with the given arguments at the Info level, using the specified context
func SetAddSource ¶ added in v0.20.0
SetAddSource causes the logger named by name to include the source position of log statements and provides reasonable defaults for initializing a flag.
func SetLevel ¶ added in v0.20.0
SetLevel sets the minimum level of the logger named by name and provides reasonable defaults for initializing a flag.
func SetLogFormat ¶ added in v0.20.0
SetLogFormat sets the format of the logger named by name and provides reasonable defaults for initializing a flag.
func WarnContext ¶
WarnContext logs a message with the given arguments at the Warn level, using the specified context
Types ¶
type ContextServices ¶ added in v0.20.0
type ContextServices struct {
// contains filtered or unexported fields
}
ContextServices provides an adapter around the context which tracks the loggers that have been registered with the app. Each app gets its own instance, which is initialized with the app.
func Services ¶ added in v0.20.0
func Services(c context.Context) *ContextServices
Services gets the context services for working with loggers. This function panics if the context does not contain context services, which are initialized with the app
func (*ContextServices) Lookup ¶ added in v0.20.0
func (c *ContextServices) Lookup(name string) (*Logger, bool)
Lookup obtains the logger which was registered with the given name, if any. The default logger is registered with the empty string as its name.
func (*ContextServices) Register ¶ added in v0.20.0
func (c *ContextServices) Register(l *Logger)
Register adds the logger to the services using its name. Any logger which was previously registered with the same name is replaced.
type LogFormat ¶ added in v0.20.0
type LogFormat int
LogFormat names the log/slog handler which backs a Logger.
const ( // TextFormat writes logs using [slog.TextHandler]. This is the default. TextFormat LogFormat = iota // JSONFormat writes logs using [slog.JSONHandler]. JSONFormat )
The available formats for log output
func (LogFormat) MarshalText ¶ added in v0.20.0
MarshalText provides the textual representation
func (LogFormat) NewHandler ¶ added in v0.20.0
NewHandler creates the handler which corresponds to the format
func (*LogFormat) UnmarshalText ¶ added in v0.20.0
UnmarshalText converts the textual representation
type Logger ¶ added in v0.20.0
type Logger struct {
// Action specifies the action which defines the action to run when this value
// is added to a pipeline. Typically, this is an initializer set via WithDefaultAction
cli.Action
// contains filtered or unexported fields
}
Logger provides a named logger, typically retrieved from the context. A logger delegates to log/slog, creating the handler on demand from the options which have been applied to it.
The default logger is named with the empty string. It is the logger that the log functions in this package delegate to and the only one which obtains flags from FlagsAndArgs.
func FromContext ¶ added in v0.20.0
FromContext retrieves the logger with the given name from the context. The default logger has the empty string as its name. This function panics if the logger is not present in the context.
func New ¶ added in v0.20.0
New creates a new logger. By default, adding the Logger to the pipeline registers it with its name and in the context, which is required for most use cases, and, for the default logger, adds flags.
func (*Logger) Debug ¶ added in v0.20.0
Debug logs a message with the given arguments at the Debug level
func (*Logger) DebugContext ¶ added in v0.20.0
DebugContext logs a message with the given arguments at the Debug level, using the specified context
func (*Logger) Error ¶ added in v0.20.0
Error logs a message with the given arguments at the Error level
func (*Logger) ErrorContext ¶ added in v0.20.0
ErrorContext logs a message with the given arguments at the Error level, using the specified context
func (*Logger) Info ¶ added in v0.20.0
Info logs a message with the given arguments at the Info level
func (*Logger) InfoContext ¶ added in v0.20.0
InfoContext logs a message with the given arguments at the Info level, using the specified context
func (*Logger) Log ¶ added in v0.20.0
Log logs a message with the given arguments at the specified level
func (*Logger) LogAttrs ¶ added in v0.20.0
LogAttrs logs a message with the given attributes at the specified level
func (*Logger) Logger ¶ added in v0.20.0
Logger obtains the underlying logger, which is created on demand from the options which have been applied. As a special case, the slog default is used when the receiver is nil.
func (*Logger) Name ¶ added in v0.20.0
Name obtains the name of the logger. The default logger has the empty string as its name.
func (*Logger) Warn ¶ added in v0.20.0
Warn logs a message with the given arguments at the Warn level
func (*Logger) WarnContext ¶ added in v0.20.0
WarnContext logs a message with the given arguments at the Warn level, using the specified context
type Option ¶ added in v0.20.0
Option defines an option for initialization of a Logger. An option is also an action, which applies it to the logger it names within the context.
func WithAction ¶ added in v0.20.0
WithAction sets the Action to use
func WithAddSource ¶ added in v0.20.0
WithAddSource sets whether the handler computes the source file and line of the log statement, corresponding to slog.HandlerOptions.AddSource.
func WithDefaultAction ¶ added in v0.20.0
func WithDefaultAction() Option
WithDefaultAction sets the default action, which registers the logger with its name and in the context, and, if it is the default logger, also adds the flags from FlagsAndArgs.
func WithLevel ¶ added in v0.20.0
WithLevel sets the minimum level of the messages which are logged, corresponding to slog.HandlerOptions.Level.
func WithLogFormat ¶ added in v0.20.0
WithLogFormat sets the format of the log output, which determines the handler that the logger uses.
func WithName ¶ added in v0.20.0
WithName names the logger. The default logger has the empty string as its name. This option is meant for use with New; renaming a logger which has already been registered doesn't change its registration.
func WithOutput ¶ added in v0.20.0
WithOutput sets the writer that the logger writes to. By default, this is the Stderr of the app.