glog

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 14 Imported by: 9

Documentation

Index

Constants

View Source
const (
	LevelTrace = slog.Level(-8)
	LevelFatal = slog.Level(20)
)
View Source
const (
	Trace = "TRACE"
	Debug = "DEBUG"
	Info  = "INFO"
	Warn  = "WARN"
	Error = "ERROR"
	Fatal = "FATAL"
)
View Source
const (
	LoggerTypeConsole = "console"
	LoggerTypePretty  = "pretty"
	LoggerTypeJSON    = "json"
)

Variables

View Source
var ColorConsoleTSFormat = "2006-01-02 15:04:05.000"
View Source
var CustomLevels = map[slog.Leveler]string{
	LevelTrace: Trace,
	LevelFatal: Fatal,
}
View Source
var DefaultLogLevel = Info

Functions

func Arg added in v0.1.1

func Arg(key string, value any) any

func NewColorConsoleHandler

func NewColorConsoleHandler(out io.Writer, opts *slog.HandlerOptions) slog.Handler

NewColorConsoleHandler creates a new ColorConsoleHandler with the provided options

func NewFocusFilterHandler

func NewFocusFilterHandler(handler slog.Handler, logger *BaseLogger) slog.Handler

func NormalizeLevel added in v0.10.0

func NormalizeLevel(value string) string

NormalizeLevel returns the canonical log-level constant for value. Matching is case-insensitive and ignores surrounding whitespace. Unsupported and empty values fall back to Info.

func NormalizeLoggerType added in v0.10.0

func NormalizeLoggerType(value string) string

NormalizeLoggerType returns the canonical output type for value. Matching is case-insensitive and ignores surrounding whitespace. Text is accepted as an alias for console. Unsupported and empty values fall back to JSON.

func Resolve added in v0.8.0

func Resolve(name string, provider LoggerProvider, logger Logger) (LoggerProvider, Logger)

Resolve resolves provider and logger with precedence provider > logger > nop.

Types

type ArgsList added in v0.7.0

type ArgsList []any

func Args added in v0.1.1

func Args(args ...any) ArgsList

type BaseLogger

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

BaseLogger implements both Logger and LoggerProvider interfaces

func NewLogger

func NewLogger(options ...Option) *BaseLogger

func (*BaseLogger) Debug

func (c *BaseLogger) Debug(msg string, args ...any)

func (*BaseLogger) Error

func (c *BaseLogger) Error(msg string, args ...any)

func (*BaseLogger) Fatal added in v0.1.1

func (c *BaseLogger) Fatal(msg string, args ...any)

func (*BaseLogger) Focus

func (c *BaseLogger) Focus(names ...string)

func (*BaseLogger) GetLogger

func (c *BaseLogger) GetLogger(name string) Logger

func (*BaseLogger) Info

func (c *BaseLogger) Info(msg string, args ...any)

func (*BaseLogger) SetGlobalFields added in v0.10.0

func (c *BaseLogger) SetGlobalFields(fields map[string]any) *BaseLogger

SetGlobalFields atomically replaces the root-owned fields included with every record in this logger tree. The supplied map is copied. A nil or empty map clears the current fields.

func (*BaseLogger) Trace

func (c *BaseLogger) Trace(msg string, args ...any)

func (*BaseLogger) Unfocus

func (c *BaseLogger) Unfocus()

func (*BaseLogger) Warn

func (c *BaseLogger) Warn(msg string, args ...any)

func (*BaseLogger) With added in v0.1.1

func (c *BaseLogger) With(args ...any) *BaseLogger

With returns a Logger that includes the given attributes in each subsequent log output.

func (*BaseLogger) WithContext

func (c *BaseLogger) WithContext(ctx context.Context) Logger

WithLevel sets the log level and returns the logger

func (*BaseLogger) WithFields added in v0.4.0

func (c *BaseLogger) WithFields(fields map[string]any) Logger

WithFields returns a logger that includes the given structured fields.

func (*BaseLogger) WithLevel

func (c *BaseLogger) WithLevel(level string) *BaseLogger

WithLevel sets the log level and returns the logger

func (*BaseLogger) WithLoggerType

func (c *BaseLogger) WithLoggerType(loggerType string) Logger

type ColorConsoleHandler

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

ColorConsoleHandler is a custom slog.Handler that outputs colored logs to the console

func (*ColorConsoleHandler) Enabled

func (h *ColorConsoleHandler) Enabled(ctx context.Context, level slog.Level) bool

Enabled implements slog.Handler.

func (*ColorConsoleHandler) Handle

func (h *ColorConsoleHandler) Handle(ctx context.Context, r slog.Record) error

Handle implements slog.Handler.

func (*ColorConsoleHandler) WithAttrs

func (h *ColorConsoleHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs implements slog.Handler.

func (*ColorConsoleHandler) WithGroup

func (h *ColorConsoleHandler) WithGroup(name string) slog.Handler

WithGroup implements slog.Handler.

func (*ColorConsoleHandler) WithTSFormat

func (h *ColorConsoleHandler) WithTSFormat(format string) *ColorConsoleHandler

type ColorConsoleOption

type ColorConsoleOption func(*ColorConsoleHandler)

func WithColorConsoleTSFormat

func WithColorConsoleTSFormat(format string) ColorConsoleOption

type FatalBehavior added in v0.7.0

type FatalBehavior int
const (
	FatalBehaviorExit FatalBehavior = iota
	FatalBehaviorPanic
	FatalBehaviorLogOnly
)

type FieldsLogger added in v0.4.0

type FieldsLogger interface {
	WithFields(map[string]any) Logger
}

FieldsLogger is an optional extension for attaching structured fields.

type FocusFilterHandler

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

func (*FocusFilterHandler) Enabled

func (h *FocusFilterHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*FocusFilterHandler) Handle

func (h *FocusFilterHandler) Handle(ctx context.Context, r slog.Record) error

func (*FocusFilterHandler) WithAttrs

func (h *FocusFilterHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*FocusFilterHandler) WithGroup

func (h *FocusFilterHandler) WithGroup(name string) slog.Handler

type Logger

type Logger interface {
	Trace(msg string, args ...any)
	Debug(msg string, args ...any)
	Info(msg string, args ...any)
	Warn(msg string, args ...any)
	Error(msg string, args ...any)
	Fatal(msg string, args ...any)
	WithContext(ctx context.Context) Logger
}

func Ensure added in v0.8.0

func Ensure(logger Logger) Logger

Ensure guarantees a non-nil Logger.

func Nop added in v0.8.0

func Nop() Logger

Nop returns a canonical no-op logger.

type LoggerProvider

type LoggerProvider interface {
	GetLogger(name string) Logger
}

func ProviderFromLogger added in v0.8.0

func ProviderFromLogger(logger Logger) LoggerProvider

ProviderFromLogger wraps a single logger as a provider.

func ProviderWithFallback added in v0.8.0

func ProviderWithFallback(provider LoggerProvider, fallback Logger) LoggerProvider

ProviderWithFallback wraps a provider and applies a fallback when nil is returned.

type Option

type Option func(*BaseLogger)

func WithAddSource added in v0.3.0

func WithAddSource(add bool) Option

func WithCallerSkip added in v0.9.0

func WithCallerSkip(skip int) Option

WithCallerSkip adds known adapter frames to the caller depth used for slog.Record.PC and error stack capture. Values below zero are treated as zero and excessive values are bounded. Use this only for stable wrapper layers owned by the application; it is not a general stack-hiding feature.

func WithContext

func WithContext(ctx context.Context) Option

func WithExitFunc added in v0.7.0

func WithExitFunc(exit func(int)) Option

WithExitFunc overrides the default exit behavior for Fatal.

func WithFatalBehavior added in v0.7.0

func WithFatalBehavior(behavior FatalBehavior) Option

WithFatalBehavior configures what Fatal does after logging.

func WithGlobalFields added in v0.10.0

func WithGlobalFields(fields map[string]any) Option

WithGlobalFields configures the initial root-owned fields included with every record in the logger tree. The supplied map is copied.

func WithHandlerWrapper added in v0.5.0

func WithHandlerWrapper(wrapper func(slog.Handler) slog.Handler) Option

WithHandlerWrapper wraps the base slog handler before focus/name handlers. Multiple options compose in declaration order: the first wrapper is closest to the base handler and each later wrapper becomes the new outer wrapper.

func WithLevel

func WithLevel(level string) Option

func WithLoggerType

func WithLoggerType(loggerType string) Option

func WithLoggerTypeConsole

func WithLoggerTypeConsole() Option

func WithLoggerTypeJSON

func WithLoggerTypeJSON() Option

func WithLoggerTypePretty

func WithLoggerTypePretty() Option

func WithName

func WithName(name string) Option

func WithRichErrorHandler added in v0.2.0

func WithRichErrorHandler(handler RichErrorHandler) Option

WithRichErrorHandler sets a custom handler function to extract attributes from errors

func WithWriter added in v0.5.0

func WithWriter(writer io.Writer) Option

WithWriter sets the output writer for the underlying slog handler.

type RichErrorHandler added in v0.2.0

type RichErrorHandler func(err error) []slog.Attr

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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