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, 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 Register(name string, pFunc ProviderFunc) bool
- func Trace(msg string, args ...any)
- func Warn(msg string, args ...any)
- type Config
- type ConfigType
- type LevelHandler
- type Logger
- func New(opts ...Option) (Logger, error)
- func NewConfigDatas(sections []string, configs map[string]any, opts ...Option) (Logger, error)
- func Provide(svcCtx *cli.ServiceContextWithConfig, components *types.Components, ...) (Logger, error)
- func ProvideNoOpts(svcCtx *cli.ServiceContextWithConfig, components *types.Components) (Logger, error)
- func ProvideWithServiceNameField(svcCtx *cli.ServiceContextWithConfig, components *types.Components) (Logger, error)
- func (l Logger) Level() slog.Level
- func (l Logger) Start(_ context.Context) error
- func (l Logger) Stop(ctx context.Context) error
- func (l Logger) String() string
- func (l Logger) Trace(msg string, args ...any)
- func (l Logger) TraceContext(ctx context.Context, msg string, args ...any)
- func (l Logger) Type() string
- func (l Logger) With(args ...any) Logger
- func (l Logger) WithConfig(sections []string, configData map[string]any, opts ...Option) (Logger, error)
- func (l Logger) WithLevel(level string) Logger
- func (l Logger) WithOpts(opts ...Option) (Logger, error)
- type Option
- type Provider
- type ProviderFunc
- type ProviderType
Constants ¶
const ( // LevelTrace must be added, because [slog] package does not have one by default. // Generate it by subtracting 4 levels from [slog.Debug] following the example of // [slog.LevelWarn] and [slog.LevelError] which are set to 4 and 8. LevelTrace slog.Level = slog.LevelDebug - 4 LevelDebug slog.Level = slog.LevelDebug LevelInfo slog.Level = slog.LevelInfo LevelWarn slog.Level = slog.LevelWarn LevelNotice slog.Level = slog.LevelWarn - 2 LevelError slog.Level = slog.LevelError LevelFatal slog.Level = slog.LevelError + 4 )
Names for common Levels. TODO(jochumdev): Something like this would be nice
type LevelT interface {
slog.Level | string | constraints.Integer
}
const ComponentType = "logger"
ComponentType is the name of the component type logger.
Variables ¶
var ( // DefaultLevel is info. DefaultLevel = LevelInfo.String() // DefaultPlugin is "slog", it support's json and text output to Stdout/Stderr and file. DefaultPlugin = "slog" // DefaultSetDefault set's the "log" and slog default logger when true. DefaultSetDefault = false // DefaultConfigSection is the section key used in config files used to // configure the logger options. DefaultConfigSection = "logger" )
var ( // ErrNoHandler happens when a the LevelHandler wrapper gets no handler. ErrNoHandler = errors.New("no handler defined") )
Functions ¶
func Register ¶ added in v0.1.0
func Register(name string, pFunc ProviderFunc) bool
Register makes a plugin available by the provided name. If Register is called twice with the same name, it panics.
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 string `json:"level,omitempty" yaml:"level,omitempty"`
// SetDefault indicates if this logger should be set as default logger.
SetDefault bool `json:"setDefault,omitempty" yaml:"setDefault,omitempty"`
// Fields are additional fields to add to the log.
Fields map[string]any `json:"fields,omitempty" yaml:"fields,omitempty"`
}
Config is the loggers config.
type ConfigType ¶ added in v0.1.0
type ConfigType interface {
// contains filtered or unexported methods
}
ConfigType is a wrapper for config, so we can pass it back to the this plugin handler.
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 ¶
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 ¶
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 NewConfigDatas ¶ added in v0.1.0
NewConfigDatas will create a new logger with the given configs, as well as the given fields.
func Provide ¶ added in v0.1.0
func Provide( svcCtx *cli.ServiceContextWithConfig, components *types.Components, opts ...Option, ) (Logger, error)
Provide provides a new logger. It will set the slog.Logger as package wide default logger.
func ProvideNoOpts ¶ added in v0.3.0
func ProvideNoOpts( svcCtx *cli.ServiceContextWithConfig, components *types.Components, ) (Logger, error)
ProvideNoOpts provides a new logger without options.
func ProvideWithServiceNameField ¶ added in v0.3.0
func ProvideWithServiceNameField( svcCtx *cli.ServiceContextWithConfig, components *types.Components, ) (Logger, error)
ProvideWithServiceNameField provides a new logger with the service name field.
func (Logger) TraceContext ¶ added in v0.1.0
TraceContext logs with context.Context.
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) WithConfig ¶ added in v0.1.0
func (l Logger) WithConfig(sections []string, configData map[string]any, opts ...Option) (Logger, error)
WithConfig returns a new logger if there's a config for it in configs else the current one. It adds the fields from the current logger.
type Option ¶
type Option func(ConfigType)
Option is a logger WithXXX Option.
func WithFields ¶ added in v0.3.0
WithFields adds additional fields to the log.
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 ¶
func WithSetDefault() Option
WithSetDefault makes the resulting logger the default logger. TODO(jochumdev): Remove this? SetDefault also stops all Plugins.
type Provider ¶ added in v0.1.0
type Provider interface {
// Key must return a unique key for the cache,
// this should be unique for this Provider with its config.
Key() string
Start() error
Stop(ctx context.Context) error
Handler() (slog.Handler, error)
}
Provider can be started/stopped and it returns a slog.Handler on request. Providers must be cacheable - there will be always one Provider for a given type AND config.
type ProviderFunc ¶ added in v0.1.0
type ProviderFunc func(section []string, configs map[string]any, opts ...Option) (ProviderType, error)
ProviderFunc is the function a Plugin must provide which returns a Provider encapsulated into a ProviderType.
type ProviderType ¶ added in v0.1.0
type ProviderType struct {
Provider
}
ProviderType is the struct that wraps the interface, so we don't have to pass interfaces everywhere.