logger

package
v2.0.0-beta4 Latest Latest
Warning

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

Go to latest
Published: May 12, 2023 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterSink

func RegisterSink(scheme string, factory func(*url.URL) (Sink, error)) error

RegisterSink registers a user-supplied factory for all sinks with a particular scheme.

All schemes must be ASCII, valid under section 0.1 of RFC 3986 (https://tools.ietf.org/html/rfc3983#section-3.1), and must not already have a factory registered. Zap automatically registers a factory for the "file" scheme.

func ToAttrs

func ToAttrs(data map[string]any) []slog.Attr

func ToLeveler

func ToLeveler(level string) slog.Leveler

Types

type ChannelConfig

type ChannelConfig struct {
	// Dedicated channels per logger. By default logger allocated via named logger.
	Channels map[string]*Config `mapstructure:"channels"`
}

ChannelConfig configures loggers per channel.

type Config

type Config struct {
	// When AddSource is true, the handler adds a ("source", "file:line")
	// attribute to the output indicating the source code position of the log
	// statement. AddSource is false by default to skip the cost of computing
	// this information.
	AddSource bool `mapstructure:"add_source"`

	// Level is the minimum enabled logging level.
	Level string `mapstructure:"level"`

	// Encoding sets the logger's encoding. Init values are "json", "text" and "console"
	Encoding string `mapstructure:"encoding"`

	// Output is a list of URLs or file paths to write logging output to.
	// See Open for details.
	OutputPaths []string `mapstructure:"output_paths"`

	Attrs map[string]any `mapstructure:"attributes"`
}

func (*Config) Logger

func (cfg *Config) Logger() (*slog.Logger, error)

func (*Config) OpenSinks

func (cfg *Config) OpenSinks() (WriteSyncer, error)

func (*Config) Opts

func (cfg *Config) Opts() HandlerOptions

type ConsoleHandler

type ConsoleHandler struct {
	// contains filtered or unexported fields
}

func NewConsoleHandler

func NewConsoleHandler(opts slog.HandlerOptions, w io.Writer, attrs ...slog.Attr) *ConsoleHandler

func (*ConsoleHandler) Enabled

func (h *ConsoleHandler) Enabled(_ context.Context, l slog.Level) bool

func (*ConsoleHandler) Handle

func (h *ConsoleHandler) Handle(_ context.Context, r slog.Record) error

func (*ConsoleHandler) WithAttrs

func (h *ConsoleHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*ConsoleHandler) WithGroup

func (h *ConsoleHandler) WithGroup(name string) slog.Handler

type HandlerOptions

type HandlerOptions struct {
	slog.HandlerOptions
}

func (HandlerOptions) NewConsoleHandler

func (opts HandlerOptions) NewConsoleHandler(w io.Writer) *ConsoleHandler

func (HandlerOptions) NewHandler

func (opts HandlerOptions) NewHandler(w io.Writer, encoding string) slog.Handler

type HandlerSyncer

type HandlerSyncer interface {
	Sync() error
}

type Log

type Log struct {
	// contains filtered or unexported fields
}

func NewLogger

func NewLogger(channels ChannelConfig, base *slog.Logger) *Log

func (*Log) NamedLogger

func (l *Log) NamedLogger(name string) *slog.Logger

type Logger

type Logger interface {
	NamedLogger(name string) *slog.Logger
}

type Sink

type Sink interface {
	WriteSyncer
	io.Closer
}

type WriteSyncer

type WriteSyncer interface {
	io.Writer
	Sync() error
}

func AddSync

func AddSync(w io.Writer) WriteSyncer

func CombineWriteSyncers

func CombineWriteSyncers(writers ...WriteSyncer) WriteSyncer

CombineWriteSyncers is a utility that combines multiple WriteSyncers into a single, locked WriteSyncer. If no inputs are supplied, it returns a no-op WriteSyncer.

It's provided purely as a convenience; the result is no different from using zapcore.NewMultiWriteSyncer and zapcore.Lock individually.

func Lock

func Lock(ws WriteSyncer) WriteSyncer

Lock wraps a WriteSyncer in a mutex to make it safe for concurrent use. In particular, *os.Files must be locked before use.

func NewMultiWriteSyncer

func NewMultiWriteSyncer(ws ...WriteSyncer) WriteSyncer

func Open

func Open(paths ...string) (WriteSyncer, func(), error)

Open is a high-level wrapper that takes a variadic number of URLs, opens or creates each of the specified resources, and combines them into a locked WriteSyncer. It also returns any error encountered and a function to close any opened files.

Passing no URLs returns a no-op WriteSyncer. Zap handles URLs without a scheme and URLs with the "file" scheme. Third-party code may register factories for other schemes using RegisterSink.

URLs with the "file" scheme must use absolute paths on the local filesystem. No user, password, port, fragments, or query parameters are allowed, and the hostname must be empty or "localhost".

Since it's common to write logs to the local filesystem, URLs without a scheme (e.g., "/var/log/foo.log") are treated as local file paths. Without a scheme, the special paths "stdout" and "stderr" are interpreted as os.Stdout and os.Stderr. When specified without a scheme, relative file paths also work.

Jump to

Keyboard shortcuts

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