log

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2025 License: Apache-2.0 Imports: 9 Imported by: 98

Documentation

Overview

Package log contains a golang.org/x/exp/slog compatible logger.

Index

Constants

View Source
const (
	LevelTrace  slog.Level = slog.LevelDebug - 1
	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
}
View Source
const ComponentType = "logger"

ComponentType is the name of the component type logger.

Variables

View Source
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"
)
View Source
var (
	// ErrNoHandler happens when a the LevelHandler wrapper gets no handler.
	ErrNoHandler = errors.New("no handler defined")
)

Functions

func Debug

func Debug(msg string, args ...any)

Debug calls Logger.Debug on the default logger.

func Error

func Error(msg string, args ...any)

Error calls Logger.Error on the default logger.

func Info

func Info(msg string, args ...any)

Info calls Logger.Info on the default logger.

func Log

func Log(level slog.Level, msg string, args ...any)

Log calls Logger.Log on the default logger.

func LogAttrs

func LogAttrs(level slog.Level, msg string, attrs ...slog.Attr)

LogAttrs calls Logger.LogAttrs on the default logger.

func ParseLevel

func ParseLevel(l string) (slog.Level, error)

ParseLevel parses a string level to an Level.

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.

func Trace

func Trace(msg string, args ...any)

Trace calls Logger.Trace on the default logger.

func Warn

func Warn(msg string, args ...any)

Warn calls Logger.Warn on the default logger.

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"`
}

Config is the loggers config.

func NewConfig

func NewConfig(opts ...Option) Config

NewConfig creates a new config with the defaults and applys opts on top.

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

func NewLevelHandler(level slog.Level, h slog.Handler) (*LevelHandler, error)

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(_ context.Context, 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(context context.Context, 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

type Logger struct {
	*slog.Logger
	// contains filtered or unexported fields
}

Logger is a go-micro logger, it is the slog.Logger, with some added methods to implement the component interface.

func New

func New(opts ...Option) (Logger, error)

New creates a new Logger from a Config.

func NewConfigDatas added in v0.1.0

func NewConfigDatas(sections []string, configs types.ConfigData, opts ...Option) (Logger, error)

NewConfigDatas will create a new logger with the given configs, as well as this logger's fields. TODO(jochumdev): Simplify this.

func Provide added in v0.1.0

func Provide(
	serviceName types.ServiceName,
	configs types.ConfigData,
	opts ...Option,
) (Logger, error)

Provide provides a new logger. It will set the slog.Logger as package wide default logger.

func (Logger) Level added in v0.1.0

func (l Logger) Level() slog.Level

Level returns the level as int.

func (Logger) Start

func (l Logger) Start() error

Start no-op.

func (Logger) Stop

func (l Logger) Stop(ctx context.Context) error

Stop stops all cached plugins if this is the default logger.

func (Logger) String

func (l Logger) String() string

String returns current handler plugin used.

func (Logger) Trace

func (l Logger) Trace(msg string, args ...any)

Trace logs at TraceLevel.

func (Logger) TraceContext added in v0.1.0

func (l Logger) TraceContext(ctx context.Context, msg string, args ...any)

TraceContext logs with context.Context.

func (Logger) Type

func (l Logger) Type() string

Type returns the component type.

func (Logger) With

func (l Logger) With(args ...any) Logger

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, configs types.ConfigData, 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.

func (Logger) WithLevel

func (l Logger) WithLevel(level string) Logger

WithLevel creates a copy of the logger with a new level. It will inherit all the fields and the context from the parent logger.

func (Logger) WithOpts added in v0.1.0

func (l Logger) WithOpts(opts ...Option) (Logger, error)

WithOpts returns a new logger with the given opt's. It adds the fields from the current logger.

type Option

type Option func(ConfigType)

Option is a logger WithXXX Option.

func WithLevel

func WithLevel(n string) Option

WithLevel sets the log level to user. TODO(davincible): would love to take in something like ( slog.Level | string | constraints.Integer) here, but not sure how that would work.

func WithPlugin

func WithPlugin(n string) Option

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 types.ConfigData, 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.

Jump to

Keyboard shortcuts

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