xslog

package
v0.0.0-...-b03ede8 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2025 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AttrRedactor

func AttrRedactor(key, val string) func(AttrReplacer) AttrReplacer

AttrRedactor returns a plugin function that replaces the attribute value with the given value.

func New

func New(w io.Writer, c Config, p PluginOptions) (*slog.Logger, error)

New creates a new logger with given configuration and plugins. It returns an error if the log format is not supported.

func TracerHandler

func TracerHandler(h slog.Handler) slog.Handler

TracerHandler returns a plugin function that adds the trace information to the log record.

func WithTraceContext

func WithTraceContext(ctx context.Context, trace TraceContext) context.Context

WithTraceContext adds the TraceContext to the context.

Types

type AttrReplacer

type AttrReplacer func([]string, slog.Attr) slog.Attr

AttrReplacer is a function type that replaces the attributes in the log record. See the slog.HandlerOptions.ReplaceAttr for more details.

type Config

type Config struct {
	// Level is the minimum log level to output.
	// Possible values are: debug, info, warn, error. Default is info.
	Level slog.Level `env:"XSLOG_LEVEL" envDefault:"info"`

	// Format is the log format. Possible values are: json, text. Default is text.
	Format string `env:"XSLOG_FORMAT" envDefault:"text"`

	// SourceEnabled enables the source code location in the log output. Default is false.
	SourceEnabled bool `env:"XSLOG_SOURCE_ENABLED" envDefault:"false"`

	// ShortSourceEnabled enables short source code location in the log output. Default is false.
	// Only works if SourceEnabled is also enabled.
	ShortSourceEnabled bool `env:"XSLOG_SHORT_SOURCE_ENABLED" envDefault:"false"`
}

Config is used to configure the logger behavior. Zero value are equivalent to the default configuration.

func (*Config) Init

func (c *Config) Init() error

Init initializes the configuration from the environment variables.

type Plugin

type Plugin[T any] func(T) T

Plugin is a function type that adds additional functionality to the logger. The type T is any type, but it should be a slog.Handler or AttrReplacer.

func Stack

func Stack[T any](ps ...Plugin[T]) Plugin[T]

Stack composes multiple plugins into a single plugin.

type PluginOptions

type PluginOptions struct {
	// Handlers plugin for the slog.Handler. If multiple plugins are needed, they can be composed using the
	// Stack function.
	Handler Plugin[slog.Handler]

	// AttrReplacer plugin for the attribute replacer. If multiple plugins are needed, they can be composed using the
	// Stack function.
	AttrReplacer Plugin[AttrReplacer]
}

PluginOptions adds additional functionality to the logger, such as adding trace ID or redacting sensitive data from the log record. Zero value is a valid plugin that does nothing.

type TraceContext

type TraceContext struct {
	// RequestID is the unique request ID value.
	RequestID string

	// CorrelationID is the unique correlation ID value.
	CorrelationID string

	// RequestIDAttrKey is the attribute key is used to display the Request ID in the log record.
	// If empty, the `request_id` ID will not be displayed in the log record.
	RequestIDAttrKey string

	// CorrelationIDAttrKey is the attribute key is used to display the Correlation ID in the log record.
	// If empty, the `correlation_id` ID will not be displayed in the log record.
	CorrelationIDAttrKey string
}

TraceContext describes the trace information.

type Tracer

type Tracer struct{ slog.Handler }

Tracer is a plugin that adds the trace information to the log record.

func (*Tracer) Handle

func (t *Tracer) Handle(ctx context.Context, rec slog.Record) error

Jump to

Keyboard shortcuts

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