Documentation ¶
Overview ¶
Package log contains a golang.org/x/exp/slog compatible logger.
Index ¶
- Constants
- Variables
- func Debug(msg string, args ...any)
- func Error(msg string, err error, args ...any)
- func Info(msg string, args ...any)
- func Log(level slog.Level, msg string, args ...any)
- func LogAttrs(level slog.Level, msg string, attrs ...slog.Attr)
- func ParseLevel(l string) (slog.Level, error)
- func Trace(msg string, args ...any)
- func Warn(msg string, args ...any)
- type Config
- type LevelHandler
- type Logger
- func (l Logger) Plugin(plugin string, level ...slog.Leveler) (Logger, error)
- func (l Logger) Start() error
- func (l Logger) Stop(_ context.Context) error
- func (l Logger) String() string
- func (l *Logger) Trace(msg string, args ...any)
- func (l Logger) Type() string
- func (l *Logger) With(args ...any) Logger
- func (l Logger) WithComponent(component, name, plugin string, level slog.Leveler) (Logger, error)
- func (l Logger) WithContext(ctx context.Context) Logger
- func (l Logger) WithLevel(level slog.Leveler) Logger
- type Option
Constants ¶
const ( DefaultLevel = InfoLevel DefaultPlugin = "textstderr" DefaultSetDefault = true )
DefaultPlugin is the default plugin to use.
const ( TraceLevel slog.Level = slog.DebugLevel - 1 DebugLevel slog.Level = slog.DebugLevel InfoLevel slog.Level = slog.InfoLevel WarnLevel slog.Level = slog.WarnLevel ErrorLevel slog.Level = slog.ErrorLevel )
Names for common Levels.
const ComponentType = "logger"
ComponentType is the name of the component type logger.
Variables ¶
var DefaultConfigSection = "logger" //nolint:gochecknoglobals
DefaultConfigSection is the section key used in config files used to configure the logger options.
var ( // ErrNoHandler happens when a the LevelHandler wrapper gets no handler. ErrNoHandler = errors.New("no handler defined") )
var Plugins = container.NewPlugins[func(level slog.Leveler) (slog.Handler, error)]() //nolint:gochecknoglobals
Plugins is the registry for Logger plugins.
Functions ¶
func ParseLevel ¶
ParseLevel parses a string level to an Level.
Types ¶
type Config ¶
type Config struct { // Plugin sets the log handler plugin to use. // Make sure to register the plugin by importing it. Plugin string `json:"plugin,omitempty" yaml:"plugin,omitempty"` // Level sets the log level to use. Level slog.Level `json:"level,omitempty" yaml:"level,omitempty"` // SetDefault dictates whether to call slog.SetDefault on the newly created logger. SetDefault bool `json:"setDefault" yaml:"setDefault"` }
Config is the loggers config.
type LevelHandler ¶
type LevelHandler struct {
// contains filtered or unexported fields
}
LevelHandler is wrapper for slog.Handler which does Leveling.
func NewLevelHandler ¶
NewLevelHandler implements slog.Handler interface. It is used to wrap a handler with a new log level. As log level cannot be modified within a handler through the interface of slog, you can use this to wrap a handler with a new log level.
func (*LevelHandler) Enabled ¶
func (h *LevelHandler) Enabled(level slog.Level) bool
Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower. Enabled is called early, before any arguments are processed, to save effort if the log event should be discarded.
func (*LevelHandler) Handle ¶
func (h *LevelHandler) Handle(r slog.Record) error
Handle handles the Record. It will only be called if Enabled returns true. Handle methods that produce output should observe the following rules:
- If r.Time is the zero time, ignore the time.
- If an Attr's key is the empty string, ignore the Attr.
func (*LevelHandler) WithAttrs ¶
func (h *LevelHandler) WithAttrs(attrs []slog.Attr) slog.Handler
WithAttrs returns a new Handler whose attributes consist of both the receiver's attributes and the arguments. The Handler owns the slice: it may retain, modify or discard it.
func (*LevelHandler) WithGroup ¶
func (h *LevelHandler) WithGroup(name string) slog.Handler
WithGroup returns a new Handler with the given group appended to the receiver's existing groups. The keys of all subsequent attributes, whether added by With or in a Record, should be qualified by the sequence of group names.
type Logger ¶
Logger is a go-micro logger, it is the slog.Logger, with some added methods to implement the component interface.
func ProvideLogger ¶
func ProvideLogger( serviceName types.ServiceName, data types.ConfigData, opts ...Option, ) (Logger, error)
ProvideLogger provides a new logger. It will set the slog.Logger as package wide default logger.
func (Logger) Plugin ¶
Plugin will return the plugin handler with set to TRACE level. To enable a custom level wrap it with a LevelHandler.
func (*Logger) With ¶
With returns a new Logger that includes the given arguments, converted to Attrs as in [Logger.Log]. The Attrs will be added to each output from the Logger.
The new Logger's handler is the result of calling WithAttrs on the receiver's handler.
func (Logger) WithComponent ¶
WithComponent will create a new logger for a component inheriting all parent logger fields, and optionally set a new level and handler.
If you want to use the parent handler and log level, pass an empty values.
It will add two fields to the sub logger, the component (e.g. broker) as component and the component plugin implementation (e.g. NATS) as plugin.
func (Logger) WithContext ¶
WithContext returns a new Logger with the same handler as the receiver and the given context.
type Option ¶
type Option func(*Config)
Option is a logger WithXXX Option.
func WithLevel ¶
WithLevel sets the log level to user. TODO: would love to take in something like ( slog.Level | string | constraints.Integer) here, but not sure how that would work.
func WithPlugin ¶
WithPlugin sets the logger plugin to be used. A logger plugin is the underlying handler the logger will use to process log events. To add your custom handler, register it as a plugin. See log/plugin.go for more details on how to do so.
func WithSetDefault ¶
WithSetDefault dictates whether or not to call slog.SetDefault.