logging

package
v4.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ErrorField is the default name for a WithError call
	ErrorField string = "error"
)

Variables

This section is empty.

Functions

func AddGlobalHook

func AddGlobalHook(hook Hook)

AddGlobalHook adds a hook that will be included in all new loggers

func DeepCopy

func DeepCopy(i initLogger) initLogger

func GetExitFunc

func GetExitFunc() func(int)

GetExitFunc returns the current exit function

func InitLoggerInContext

func InitLoggerInContext() context.Context

func SetExitFunc

func SetExitFunc(f func(int))

SetExitFunc sets the exit function for testing purposes

func TestContext

func TestContext(ctx context.Context) context.Context

TestContext returns a newcontext with the default logger, for use in tests only

func WithLogger

func WithLogger(ctx context.Context, logger Logger) context.Context

WithLogger adds a logger to the context

Types

type CtxKey

type CtxKey string

CtxKey contains context keys for this package

const (
	// LoggerKey is used to obtain/set the logger from a context
	LoggerKey CtxKey = "logger"
)

type Fields

type Fields map[string]any

Fields are used to carry the values of each field

type Hook

type Hook interface {
	Levels() []Level
	Fire(ctx context.Context, level Level, msg string, fields Fields)
}

Hook is used to tap into the log

func GetGlobalHooks

func GetGlobalHooks() []Hook

GetGlobalHooks returns all global hooks

type Level

type Level string

Level is used to indicate log level

const (
	// Debug level events
	Debug Level = "debug"
	// Info level events
	Info Level = "info"
	// Warn level events
	Warn Level = "warn"
	// Error level events
	Error Level = "error"
)

func ParseLevel

func ParseLevel(s string) (Level, error)

ParseLevel parses a string into a Level enum

func ParseLevelOr

func ParseLevelOr(s string, defaultLevel Level) (Level, error)

type LogType

type LogType string
const (
	JSON LogType = "json"
	Text LogType = "text"
)

func TypeFromString

func TypeFromString(s string) (LogType, error)

func TypeFromStringOr

func TypeFromStringOr(s string, defaultType LogType) (LogType, error)

type Logger

type Logger interface {
	WithFields(fields Fields) Logger
	WithField(name string, value any) Logger
	WithError(err error) Logger

	// When issuing a log, adding this will panic
	WithPanic() Logger
	// When issuing a log, adding this will exit 1
	WithFatal() Logger

	Debug(ctx context.Context, msg string)

	Info(ctx context.Context, msg string)

	Warn(ctx context.Context, msg string)

	Error(ctx context.Context, msg string)

	// NewBackgroundContext returns a new context with this logger in it
	NewBackgroundContext() context.Context

	// InContext returns a new context with this logger in it
	InContext(ctx context.Context) (context.Context, Logger)

	Level() Level
}

Logger exports a logging interface

func GetLoggerFromContextOrNil

func GetLoggerFromContextOrNil(ctx context.Context) Logger

GetLoggerFromContextOrNil returns a logger from context, returns nil if not found You probably should use one of the other functions that return a logger instead of this one

func InitLogger

func InitLogger() Logger

func NewSlogLogger

func NewSlogLogger(logLevel Level, format LogType, hooks ...Hook) Logger

NewSlogLogger returns a slog based logger

func NewSlogLoggerCustom

func NewSlogLoggerCustom(logLevel Level, format LogType, out io.Writer, hooks ...Hook) Logger

NewSlogLoggerCustom returns a slog based logger with custom output destination

func NewTestLogger

func NewTestLogger(logLevel Level, format LogType, hooks ...Hook) Logger

NewTestLogger creates a logger that doesn't output to stdout for testing

func RequireLoggerFromContext

func RequireLoggerFromContext(ctx context.Context) Logger

RequireLoggerFromContext returns a logger from context, panics if not found This should be used almost

type TestEntry

type TestEntry struct {
	Level  Level
	Msg    string
	Fields Fields
}

TestEntry represents a captured log entry

type TestHook

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

TestHook is a hook implementation for testing that captures log messages

func NewTestHook

func NewTestHook() *TestHook

NewTestHook creates a new test hook

func (*TestHook) AllEntries

func (h *TestHook) AllEntries() []TestEntry

AllEntries returns all captured log entries

func (*TestHook) Fire

func (h *TestHook) Fire(_ context.Context, level Level, msg string, fields Fields)

Fire is called when a log event is fired

func (*TestHook) LastEntry

func (h *TestHook) LastEntry() *TestEntry

LastEntry returns the last captured log entry

func (*TestHook) Levels

func (h *TestHook) Levels() []Level

Levels returns the levels this hook should fire on

func (*TestHook) Reset

func (h *TestHook) Reset()

Reset clears all captured entries

Jump to

Keyboard shortcuts

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