Documentation
¶
Index ¶
- func AttrRedactor(key, val string) func(AttrReplacer) AttrReplacer
- func New(w io.Writer, c Config, p PluginOptions) (*slog.Logger, error)
- func TracerHandler(h slog.Handler) slog.Handler
- func WithTraceContext(ctx context.Context, trace TraceContext) context.Context
- type AttrReplacer
- type Config
- type Plugin
- type PluginOptions
- type TraceContext
- type Tracer
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 ¶
New creates a new logger with given configuration and plugins. It returns an error if the log format is not supported.
func TracerHandler ¶
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 ¶
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.
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.
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.