log

package module
v0.0.0-...-78a92dc Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

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

CorrelationIDHeaderKey is the HTTP header key used for transmitting correlation IDs.

Variables

This section is empty.

Functions

func Debug

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

func DebugCtx

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

func Error

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

func ErrorCtx

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

func HTTPHandler

func HTTPHandler(handler http.Handler) http.Handler

HTTPHandler is a logging middleware for http server ordering multiple middlewares for http.Mux is important logger should be the last one due to Handler call order

Example:

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

func Info

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

func InfoCtx

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

func MetadataHandler

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

MetadataHandler returns slog.Handler with metadata

func TextHandler

func TextHandler(out io.Writer, multiline bool) *textHandler

TextHandler returns slog.Handler for rendering message in text format

func Trace

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

func TraceCtx

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

func Warn

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

func WarnCtx

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

Types

type Attr

type Attr = slog.Attr

An Attr is a key-value pair.

func Any

func Any(key string, value any) Attr

Any returns any 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) Attr

Bool returns bool Attr

func Duration

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

Duration returns duration Attr

func Err

func Err(v error) Attr

Err returns error Attr

func Float

func Float[T constraints.Float](key string, v T) Attr

Float returns float64 Attr

func Group

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

Group returns group Attr

func Int

func Int[T constraints.Signed](key string, v T) Attr

Int returns int64 Attr

func JSON

func JSON(v any) Attr

func NamedJSON

func NamedJSON(k string, v any) Attr

func String

func String(key, value string) Attr

String returns string Attr

func Time

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

Time returns time Attr

func Uint

func Uint[T constraints.Unsigned](key string, v T) Attr

Uint returns uint64 Attr

type Config

type Config struct {
	Level     string
	Format    string
	Multiline *bool

	Output io.Writer
	// contains filtered or unexported fields
}

type ContextMeta

type ContextMeta string
const (
	// CorrelationIdKey is a default key name for go contexts
	CorrelationIdKey ContextMeta = "correlation_id"

	// PC - program counter, where log is created
	PC ContextMeta = "program_counter"
)

type Logger

type Logger struct {
	Logger *slog.Logger
}

Logger is a custom slog.Logger implementation

var DefaultLogger Logger = NewWithConfiguration(Config{
	pcOffset: 1,
})

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)

func (*Logger) DebugCtx

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

Logs with default logger at level DEBUG with given context

func (*Logger) Error

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

Logs with default logger at level ERROR

func (*Logger) ErrorCtx

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

Logs with default logger at level ERROR with given context

func (*Logger) Info

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

Logs with default logger at level INFO

func (*Logger) InfoCtx

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

Logs with default logger at level INFO with given context

func (*Logger) Trace

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

Logs with default logger at level TRACE

func (*Logger) TraceCtx

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

Logs with default logger at level TRACE with given context

func (*Logger) Warn

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

Logs with default logger at level WARN

func (*Logger) WarnCtx

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

Logs with default logger at level WARN with given context

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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