log

package
v0.0.0-...-134f61d Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2025 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package log implements a common logger based on slog.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetGlobalLogger

func GetGlobalLogger() *slog.Logger

GetGlobalLogger returns the global logger. If the global logger is not set, initialize the global logger based on a default config and return it.

func NewLogger

func NewLogger(pkgName string) *slog.Logger

NewLogger initializes a new logger with the given package name.

func SetGlobalConfig

func SetGlobalConfig(cfg *Config) (cleanup func(), err error)

SetGlobalConfig sets a global config that will be used every time a new logger is initialized, and returns a cleanup hook that cleans resources used by loggers.

Note that calling this function will also sets a global logger based on the given config.

func WithOTelAttrs

func WithOTelAttrs(logger *slog.Logger, attrs ...attribute.KeyValue) *slog.Logger

WithOTelAttrs returns a new logger with OpenTelemetry attributes.

Types

type Config

type Config struct {
	// Type is the type of logger. Currently support "light", "local" and "otel".
	// The "light" logger outputs logs to stderr, the "local" logger outputs logs to stderr and a local file, and the
	// "otel" logger outputs logs to the global open telemetry logger provider.
	Type string `json:"type" yaml:"type" toml:"type" xml:"type" env:"LOG_TYPE" default:"light"`

	// Level is the log level. Possible values are "debug", "info", "warn" and "error".
	// Note that this config option doesn't effect "otel" logger.
	Level string `json:"level" yaml:"level" toml:"level" xml:"level" env:"LOG_LEVEL" default:"debug"`

	// Local is the local log config.
	Local LocalConfig `json:"local" yaml:"local" toml:"local" xml:"local"`
}

Config defines the log config model.

type LocalConfig

type LocalConfig struct {
	// Path is the file to write logs to. Backup log files will be retained in the same directory.
	Path string `json:"path" yaml:"path" toml:"path" xml:"path" env:"LOG_LOCAL_PATH" default:"/tmp/test/log"`

	// MaxSizeMB is the maximum size in megabytes of the log file before it gets rotated.
	MaxSizeMB int `json:"max_size_mb" yaml:"max_size_mb" toml:"max_size_mb" xml:"max_size_mb" env:"LOG_LOCAL_MAX_SIZE_MB" default:"100"` // nolint:lll

	// MaxBackups is the maximum number of old log files to retain.
	MaxBackups int `json:"max_backups" yaml:"max_backups" toml:"max_backups" xml:"max_backups" env:"LOG_LOCAL_MAX_BACKUPS" default:"3"` // nolint:lll
}

LocalConfig defines the local log config.

Jump to

Keyboard shortcuts

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