Documentation
¶
Overview ¶
Package logger provides structured logging facilities with custom log levels, pattern layouts, ANSI color support, and slog integration for Rays.
Index ¶
- Constants
- func GetEmitterIdentity() string
- func ReduceFunctionName(function string) string
- func SetEmitterIdentity(identity string)
- type AttrNode
- type Buffer
- type ColorNode
- type CompositeNode
- type GroupNode
- type Handler
- func (this *Handler) EnableColor()
- func (this *Handler) Enabled(_ context.Context, level slog.Level) bool
- func (this *Handler) GetFormat() string
- func (this *Handler) Handle(_ context.Context, r slog.Record) error
- func (this *Handler) IsColor() bool
- func (this *Handler) ReplaceAttr() func(groups []string, a slog.Attr) slog.Attr
- func (this *Handler) SetColor(enabled bool)
- func (this *Handler) SetFormat(format string)
- func (this *Handler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (this *Handler) WithGroup(name string) slog.Handler
- type KeyNode
- type LiteralNode
- type Logger
- func (this *Logger) AddSource()
- func (this *Logger) Clone() *Logger
- func (this *Logger) EnableColor()
- func (this *Logger) Fatal(msg string, args ...any)
- func (this *Logger) FatalContext(ctx context.Context, msg string, args ...any)
- func (this *Logger) GetFormat() string
- func (this *Logger) GetHandlerOptions() slog.HandlerOptions
- func (this *Logger) IsColor() bool
- func (this Logger) New() *Logger
- func (this *Logger) Printf(format string, v ...any)
- func (this *Logger) Reset()
- func (this *Logger) SetColor(enabled bool)
- func (this *Logger) SetDefault()
- func (this *Logger) SetFormat(format string)
- func (this *Logger) SetHandler(handlerFunc func(io.Writer, *slog.HandlerOptions) slog.Handler)
- func (this *Logger) SetHandlerOptions(options *slog.HandlerOptions)
- func (this *Logger) SetLogLevel(level slog.Level)
- func (this *Logger) SetOutput(output io.Writer)
- func (this *Logger) Trace(msg string, args ...any)
- func (this *Logger) TraceContext(ctx context.Context, msg string, args ...any)
- func (this Logger) With(args ...any) *Logger
- func (this Logger) WithGroup(name string) *Logger
- type NotLastNode
- type PatternNode
- type ValueFormatNode
Constants ¶
const ( // LevelTrace defines the trace log level (-8), more granular than LevelDebug. LevelTrace = slog.Level(-8) // LevelDebug defines the debug log level (-4). LevelDebug = slog.LevelDebug // LevelInfo defines the informational log level (0). LevelInfo = slog.LevelInfo // LevelWarn defines the warning log level (4). LevelWarn = slog.LevelWarn // LevelError defines the error log level (8). LevelError = slog.LevelError // LevelFatal defines the fatal log level (12), representing unrecoverable errors. LevelFatal = slog.Level(12) )
const ( // DefaultFormat is the default log pattern format using colored output. DefaultFormat = "" /* 131-byte string literal not displayed */ // TextFormat is the standard slog logfmt-style text pattern. TextFormat = `` /* 128-byte string literal not displayed */ // JSONFormat is the standard JSON log format. JSONFormat = `` /* 129-byte string literal not displayed */ // ECSFormat is the Elastic Common Schema (ECS) v1.2.0 JSON log format. ECSFormat = `` /* 183-byte string literal not displayed */ // GELFFormat is the Graylog Extended Log Format (GELF) v1.1 JSON format. GELFFormat = `` /* 183-byte string literal not displayed */ // LogstashFormat is the Logstash JSON event format. LogstashFormat = `` /* 168-byte string literal not displayed */ )
Variables ¶
This section is empty.
Functions ¶
func GetEmitterIdentity ¶
func GetEmitterIdentity() string
GetEmitterIdentity returns the currently configured emitter identity, or the system hostname if unset.
func ReduceFunctionName ¶
ReduceFunctionName reduces a fully qualified function name for concise log source reporting.
func SetEmitterIdentity ¶
func SetEmitterIdentity(identity string)
SetEmitterIdentity sets the emitter identity (hostname) used in log formats like GELF.
Types ¶
type AttrNode ¶
type AttrNode struct {
// contains filtered or unexported fields
}
AttrNode renders structured log attributes.
type Buffer ¶
type Buffer []byte
Buffer is a byte buffer.
This implementation is adapted from the unexported type buffer in go/src/fmt/print.go.
func NewBuffer ¶
func NewBuffer() *Buffer
NewBuffer retrieves a Buffer from the pool. Callers should call Free when finished.
func (*Buffer) Free ¶
func (b *Buffer) Free()
Free returns the Buffer to the pool if its capacity does not exceed the maximum buffer size.
func (*Buffer) String ¶
String returns the contents of the unread portion of the buffer as a string.
type ColorNode ¶
type ColorNode struct {
// contains filtered or unexported fields
}
ColorNode wraps an inner pattern node with ANSI mode and color styling.
type CompositeNode ¶
type CompositeNode struct {
// contains filtered or unexported fields
}
CompositeNode renders an ordered sequence of child pattern nodes.
func (*CompositeNode) Render ¶
func (n *CompositeNode) Render(ctx *renderContext, buf *Buffer)
Render evaluates and writes all child pattern nodes sequentially to the output buffer.
type GroupNode ¶
type GroupNode struct {
// contains filtered or unexported fields
}
GroupNode renders attribute groups.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is an slog.Handler implementation that formats log records using a compiled pattern-based layout engine with ANSI color and mode support.
func NewHandler ¶
func NewHandler(w io.Writer, opts *slog.HandlerOptions) *Handler
NewHandler creates a new Handler writing to w with DefaultFormat and the given options.
func NewHandlerWithFormat ¶
NewHandlerWithFormat creates a new Handler writing to w using the specified pattern format. If format is empty, DefaultFormat is used.
func (*Handler) EnableColor ¶
func (this *Handler) EnableColor()
EnableColor restores AUTO mode and refreshes detection. Call it after changing a writer's terminal capability to invalidate the cached result immediately.
func (*Handler) Enabled ¶
Enabled reports whether the handler emits log records at the specified level.
func (*Handler) Handle ¶
Handle formats and writes the slog.Record to the destination writer according to the pattern AST.
func (*Handler) ReplaceAttr ¶
ReplaceAttr returns the attribute replacement function from HandlerOptions, or a default pass-through function if none was provided.
func (*Handler) SetFormat ¶
SetFormat updates the pattern format and recompiles the layout AST. If format is empty, DefaultFormat is used.
type KeyNode ¶
type KeyNode struct {
// contains filtered or unexported fields
}
KeyNode renders standard slog record attributes (time, level, source, message).
type LiteralNode ¶
type LiteralNode struct {
// contains filtered or unexported fields
}
LiteralNode renders fixed static text.
func (*LiteralNode) Render ¶
func (n *LiteralNode) Render(_ *renderContext, buf *Buffer)
Render writes literal static text to the output buffer.
type Logger ¶
Logger wraps slog.Logger with support for custom log levels (Trace, Fatal), dynamic format patterns, ANSI color modes, and handler configuration.
func NewLogger ¶
func NewLogger() *Logger
NewLogger initializes and returns a new Logger writing to os.Stderr with default settings.
func (*Logger) AddSource ¶
func (this *Logger) AddSource()
AddSource enables source file and line number reporting in log outputs.
func (*Logger) EnableColor ¶
func (this *Logger) EnableColor()
EnableColor sets ANSI color output to automatic detection based on terminal capability.
func (*Logger) FatalContext ¶
FatalContext logs a message at LevelFatal with the provided context.
func (*Logger) GetHandlerOptions ¶
func (this *Logger) GetHandlerOptions() slog.HandlerOptions
GetHandlerOptions returns a copy of the underlying slog.HandlerOptions.
func (Logger) New ¶
New initializes and returns a new Logger writing to os.Stderr with default settings.
func (*Logger) Reset ¶
func (this *Logger) Reset()
Reset restores the logger to its default handler, format, and color mode while preserving the logger's output and handler options, including the configured log level.
func (*Logger) SetDefault ¶
func (this *Logger) SetDefault()
SetDefault sets this Logger as the standard library's default slog logger.
func (*Logger) SetFormat ¶
SetFormat configures the pattern layout format on the underlying handler if supported.
func (*Logger) SetHandler ¶
SetHandler configures a new handler factory function and rebuilds the underlying slog.Logger.
func (*Logger) SetHandlerOptions ¶
func (this *Logger) SetHandlerOptions(options *slog.HandlerOptions)
SetHandlerOptions replaces the logger's HandlerOptions and rebuilds the underlying handler.
func (*Logger) SetLogLevel ¶
SetLogLevel sets the minimum logging level required to emit records.
func (*Logger) TraceContext ¶
TraceContext logs a message at LevelTrace with the provided context.
type NotLastNode ¶
type NotLastNode struct {
// contains filtered or unexported fields
}
NotLastNode renders text only if the current element is not the last one in an iteration.
func (*NotLastNode) Render ¶
func (n *NotLastNode) Render(ctx *renderContext, buf *Buffer)
Render writes separator text if the current element is not the last one in an iteration.
type PatternNode ¶
type PatternNode interface {
Render(ctx *renderContext, buf *Buffer)
}
PatternNode represents a node in the compiled log pattern AST.
type ValueFormatNode ¶
type ValueFormatNode struct {
// contains filtered or unexported fields
}
ValueFormatNode formats attribute keys, values, and groups with optional padding, truncation, quoting, or custom layouts.
func (*ValueFormatNode) Render ¶
func (n *ValueFormatNode) Render(ctx *renderContext, buf *Buffer)
Render formats attribute keys, values, or group names applying padding, truncation, or alignment.