Documentation
¶
Overview ¶
The log package contains log/slog related types.
Index ¶
- Variables
- func MustLogger(ctx context.Context) *slog.Logger
- func MustWithAttrs(ctx context.Context, args ...any) (context.Context, *slog.Logger)
- func MustWithGroup(ctx context.Context, name string) (context.Context, *slog.Logger)
- func MustWithGroupAttrs(ctx context.Context, name string, args ...any) (context.Context, *slog.Logger)
- func WithLogger(ctx context.Context, logger *slog.Logger) context.Context
- func WithTestLogger(ctx context.Context) context.Context
- type BufferedHandler
- func (h *BufferedHandler) Enabled(ctx context.Context, level slog.Level) bool
- func (h *BufferedHandler) Flush() error
- func (h *BufferedHandler) Handle(ctx context.Context, record slog.Record) error
- func (h *BufferedHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *BufferedHandler) WithGroup(name string) slog.Handler
- type MultiHandler
- type TerminalHandlerColorScheme
- type TerminalHandlerOptions
- type TerminalLineHandler
- func (h *TerminalLineHandler) Enabled(ctx context.Context, level slog.Level) bool
- func (h *TerminalLineHandler) Handle(ctx context.Context, record slog.Record) error
- func (h *TerminalLineHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *TerminalLineHandler) WithGroup(name string) slog.Handler
- type TerminalTreeHandler
- type TerminalValue
- type TerminalValuer
Constants ¶
This section is empty.
Variables ¶
var DefaultTerminalHandlerColorScheme = &TerminalHandlerColorScheme{ GroupName: ansi.SGRs{}, AttrKey: ansi.SGRs{ansi.FgCyan, ansi.Dim}, AttrValue: ansi.SGRs{ansi.Dim}, Time: ansi.SGRs{ansi.Dim}, LevelDebug: ansi.SGRs{ansi.FgCyan, ansi.Bold}, MessageDebug: ansi.SGRs{ansi.Bold}, LevelInfo: ansi.SGRs{ansi.FgGreen, ansi.Bold}, MessageInfo: ansi.SGRs{ansi.Bold}, LevelWarn: ansi.SGRs{ansi.FgYellow, ansi.Bold}, MessageWarn: ansi.SGRs{ansi.Bold}, LevelError: ansi.SGRs{ansi.FgRed, ansi.Bold}, MessageError: ansi.SGRs{ansi.Bold}, File: ansi.SGRs{ansi.Dim, ansi.FgBlue}, Line: ansi.SGRs{ansi.Dim, ansi.FgBlue}, Function: ansi.SGRs{ansi.Dim, ansi.FgBlue}, }
Functions ¶
func MustLogger ¶
Returns the logger value associated with the context, which must have been previously set with WithLogger. It panics if no logger value has been set previously.
func MustWithAttrs ¶
Returns a copy of the given context with a logger that has attributes added to it. The logger is retrieved from the context with MustLogger and attributes are added to it with slog.Logger.With, then the new logger is stored in the returned context.
func MustWithGroup ¶
Returns a copy of the given context with a logger that has a group added to it. The logger is retrieved from the context with MustLogger and a group is added to it with slog.Logger.MustWithGroup, then the new logger is stored in the returned context.
func MustWithGroupAttrs ¶
func MustWithGroupAttrs(ctx context.Context, name string, args ...any) (context.Context, *slog.Logger)
Returns a copy of the given context with a logger that has a group and attributes added to it. The logger is retrieved from the context with MustLogger, a group is added to it with slog.Logger.WithGroup, and attributes are added with slog.Logger.With, then the new logger is stored in the returned context.
func WithLogger ¶
Returns a copy of the given context with the logger value set. The value can be retreived with MustLogger, [MustContextLoggerIndented] or [MustLoggerIndented].
func WithTestLogger ¶
Similar to WithLogger, but constructs a new logger suitable for using during tests.
Types ¶
type BufferedHandler ¶
type BufferedHandler struct {
// contains filtered or unexported fields
}
BufferedHandler is a slog.Handler that buffers log records in memory until Dispatch() is called. This allows for batching log operations which can be useful for performance or to ensure logs from related operations are grouped together.
BufferedHandler wraps another handler that will process the log records when Dispatch() is called. All log records are stored in a shared buffer so that multiple instances created through WithAttrs() or WithGroup() will share the same underlying buffered logs.
The buffer is not automatically flushed - you must call Dispatch() explicitly to process the buffered log records. If Dispatch() is not called, logs will remain in memory and not be processed by the underlying handler.
func NewBufferedHandler ¶
func NewBufferedHandler(handler slog.Handler) *BufferedHandler
func (*BufferedHandler) Flush ¶
func (h *BufferedHandler) Flush() error
Flush processes all buffered log records by sending them to the underlying handler. After a successful dispatch, the buffer is cleared. Returns an error if any of the buffered records fail to process.
It is safe to call Flush() multiple times, though subsequent calls will have no effect until new log records are buffered.
type MultiHandler ¶
type MultiHandler struct {
// contains filtered or unexported fields
}
MultiHandler is a slog.Handler that dispatches log records to multiple handlers. It combines the behavior of multiple handlers into a single handler. When a log record is handled, it is sent to all the registered handlers. If any handler returns an error, the errors are joined together.
func NewMultiHandler ¶
func NewMultiHandler(handlers ...slog.Handler) *MultiHandler
type TerminalHandlerColorScheme ¶
type TerminalHandlerColorScheme struct {
GroupName ansi.SGRs
AttrKey ansi.SGRs
AttrValue ansi.SGRs
Time ansi.SGRs
LevelDebug ansi.SGRs
MessageDebug ansi.SGRs
LevelInfo ansi.SGRs
MessageInfo ansi.SGRs
LevelWarn ansi.SGRs
MessageWarn ansi.SGRs
LevelError ansi.SGRs
MessageError ansi.SGRs
File ansi.SGRs
Line ansi.SGRs
Function ansi.SGRs
}
ANSI color scheme
type TerminalHandlerOptions ¶
type TerminalHandlerOptions struct {
slog.HandlerOptions
// Disable adding an emoji before group names when the group name does not start with an
// emoji code point (see [unicode.IsEmojiStartCodePoint]).
DisableGroupEmoji bool
// Time layout for timestamps; if empty, time is not included in output.
TimeLayout string
// If true, force ANSI escape sequences for color, even when no TTY detected.
ForceColor bool
// If true, disable color, even when TTY detected; takes precedence over ForceColor.
NoColor bool
// ANSI color scheme. Default to DefaultColorScheme if unset.
ColorScheme *TerminalHandlerColorScheme
}
TerminalHandlerOptions extends HandlerOptions with specific options.
type TerminalLineHandler ¶
type TerminalLineHandler struct {
// contains filtered or unexported fields
}
TerminalLineHandler is a slog.Handler implementation that formats log records as lines of text suitable for terminal output, with optional colorization. It provides a structured, human-readable output format with customizable styling through color schemes.
Features: - Terminal-aware colorized output (auto-detects terminal capabilities) - Custom time formatting - Formatted level indicators - Hierarchical group support - Structured attribute rendering - Source code location reporting (when enabled)
The handler will automatically detect if the output is a terminal and enable/disable colors accordingly, unless explicitly configured otherwise.
func NewTerminalLineHandler ¶
func NewTerminalLineHandler(w io.Writer, opts *TerminalHandlerOptions) *TerminalLineHandler
NewTerminalLineHandler creates a new TerminalTextHandler
type TerminalTreeHandler ¶
type TerminalTreeHandler struct {
// contains filtered or unexported fields
}
TerminalTreeHandler implements slog.Handler interface with enhanced console output features. It provides colorized logging with level-appropriate colors, proper indentation for nested groups, and smart handling of multiline content. TerminalTreeHandler automatically detects terminal capabilities and enables or disables ANSI color codes accordingly, though this behavior can be overridden through options. The handler also supports customizable timestamp formats and sophisticated attribute formatting.
func NewTerminalTreeHandler ¶
func NewTerminalTreeHandler(w io.Writer, opts *TerminalHandlerOptions) *TerminalTreeHandler
NewTerminalTreeHandler creates a new TerminalTreeHandler
type TerminalValue ¶ added in v1.1.0
type TerminalValue struct {
// contains filtered or unexported fields
}
TerminalValue represents a string value that may contain ANSI escape sequences. It automatically provides different representations for terminal vs. other handlers: - String() and MarshalText() return the text with all ANSI sequences removed - TerminalValue() returns the original text with ANSI sequences for terminal handlers
func NewTerminalValue ¶ added in v1.1.0
func NewTerminalValue(text string) TerminalValue
NewTerminalValue creates a new TerminalValue from a string that may contain Select Graphic Rendition (SGR) ANSI sequences.
IMPORTANT: Only use Select Graphic Rendition (SGR) escape sequences for colors and text formatting. Other ANSI control sequences (cursor movement, screen clearing, etc.) will disrupt terminal output.
func (TerminalValue) MarshalText ¶ added in v1.1.0
func (tv TerminalValue) MarshalText() ([]byte, error)
MarshalText implements encoding.TextMarshaler for JSON compatibility
func (TerminalValue) String ¶ added in v1.1.0
func (tv TerminalValue) String() string
String returns the text with all ANSI escape sequences removed
func (TerminalValue) TerminalValue ¶ added in v1.1.0
func (tv TerminalValue) TerminalValue() slog.Value
TerminalValue implements TerminalValuer, returning the original text with ANSI sequences
type TerminalValuer ¶ added in v1.1.0
type TerminalValuer interface {
// TerminalValue returns a slog.Value that should only contain
// Select Graphic Rendition (SGR) ANSI escape sequences.
// Other control sequences will disrupt terminal display.
TerminalValue() slog.Value
}
TerminalValuer is implemented by any value that wants to provide a terminal representation for slogxt terminal handlers.
IMPORTANT: Only use Select Graphic Rendition (SGR) escape sequences for colors and text formatting. Other ANSI control sequences (cursor movement, screen clearing, etc.) will disrupt terminal output.