logger

package
v2.0.0-alpha7 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2023 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

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

func Enabled

func Enabled(level slog.Level) bool

func Error

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

func Info

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

func Init

func Init(cfg *Config)

func IsDebug

func IsDebug() bool

func Log

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

func LogAttrs

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

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 Sync

func Sync() error

func Warn

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

func With

func With(args ...any) *slog.Logger

func WithContext

func WithContext(ctx context.Context) *slog.Logger

func WithGroup

func WithGroup(name string) *slog.Logger

Types

type Config

type Config struct {
	Development bool `mapstructure:"development"`

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

	// File logger options
	FileLogger *FileConfig `mapstructure:"file_logger_options"`

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

func (*Config) Init

func (cfg *Config) Init()

type ConsoleHandler

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

func NewConsoleHandler

func NewConsoleHandler(w io.Writer, level slog.Level, source bool, attrs ...slog.Attr) *ConsoleHandler

func (*ConsoleHandler) Enabled

func (h *ConsoleHandler) Enabled(level slog.Level) bool

func (*ConsoleHandler) Handle

func (h *ConsoleHandler) Handle(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 FileConfig

type FileConfig struct {
	// Filename is the file to write logs to.  Backup log files will be retained
	// in the same directory.  It uses <processname>-lumberjack.log in
	// os.TempDir() if empty.
	LogOutput string `mapstructure:"log_output"`

	// MaxSize is the maximum size in megabytes of the log file before it gets
	// rotated. It defaults to 100 megabytes.
	MaxSize int `mapstructure:"max_size"`

	// MaxAge is the maximum number of days to retain old log files based on the
	// timestamp encoded in their filename.  Note that a day is defined as 24
	// hours and may not exactly correspond to calendar days due to daylight
	// savings, leap seconds, etc. The default is not to remove old log files
	// based on age.
	MaxAge int `mapstructure:"max_age"`

	// MaxBackups is the maximum number of old log files to retain.  The default
	// is to retain all old log files (though MaxAge may still cause them to get
	// deleted.)
	MaxBackups int `mapstructure:"max_backups"`

	// Compress determines if the rotated log files should be compressed
	// using gzip. The default is not to perform compression.
	Compress bool `mapstructure:"compress"`
}

func (*FileConfig) Init

func (fl *FileConfig) Init()

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