Documentation ¶
Overview ¶
Package log provides a wrapper around the slog package (might change implementation later). It provides an opinionated interface for logging structured data always with context.
Index ¶
- Constants
- func BindFlags(flags *pflag.FlagSet, cfg *Config)
- func Debug(ctx context.Context, msg string, attrs ...any)
- func Error(ctx context.Context, msg string, err error, attrs ...any)
- func Hex7(key string, value []byte) slog.Attr
- func Info(ctx context.Context, msg string, attrs ...any)
- func Init(ctx context.Context, cfg Config) (context.Context, error)
- func Warn(ctx context.Context, msg string, err error, attrs ...any)
- func WithCLILogger(ctx context.Context) context.Context
- func WithCtx(ctx context.Context, attrs ...any) context.Context
- func WithLogger(ctx context.Context, logger *slog.Logger) context.Context
- func WithNoopLogger(ctx context.Context) context.Context
- type Config
Constants ¶
const ( FormatCLI = "cli" FormatConsole = "console" FormatJSON = "json" FormatLogfmt = "logfmt" ColorForce = "force" ColorDisable = "disable" ColorAuto = "auto" )
Variables ¶
This section is empty.
Functions ¶
func Error ¶
Error logs the message and error and arguments at error level. If err is nil, it will not be logged.
func Hex7 ¶
Hex7 is a convenience function for a hex-encoded log attribute limited to first 7 chars for brevity. Note this is NOT 0x prefixed.
func Init ¶
Init initializes the global logger with the given config. It also returns a copy of the context with the logger attached, see WithLogger. It returns an error if the config is invalid.
func Warn ¶
Warn logs the message and error and attributes at warning level. If err is nil, it will not be logged.
func WithCLILogger ¶
WithCLILogger returns a copy of the context with a cli logger.
func WithCtx ¶
WithCtx returns a copy of the context with which the logging attributes are associated. Usage:
ctx := log.WithCtx(ctx, "height", 1234) ... log.Info(ctx, "Height processed") // Will contain attribute: height=1234
func WithLogger ¶
WithLogger returns a copy of the context with which the logger is associated replacing the default global logger when logging with this context.