slog

package
v1.7.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 22, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

Slog

Slog (short for "structured log") is a wrapper over go-kit/log with a defined log structure and a set of helper methods.

Configurations

Environment Variable Default Value Allowed Values
LOG_LEVEL "info" "debug", "info", "warn", "error"
LOG_CALLER_DEPTH 4 Z

Log Levels & Filtering

All log levels are allowed to be logged by default. Use the config "LOG_LEVEL" to filter out.

Set Value
"debug" debug + info + warn + error
"info" info + warn + error
"warn" warn + error
"error" error

Usage

  1. Log line without custom fields:
slog.Debug("the five boxing")
level=debug ts=2011-08-22T10:07:34.329181992Z caller=main.go:21 msg="the five boxing"
  1. Log line with custom fields:
slog.Warn("wizards jump quickly", "unit_bytes", 8008)
level=warn ts=2012-07-18T11:27:32.616223846Z caller=main.go:70 msg="the five boxing" unit_bytes=8008
  1. Create a logger with trace_id attached
slogger := slog.WithTraceId(ctx)
slogger.Info("the quick brown")
  1. Create a logger with custom fields attached
logFields := map[string]interface{}{
	"integration_uuid": response.IntegrationUuid,
}
slogger := slog.WithFields(logFields)
slogger.Warn("fox jumps over")
  1. Create a logger with trace_id and custom fields attached
logFields := map[string]interface{}{
	"integration_uuid": response.IntegrationUuid,
}
slogger := slog.WithFields(logFields).WithTraceId(ctx)
slogger.Debug("the lazy dog")

Logging Methods

slog.Debug(msg string, args ...any)
slog.Debugf(format string, args ...any)
slog.Info(msg string, args ...any)
slog.Infof(format string, args ...any)
slog.Warn(msg string, args ...any)
slog.Warnf(format string, args ...any)
slog.Error(err error, msg string, args ...any)
slog.Errorf(err error, format string, args ...any)

Helper Methods

slog.WithFields(fields map[string]any) Logger
slog.WithTraceId(ctx context.Context) Logger
slog.DefaultLogger() log.Logger

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, args ...any)

Debug logs a line with level debug.

func Debugf

func Debugf(format string, args ...any)

Debugf logs a format line with level debug.

func DefaultLogger added in v1.5.0

func DefaultLogger() log.Logger

DefaultLogger returns the default instance of logfmt logger

func Error

func Error(err error, msg string, args ...any)

Error logs a line with level error. If err is not nil it adds only the msg string or vice-versa. Otherwise adds both.

func Errorf

func Errorf(err error, format string, args ...any)

Errorf logs a format line with level error. If err is not nil it adds only the msg string or vice-versa. Otherwise adds both.

func Info

func Info(msg string, args ...any)

Info logs a line with level Info.

func Infof

func Infof(format string, args ...any)

Infof logs a format line with level info.

func Warn

func Warn(msg string, args ...any)

Warn logs a line with level warn.

func Warnf

func Warnf(format string, args ...any)

Warnf logs a format line with level warn.

Types

type Logger added in v1.5.0

type Logger interface {
	Info(msg string, args ...any)
	Warn(msg string, args ...any)
	Debug(msg string, args ...any)
	Error(err error, msg string, args ...any)
	Infof(format string, args ...any)
	Warnf(format string, args ...any)
	Debugf(format string, args ...any)
	Errorf(err error, format string, args ...any)
	WithTraceId(ctx context.Context) Logger
	WithFields(fields map[string]any) Logger
}

Logger represents the methods supported by slog

func NewLogger added in v1.5.0

func NewLogger() Logger

NewLogger returns a new instance of Logger.

func WithFields

func WithFields(fields map[string]any) Logger

WithFields returns WithFields using the defaultLoggerWrapper.

func WithTraceId

func WithTraceId(ctx context.Context) Logger

WithTraceId returns WithTraceId using the defaultLoggerWrapper.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL