lager

package module
v0.0.0-...-343d286 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2025 License: MIT Imports: 11 Imported by: 0

README

go-lager

Documentation

Index

Constants

View Source
const (
	LevelTrace   = slog.Level(-8)
	LevelDebug   = slog.LevelDebug
	LevelInfo    = slog.LevelInfo
	LevelWarning = slog.LevelWarn
	LevelError   = slog.LevelError
)

Variables

View Source
var DebugTransformFunc = func(s string) string {
	return ansi.Cyan + s + ansi.Reset
}
View Source
var ErrorTransformFunc = func(s string) string {
	return ansi.Red + s + ansi.Reset
}
View Source
var InfoTransformFunc = func(s string) string {
	return s
}
View Source
var TraceTransformFunc = func(s string) string {
	return ansi.Magenta + s + ansi.Reset
}
View Source
var WarnTransformFunc = func(s string) string {
	return ansi.Yellow + s + ansi.Reset
}

Functions

func DebugIndent

func DebugIndent(msg string, indent int, args ...any)

func ErrorIndent

func ErrorIndent(msg string, indent int, args ...any)

func InfoIndent

func InfoIndent(msg string, indent int, args ...any)

func Init

func Init(logFileNm string, level slog.Leveler) (*os.File, error)

func Trace

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

func TraceIndent

func TraceIndent(msg string, indent int, args ...any)

func WarnIndent

func WarnIndent(msg string, indent int, args ...any)

Types

type FileHandler

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

FileHandler strips ANSI codes and prepends datetime

func NewFileHandler

func NewFileHandler(w io.Writer, opts *HandlerOptions) *FileHandler

func (*FileHandler) Enabled

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

Enabled checks if the handler is enabled for the given log level

func (*FileHandler) Handle

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

Handle processes a log record and writes it to the file

func (*FileHandler) TimeFormat

func (h *FileHandler) TimeFormat(format string) *FileHandler

func (*FileHandler) WithAttrs

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

WithAttrs adds attributes to the handler

func (*FileHandler) WithGroup

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

WithGroup adds a group name to the handler

type HandlerOptions

type HandlerOptions struct {
	// AddTime causes the handler to include the timestamp in the output.
	AddTime bool

	// AddLevel causes the handler to include the log level in the output.
	AddLevel bool

	// AddSource causes the handler to compute the source code position
	// of the log statement and add a SourceKey attribute to the output.
	AddSource bool

	// AddAttrs causes the handler to include the attributes in the output.
	AddAttrs bool

	// AddGroups causes the handler to include the groups in the output.
	AddGroups bool

	// NoColor causes the handler to strip ANSI escape codes from the
	// output.
	NoColor bool

	// Level reports the minimum level to log.
	// Levels with lower levels are discarded.
	// If nil, the Handler uses [slog.LevelInfo].
	// This field is ignored if Enablers is not nil.
	Level slog.Leveler

	// Enablers is a list of functions that determine whether a log
	// record should be logged. If any function returns false,
	// the record is discarded. If nil or empty, only the Level
	// field is used to determine whether to log a record.
	Enablers []func(ctx context.Context, level slog.Level) bool

	// ReplaceAttr is called to rewrite each non-group attribute before it is logged.
	// The attribute's value has been resolved (see [Value.Resolve]).
	// If ReplaceAttr returns a zero Attr, the attribute is discarded.
	//
	// The built-in attributes with keys "time", "level", "source", and "msg"
	// are passed to this function, except that time is omitted
	// if zero, and source is omitted if AddSource is false.
	//
	// The first argument is a list of currently open groups that contain the
	// Attr. It must not be retained or modified. ReplaceAttr is never called
	// for Group attributes, only their contents. For example, the attribute
	// list
	//
	//     Int("a", 1), Group("g", Int("b", 2)), Int("c", 3)
	//
	// results in consecutive calls to ReplaceAttr with the following arguments:
	//
	//     nil, Int("a", 1)
	//     []string{"g"}, Int("b", 2)
	//     nil, Int("c", 3)
	//
	// ReplaceAttr can be used to change the default keys of the built-in
	// attributes, convert types (for example, to replace a `time.Time` with the
	// integer seconds since the Unix epoch), sanitize personal information, or
	// remove attributes from the output.
	ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
}

type MultiHandler

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

MultiHandler is a [Handler] that invokes all the given Handlers. Its Enable method reports whether any of the handlers' Enabled methods return true. Its Handle, WithAttr and WithGroup methods call the corresponding method on each of the enabled handlers.

func NewMultiHandler

func NewMultiHandler(handlers ...slog.Handler) *MultiHandler

NewMultiHandler creates a MultiHandler with the given Handlers.

func (*MultiHandler) Enabled

func (h *MultiHandler) Enabled(ctx context.Context, l slog.Level) bool

func (*MultiHandler) Handle

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

func (*MultiHandler) WithAttrs

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

func (*MultiHandler) WithGroup

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

type StderrHandler

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

func NewStderrHandler

func NewStderrHandler(opts *HandlerOptions) *StderrHandler

func (*StderrHandler) Enabled

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

Enabled checks if the handler is enabled for the given log level

func (*StderrHandler) Handle

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

Handle processes a log record and writes the message to the handler's output

func (*StderrHandler) WithAttrs

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

WithAttrs returns a new handler with the given attributes added

func (*StderrHandler) WithGroup

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

WithGroup returns a new handler with the given group name

type StdoutHandler

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

func NewStdoutHandler

func NewStdoutHandler(opts *HandlerOptions) *StdoutHandler

func (*StdoutHandler) Enabled

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

Enabled checks if the handler is enabled for the given log level

func (*StdoutHandler) Handle

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

Handle processes a log record and writes the message to the handler's output

func (*StdoutHandler) WithAttrs

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

WithAttrs returns a new handler with the given attributes added

func (*StdoutHandler) WithGroup

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

WithGroup returns a new handler with the given group name

type StreamHandler

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

StreamHandler prints only the message text

func NewStreamHandler

func NewStreamHandler(st StreamType, opts *HandlerOptions) *StreamHandler

NewStreamHandler creates a new StreamHandler with the given writer and options

func (*StreamHandler) Enabled

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

Enabled checks if the handler is enabled for the given log level

func (*StreamHandler) Handle

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

Handle processes a log record and writes the message to the handler's output

func (*StreamHandler) WithAttrs

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

WithAttrs returns a new handler with the given attributes added

func (*StreamHandler) WithGroup

func (h *StreamHandler) WithGroup(string) slog.Handler

WithGroup returns a new handler with the given group name

type StreamType

type StreamType int
const (
	StreamStdout StreamType = iota
	StreamStderr
)

Jump to

Keyboard shortcuts

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