log

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2023 License: Apache-2.0 Imports: 8 Imported by: 44

Documentation

Overview

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

Index

Constants

View Source
const (
	DefaultLevel      = InfoLevel
	DefaultPlugin     = "textstderr"
	DefaultSetDefault = true
)

DefaultPlugin is the default plugin to use.

View Source
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.

View Source
const ComponentType = "logger"

ComponentType is the name of the component type logger.

Variables

View Source
var DefaultConfigSection = "logger" //nolint:gochecknoglobals

DefaultConfigSection is the section key used in config files used to configure the logger options.

View Source
var (
	// ErrNoHandler happens when a the LevelHandler wrapper gets no handler.
	ErrNoHandler = errors.New("no handler defined")
)
View Source
var Plugins = container.NewPlugins[func(level slog.Leveler) (slog.Handler, error)]() //nolint:gochecknoglobals

Plugins is the registry for Logger plugins.

Functions

func Debug

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

Debug calls Logger.Debug on the default logger.

func Error

func Error(msg string, err error, 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 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 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.

func NewConfig

func NewConfig() Config

NewConfig creates a new config with the defaults.

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(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

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(cfg Config) (Logger, error)

New creates a new Logger from a Config.

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

func (l Logger) Plugin(plugin string, level ...slog.Leveler) (Logger, error)

Plugin will return the plugin handler with set to TRACE level. To enable a custom level wrap it with a LevelHandler.

func (Logger) Start

func (l Logger) Start() error

Start no-op.

func (Logger) Stop

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

Stop no-op.

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) 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) WithComponent

func (l Logger) WithComponent(component, name, plugin string, level slog.Leveler) (Logger, error)

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

func (l Logger) WithContext(ctx context.Context) Logger

WithContext returns a new Logger with the same handler as the receiver and the given context.

func (Logger) WithLevel

func (l Logger) WithLevel(level slog.Leveler) 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.

type Option

type Option func(*Config)

Option is a logger WithXXX Option.

func WithLevel

func WithLevel(level slog.Level) Option

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

func WithPlugin(plugin 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(setDefault bool) Option

WithSetDefault dictates whether or not to call slog.SetDefault.

Jump to

Keyboard shortcuts

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