Log messages are lower-case event names with their details as key/value
attributes ("error" for errors), in place of formatted strings.
Loggable package take a WithLogger option as part of their "New*" call,
defaulting to slog.Default(). Each tags its lines with its `component`.
Some debug loggers print directly rather than using slog when in
terminal logging mode.
41 lines
859 B
Go
41 lines
859 B
Go
package mock
|
|
|
|
import (
|
|
"log/slog"
|
|
)
|
|
|
|
// Option configures a Store, Generator or handler.
|
|
type Option func(*options)
|
|
|
|
// options holds the settings Options change.
|
|
type options struct {
|
|
log *slog.Logger
|
|
}
|
|
|
|
// WithLogger makes a Store, Generator or handler log to logger instead of
|
|
// slog.Default().
|
|
func WithLogger(logger *slog.Logger) Option {
|
|
return func(opts *options) {
|
|
opts.log = logger
|
|
}
|
|
}
|
|
|
|
// applyOptions returns the settings opts make.
|
|
func applyOptions(opts []Option) options {
|
|
var settings options
|
|
for _, opt := range opts {
|
|
opt(&settings)
|
|
}
|
|
return settings
|
|
}
|
|
|
|
// logger returns the logger to use, tagged with component: the given one, or
|
|
// slog.Default(), looked up now.
|
|
func (opts options) logger(component string) *slog.Logger {
|
|
log := opts.log
|
|
if log == nil {
|
|
log = slog.Default()
|
|
}
|
|
return log.With("component", component)
|
|
}
|