logger

package module
v6.0.0-beta.1 Latest Latest
Warning

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

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

README

Documentation

Overview

Package logger provides the RoadRunner logger plugin backed by log/slog.

It supports multiple output modes (development, production, raw, off/none), per-plugin channel overrides, and file-based output.

Index

Constants

View Source
const PluginName = "logs"

PluginName declares plugin name.

Variables

This section is empty.

Functions

This section is empty.

Types

type BuildResult

type BuildResult struct {
	Logger  *slog.Logger
	Closers []io.Closer
}

BuildResult holds the logger and any resources that need cleanup.

type ChannelConfig

type ChannelConfig struct {
	// Dedicated channels per logger. By default logger allocated via named logger.
	Channels map[string]*Config `mapstructure:"channels"`
}

ChannelConfig configures loggers per channel.

type Config

type Config struct {
	// Mode configures logger based on some default template (development,
	// production, raw, off, none).
	Mode Mode `mapstructure:"mode"`

	// Level is the minimum enabled logging level.
	Level string `mapstructure:"level"`

	// Format is a custom format string with %placeholder% tokens (e.g.
	// "%time% [%level%] %message% %attrs%"). When set, it overrides Mode for
	// handler selection.
	Format string `mapstructure:"format"`

	// TimeFormat is a Go time layout used for the %time% placeholder in a
	// custom format string. Defaults to [time.RFC3339].
	TimeFormat string `mapstructure:"time_format"`

	// LineEnding for log entries. Default: "\n".
	LineEnding string `mapstructure:"line_ending"`

	// SkipLineEnding determines if the logger should skip appending the default
	// line ending to each log entry.
	SkipLineEnding bool `mapstructure:"skip_line_ending"`

	// Encoding sets the logger's encoding. Valid values are "json" and
	// "console".
	Encoding string `mapstructure:"encoding"`

	// Output is a list of URLs or file paths to write logging output to.
	Output []string `mapstructure:"output"`

	// ErrorOutput is a list of URLs to write internal logger errors to. The
	// default is standard error.
	ErrorOutput []string `mapstructure:"err_output"`
}

Config holds the logger configuration for a single channel.

func (*Config) BuildLogger

func (cfg *Config) BuildLogger() (*BuildResult, error)

BuildLogger creates an *slog.Logger from the configuration.

func (*Config) InitDefault

func (cfg *Config) InitDefault()

InitDefault sets default values for empty fields.

type Configurer

type Configurer interface {
	// UnmarshalKey takes a single key and unmarshal it into a Struct.
	UnmarshalKey(name string, out any) error
	// Has checks if a config section exists.
	Has(name string) bool
}

Configurer provides access to the application configuration.

type FormatHandler

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

FormatHandler is an slog.Handler that renders log records according to a user-defined format string containing %placeholder% tokens such as %time%, %level%, %message%, %attrs%, %logger%, %source_file%, %source_line%, and %source_func%.

Unknown placeholders are left in the output verbatim.

func NewFormatHandler

func NewFormatHandler(w io.Writer, opts *FormatHandlerOptions) *FormatHandler

NewFormatHandler returns a FormatHandler that writes to w using the given options. The format string is scanned once to determine which placeholders are present so that expensive operations (source lookup, attr rendering) can be skipped when not needed.

func (*FormatHandler) Enabled

func (h *FormatHandler) Enabled(_ context.Context, level slog.Level) bool

Enabled reports whether the handler is enabled for the given level.

func (*FormatHandler) Handle

func (h *FormatHandler) Handle(_ context.Context, r slog.Record) error

Handle formats and writes a single log record.

func (*FormatHandler) WithAttrs

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

WithAttrs returns a new FormatHandler that includes the given attributes in every subsequent record. The current group prefix is baked into attr keys so that groups added later do not retroactively affect them.

func (*FormatHandler) WithGroup

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

WithGroup returns a new FormatHandler that qualifies subsequent attributes with the given group name using dot-separated keys.

type FormatHandlerOptions

type FormatHandlerOptions struct {
	// Level is the minimum enabled logging level.
	Level slog.Leveler
	// Format is the format string with %placeholder% tokens.
	Format string
	// TimeLayout is the Go time layout used for the %time% placeholder.
	// Defaults to [time.RFC3339] when empty.
	TimeLayout string
	// LineEnding is appended after each formatted record. Defaults to "\n".
	// Set to a non-nil pointer to an empty string to suppress the line ending.
	LineEnding *string
}

FormatHandlerOptions configures a FormatHandler.

type Log

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

Log implements the Logger interface, dispatching named loggers to per-channel configurations when available, falling back to the base logger.

func NewLogger

func NewLogger(channels ChannelConfig, base *slog.Logger) *Log

NewLogger creates a new Log with the given channel overrides and base logger.

func (*Log) Close

func (l *Log) Close() error

Close releases all resources (file handles, file writers) opened by channel-specific loggers created through [NamedLogger].

func (*Log) NamedLogger

func (l *Log) NamedLogger(name string) *slog.Logger

NamedLogger returns a logger for the given name. If a channel-specific configuration exists, a dedicated logger is built from it; otherwise the base logger is returned with a "logger" attribute set to name.

type Logger

type Logger interface {
	NamedLogger(name string) *slog.Logger
}

Logger is the main logger interface to provide a named (per-plugin) logger.

type Mode

type Mode string

Mode represents available logger modes

type Plugin

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

Plugin manages the slog-based logger.

func (*Plugin) Init

func (p *Plugin) Init(cfg Configurer) error

Init logger service.

func (*Plugin) Name

func (p *Plugin) Name() string

Name returns a user-friendly plugin name.

func (*Plugin) Provides

func (p *Plugin) Provides() []*dep.Out

Provides declares the services this plugin exports.

func (*Plugin) Serve

func (p *Plugin) Serve() chan error

Serve starts the plugin (no-op for the logger).

func (*Plugin) ServiceLogger

func (p *Plugin) ServiceLogger() *Log

ServiceLogger returns a logger dedicated to the specific channel.

func (*Plugin) Stop

func (p *Plugin) Stop(context.Context) error

Stop gracefully shuts down the plugin, closing any file handles opened for log output — both root-level and per-channel closers.

type RawHandler

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

RawHandler is an slog.Handler that outputs only the log message followed by a newline. Structured attributes and groups are discarded. This is used for the "raw" logger mode where callers want plain, undecorated output.

func NewRawHandler

func NewRawHandler(w io.Writer, level slog.Leveler) *RawHandler

NewRawHandler returns a RawHandler writing to w. Only records at or above the given level are emitted.

func (*RawHandler) Enabled

func (h *RawHandler) Enabled(_ context.Context, level slog.Level) bool

func (*RawHandler) Handle

func (h *RawHandler) Handle(_ context.Context, r slog.Record) error

func (*RawHandler) WithAttrs

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

WithAttrs returns the same handler — raw mode discards attributes.

func (*RawHandler) WithGroup

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

WithGroup returns the same handler — raw mode discards groups.

Jump to

Keyboard shortcuts

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