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
- Variables
- func BindFlags(flags *pflag.FlagSet, cfg *Config)
- func Debug(ctx context.Context, msg string, attrs ...any)
- func DebugErr(ctx context.Context, msg string, err error, 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 InfoErr(ctx context.Context, msg string, err error, 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
- func WithSkip(ctx context.Context, skip int) context.Context
- func WithTestLogger(ctx context.Context, t *testing.T) context.Context
- type Config
Constants ¶
const ( FormatCLI = "cli" FormatConsole = "console" FormatJSON = "json" FormatLogfmt = "logfmt" ColorForce = "force" ColorDisable = "disable" ColorAuto = "auto" )
Variables ¶
var ( LevelDebug = strings.ToLower(slog.LevelDebug.String()) LevelInfo = strings.ToLower(slog.LevelInfo.String()) )
Functions ¶
func DebugErr ¶ added in v0.12.0
DebugErr logs the message and error and attributes at debug level. If err is nil, it will not be logged, but rather use Debug in that case.
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 InfoErr ¶ added in v0.12.0
InfoErr logs the message and error and attributes at info level. If err is nil, it will not be logged, but rather use Info in that case.
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.
func WithNoopLogger ¶
WithNoopLogger returns a copy of the context with a noop logger which discards all logs.
func WithSkip ¶
WithSkip returns a copy of the context with the skip value set. This is used to control the number of stack frames to skip when `source` is calculated.
func WithTestLogger ¶ added in v0.15.0
WithTestLogger returns a copy of the context with a test logger. It ensures that logs are only logged when the `go test -v` is provided or if the test fails. It also ensures that parallel test logs are grouped separately. This is useful for e2e/test/* that runs many tests in parallel, and aims to avoid log flooding in the test output.