logger

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package logger provides structured logging facilities with custom log levels, pattern layouts, ANSI color support, and slog integration for Rays.

Index

Constants

View Source
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)
)
View Source
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

func ReduceFunctionName(function string) string

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.

func (*AttrNode) Render

func (n *AttrNode) Render(ctx *renderContext, buf *Buffer)

Render iterates over collected attributes, formatting each attribute with the inner pattern.

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) Len

func (b *Buffer) Len() int

Len returns the number of bytes in the buffer.

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset resets the buffer length to zero.

func (*Buffer) SetLen

func (b *Buffer) SetLen(n int)

SetLen truncates the buffer length to n bytes.

func (*Buffer) String

func (b *Buffer) String() string

String returns the contents of the unread portion of the buffer as a string.

func (*Buffer) Write

func (b *Buffer) Write(p []byte) (int, error)

Write appends the byte slice p to the buffer.

func (*Buffer) WriteByte

func (b *Buffer) WriteByte(c byte) error

WriteByte appends a single byte c to the buffer.

func (*Buffer) WriteString

func (b *Buffer) WriteString(s string) (int, error)

WriteString appends the string s to the buffer.

type ColorNode

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

ColorNode wraps an inner pattern node with ANSI mode and color styling.

func (*ColorNode) Render

func (n *ColorNode) Render(ctx *renderContext, buf *Buffer)

Render evaluates the inner pattern node wrapped in ANSI escape codes if color is enabled.

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.

func (*GroupNode) Render

func (n *GroupNode) Render(ctx *renderContext, buf *Buffer)

Render formats structured groups using the inner pattern or nested JSON/GELF structures.

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

func NewHandlerWithFormat(w io.Writer, opts *slog.HandlerOptions, format string) *Handler

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

func (this *Handler) Enabled(_ context.Context, level slog.Level) bool

Enabled reports whether the handler emits log records at the specified level.

func (*Handler) GetFormat

func (this *Handler) GetFormat() string

GetFormat returns the currently configured pattern format string.

func (*Handler) Handle

func (this *Handler) Handle(_ context.Context, r slog.Record) error

Handle formats and writes the slog.Record to the destination writer according to the pattern AST.

func (*Handler) IsColor

func (this *Handler) IsColor() bool

IsColor reports whether ANSI color output is currently enabled.

func (*Handler) ReplaceAttr

func (this *Handler) ReplaceAttr() func(groups []string, a slog.Attr) slog.Attr

ReplaceAttr returns the attribute replacement function from HandlerOptions, or a default pass-through function if none was provided.

func (*Handler) SetColor

func (this *Handler) SetColor(enabled bool)

SetColor explicitly enables or disables ANSI color output.

func (*Handler) SetFormat

func (this *Handler) SetFormat(format string)

SetFormat updates the pattern format and recompiles the layout AST. If format is empty, DefaultFormat is used.

func (*Handler) WithAttrs

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

WithAttrs returns a new Handler with the given attributes appended to the handler's pre-formatted attributes.

func (*Handler) WithGroup

func (this *Handler) WithGroup(name string) slog.Handler

WithGroup returns a new Handler with the given group name appended to the current group hierarchy.

type KeyNode

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

KeyNode renders standard slog record attributes (time, level, source, message).

func (*KeyNode) Render

func (n *KeyNode) Render(ctx *renderContext, buf *Buffer)

Render binds the current record field or attribute key and evaluates the inner pattern node.

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

type Logger struct {
	*slog.Logger
	// contains filtered or unexported fields
}

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) Clone

func (this *Logger) Clone() *Logger

Clone creates a duplicate copy of the Logger with independent configuration.

func (*Logger) EnableColor

func (this *Logger) EnableColor()

EnableColor sets ANSI color output to automatic detection based on terminal capability.

func (*Logger) Fatal

func (this *Logger) Fatal(msg string, args ...any)

Fatal logs a message at LevelFatal using context.Background().

func (*Logger) FatalContext

func (this *Logger) FatalContext(ctx context.Context, msg string, args ...any)

FatalContext logs a message at LevelFatal with the provided context.

func (*Logger) GetFormat

func (this *Logger) GetFormat() string

GetFormat returns the currently configured pattern layout format string.

func (*Logger) GetHandlerOptions

func (this *Logger) GetHandlerOptions() slog.HandlerOptions

GetHandlerOptions returns a copy of the underlying slog.HandlerOptions.

func (*Logger) IsColor

func (this *Logger) IsColor() bool

IsColor reports whether ANSI color output is enabled.

func (Logger) New

func (this Logger) New() *Logger

New initializes and returns a new Logger writing to os.Stderr with default settings.

func (*Logger) Printf

func (this *Logger) Printf(format string, v ...any)

Printf formats and logs an informational message at LevelInfo.

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) SetColor

func (this *Logger) SetColor(enabled bool)

SetColor explicitly forces ANSI color output on or off.

func (*Logger) SetDefault

func (this *Logger) SetDefault()

SetDefault sets this Logger as the standard library's default slog logger.

func (*Logger) SetFormat

func (this *Logger) SetFormat(format string)

SetFormat configures the pattern layout format on the underlying handler if supported.

func (*Logger) SetHandler

func (this *Logger) SetHandler(handlerFunc func(io.Writer, *slog.HandlerOptions) slog.Handler)

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

func (this *Logger) SetLogLevel(level slog.Level)

SetLogLevel sets the minimum logging level required to emit records.

func (*Logger) SetOutput

func (this *Logger) SetOutput(output io.Writer)

SetOutput redirects logging output to the specified io.Writer.

func (*Logger) Trace

func (this *Logger) Trace(msg string, args ...any)

Trace logs a message at LevelTrace using context.Background().

func (*Logger) TraceContext

func (this *Logger) TraceContext(ctx context.Context, msg string, args ...any)

TraceContext logs a message at LevelTrace with the provided context.

func (Logger) With

func (this Logger) With(args ...any) *Logger

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

func (Logger) WithGroup

func (this Logger) WithGroup(name string) *Logger

WithGroup returns a new Logger that starts a group with the given name.

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.

Jump to

Keyboard shortcuts

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