Documentation
¶
Index ¶
- Constants
- func AddGlobalHook(hook Hook)
- func DeepCopy(i initLogger) initLogger
- func GetExitFunc() func(int)
- func InitLoggerInContext() context.Context
- func SetExitFunc(f func(int))
- func TestContext(ctx context.Context) context.Context
- func WithLogger(ctx context.Context, logger Logger) context.Context
- type CtxKey
- type Fields
- type Hook
- type Level
- type LogType
- type Logger
- func GetLoggerFromContextOrNil(ctx context.Context) Logger
- func InitLogger() Logger
- func NewSlogLogger(logLevel Level, format LogType, hooks ...Hook) Logger
- func NewSlogLoggerCustom(logLevel Level, format LogType, out io.Writer, hooks ...Hook) Logger
- func NewTestLogger(logLevel Level, format LogType, hooks ...Hook) Logger
- func RequireLoggerFromContext(ctx context.Context) Logger
- type TestEntry
- type TestHook
Constants ¶
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 InitLoggerInContext ¶
func SetExitFunc ¶
func SetExitFunc(f func(int))
SetExitFunc sets the exit function for testing purposes
func TestContext ¶
TestContext returns a newcontext with the default logger, for use in tests only
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 Hook ¶
type Hook interface {
Levels() []Level
Fire(ctx context.Context, level Level, msg string, fields Fields)
}
Hook is used to tap into the log
type Level ¶
type Level string
Level is used to indicate log level
func ParseLevel ¶
ParseLevel parses a string into a Level enum
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 ¶
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 ¶
NewSlogLogger returns a slog based logger
func NewSlogLoggerCustom ¶
NewSlogLoggerCustom returns a slog based logger with custom output destination
func NewTestLogger ¶
NewTestLogger creates a logger that doesn't output to stdout for testing
func RequireLoggerFromContext ¶
RequireLoggerFromContext returns a logger from context, panics if not found This should be used almost
type TestHook ¶
type TestHook struct {
// contains filtered or unexported fields
}
TestHook is a hook implementation for testing that captures log messages
func (*TestHook) AllEntries ¶
AllEntries returns all captured log entries