log

package
v0.0.0-...-80fb781 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2024 License: MIT, MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LevelTrace = slog.Level(-8)
	LevelFatal = slog.Level(12)
)

Those are missing levels from slog.Logger

View Source
const CorrelationIDHeaderKey = "X-Correlation-ID"

Variables

This section is empty.

Functions

func Any

func Any(key string, value any) slog.Attr

Any returns any slog.Attr

Use only if value is of a custom type or if specific Attr does not exist

func Bool

func Bool(key string, v bool) slog.Attr

Bool returns bool slog.Attr

func Debug

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

Logs with default logger at level DEBUG

func DebugCtx

func DebugCtx(ctx context.Context, msg string, args ...any)

Logs with default logger at level DEBUG with given context

func Duration

func Duration(key string, v time.Duration) slog.Attr

Duration returns duration slog.Attr

func Error

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

Logs with default logger at level ERROR

func ErrorCtx

func ErrorCtx(ctx context.Context, msg string, args ...any)

Logs with default logger at level ERROR with given context

func Fatal

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

Logs with default logger at level FATAL

func FatalCtx

func FatalCtx(ctx context.Context, msg string, args ...any)

Logs with default logger at level FATAL with given context

func Float64

func Float64(key string, v float64) slog.Attr

Float64 returns float64 slog.Attr

func Group

func Group(key string, v ...slog.Attr) slog.Attr

Group returns group slog.Attr

func HTTPHandler

func HTTPHandler(handler http.Handler) http.Handler

HTTPHandler is a logging middleware for http server

Example:

mux := http.NewServeMux()
mux.HandleFunc("/v1/hello", HelloHandler)
log.Fatal(http.ListenAndServe(addr, log.HTTPHandler(mux)))

func Info

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

Logs with default logger at level INFO

func InfoCtx

func InfoCtx(ctx context.Context, msg string, args ...any)

Logs with default logger at level INFO with given context

func Int

func Int(key string, value int) slog.Attr

Int returns int slog.Attr

func Int64

func Int64(key string, value int64) slog.Attr

Int64 returns int64 slog.Attr

func MetadataHandler

func MetadataHandler(handler slog.Handler, cfg Config) *metadataHandler

MetadataHandler returns slog.Handler with metadata

func String

func String(key, value string) slog.Attr

String returns string slog.Attr

func TextHandler

func TextHandler(out io.Writer, cfg Config) *textHandler

TextHandler returns slog.Handler for rendering message in text format

func Time

func Time(key string, v time.Time) slog.Attr

Time returns time slog.Attr

func Trace

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

Logs with default logger at level TRACE

func TraceCtx

func TraceCtx(ctx context.Context, msg string, args ...any)

Logs with default logger at level TRACE with given context

func Uint64

func Uint64(key string, v uint64) slog.Attr

Uint64 returns uint64 slog.Attr

func Warn

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

Logs with default logger at level WARN

func WarnCtx

func WarnCtx(ctx context.Context, msg string, args ...any)

Logs with default logger at level WARN with given context

Types

type Config

type Config struct {
	Level     string
	Multiline bool

	// Additional metadata
	Enviroment string
}

type ContextMeta

type ContextMeta string

ContextMeta is a type to be used with go context ie. log.CorrelationIdKey is ContextMeta type

context.WithValue(ctx, log.CorrelationIdKey, correlationID)
const CorrelationIDKey ContextMeta = "correlation_id"

CorrelationIDKey is a default key name for go contexts

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger is a custom slog.Logger implementation

func New

func New() Logger

New returns logger with default configuration

func NewWithConfiguration

func NewWithConfiguration(cfg Config) Logger

NewWithConfiguration returns logger with given configuration

func (*Logger) Debug

func (l *Logger) Debug(msg string, args ...any)

Logs at level DEBUG

func (*Logger) DebugCtx

func (l *Logger) DebugCtx(ctx context.Context, msg string, args ...any)

Logs at level DEBUG with given context

func (*Logger) Error

func (l *Logger) Error(msg string, args ...any)

Logs at level ERROR

func (*Logger) ErrorCtx

func (l *Logger) ErrorCtx(ctx context.Context, msg string, args ...any)

Logs at level ERROR with given context

func (*Logger) Fatal

func (l *Logger) Fatal(msg string, args ...any)

Logs at level FATAL

func (*Logger) FatalCtx

func (l *Logger) FatalCtx(ctx context.Context, msg string, args ...any)

Logs at level FATAL with given context

func (*Logger) Info

func (l *Logger) Info(msg string, args ...any)

Logs at level INFO

func (*Logger) InfoCtx

func (l *Logger) InfoCtx(ctx context.Context, msg string, args ...any)

Logs at level INFO with given context

func (*Logger) Trace

func (l *Logger) Trace(msg string, args ...any)

Logs at level TRACE

func (*Logger) TraceCtx

func (l *Logger) TraceCtx(ctx context.Context, msg string, args ...any)

Logs at level TRACE with given context

func (*Logger) Warn

func (l *Logger) Warn(msg string, args ...any)

Logs at level WARN

func (*Logger) WarnCtx

func (l *Logger) WarnCtx(ctx context.Context, msg string, args ...any)

Logs at level WARN with given context

type Metadata

type Metadata map[string]any

Metadata is for easy way to add attrs

log.Info("Message with metadata", log.Metadata{
	"query":    util.CleanString(sql),
	"args":     args,
	"duration": info.Duration.Round(time.Microsecond).String(),
})

Jump to

Keyboard shortcuts

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