logging

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2025 License: MIT Imports: 12 Imported by: 12

Documentation

Index

Constants

View Source
const (
	CONSOLE = "console"
	JOURNAL = "systemd-journald"
)

Variables

This section is empty.

Functions

func AssertOutput

func AssertOutput(o string) error

AssertOutput returns an error if output is not a valid logger output.

func Error added in v0.7.0

func Error(e error) zap.Field

Error returns a zap.Field for logging the provided error. This function checks if the error includes a stack trace from the pkg/errors library. If a stack trace is present, it is suppressed in the log output because logging a stack trace is not necessary. Otherwise, the error is logged normally.

func NewJournaldCore

func NewJournaldCore(identifier string, enab zapcore.LevelEnabler) zapcore.Core

NewJournaldCore returns a zapcore.Core that sends log entries to systemd-journald and uses the given identifier as a prefix for structured logging context that is sent as journal fields.

Types

type Config

type Config struct {
	// zapcore.Level at 0 is for info level.
	Level  zapcore.Level `yaml:"level" env:"LEVEL" default:"0"`
	Output string        `yaml:"output" env:"OUTPUT"`
	// Interval for periodic logging.
	Interval time.Duration `yaml:"interval" env:"INTERVAL" default:"20s"`
	Options  Options       `yaml:"options" env:"OPTIONS"`
}

Config defines Logger configuration.

func (*Config) SetDefaults added in v0.4.0

func (c *Config) SetDefaults()

SetDefaults implements defaults.Setter to configure the log output if it is not set: systemd-journald is used when Icinga DB is running under systemd, otherwise stderr.

func (*Config) Validate

func (c *Config) Validate() error

Validate checks constraints in the configuration and returns an error if they are violated.

type CoreFactory added in v0.8.0

type CoreFactory func(zap.AtomicLevel) zapcore.Core

CoreFactory is a function type that creates a zapcore.Core based on the provided zap.AtomicLevel.

type Logger

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

Logger wraps zap.SugaredLogger and allows to get the interval for periodic logging.

func NewLogger

func NewLogger(base *zap.SugaredLogger, interval time.Duration) *Logger

NewLogger returns a new Logger.

func (*Logger) Interval

func (l *Logger) Interval() time.Duration

Interval returns the interval for periodic logging.

type Logging

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

Logging implements access to a default logger and named child loggers. Log levels can be configured per named child via Options which, if not configured, fall back on a default log level. Logs either to the console or to systemd-journald.

func NewLogging

func NewLogging(name string, level zapcore.Level, output string, options Options, interval time.Duration) (*Logging, error)

NewLogging takes the name and log level for the default logger, output where log messages are written to, options having log levels for named child loggers and returns a new Logging.

func NewLoggingFromConfig

func NewLoggingFromConfig(name string, c Config) (*Logging, error)

NewLoggingFromConfig returns a new Logging from Config.

func NewLoggingWithFactory added in v0.8.0

func NewLoggingWithFactory(name string, level zapcore.Level, interval time.Duration, f CoreFactory) *Logging

NewLoggingWithFactory constructs a new Logging with the provided CoreFactory.

This is useful if you want to customize the zapcore.Core creation, e.g. for testing purposes. Note that the returned Logging instance will not have any options set, i.e. all child loggers will inherit the default log level. If you need options, set them manually after obtaining the Logging instance.

Example usage:

logs := logging.NewLoggingWithFactory("testing", zapcore.DebugLevel, time.Second, func(level zap.AtomicLevel) zapcore.Core {
    return zaptest.NewLogger(t, zaptest.Level(level.Level())).Core()
})

childLogger := logs.GetChildLogger("my-component")

The above example creates a Logging instance that logs to the test logger provided by the zaptest package. All loggers created from this Logging instance will log to the same test logger, which will only be visible in the test output when the test fails or when the test is run with the -v flag.

func (*Logging) ForceLog added in v0.6.3

func (l *Logging) ForceLog() zap.Option

ForceLog results in every message being logged.

This zap.Option is the opposite of zap.IncreaseLevel, it just decreases the log level to debug. Since zap's architecture does not allow this with the same zapcore.Core, it replaces the core with a freshly created one from the Logging's core factory.

func (*Logging) GetChildLogger

func (l *Logging) GetChildLogger(name string) *Logger

GetChildLogger returns a named child logger. Log levels for named child loggers are obtained from the logging options and, if not found, set to the default log level.

func (*Logging) GetLogger

func (l *Logging) GetLogger() *Logger

GetLogger returns the default logger.

type Options

type Options map[string]zapcore.Level

Options define child loggers with their desired log level.

func (*Options) UnmarshalText added in v0.4.0

func (o *Options) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler to allow Options to be parsed by env.

This custom TextUnmarshaler is necessary as - for the moment - env does not support map[T]encoding.TextUnmarshaler. After <https://github.com/caarlos0/env/pull/323> got merged and a new env release was drafted, this method can be removed.

func (*Options) UnmarshalYAML added in v0.4.0

func (o *Options) UnmarshalYAML(unmarshal func(any) error) error

UnmarshalYAML implements yaml.InterfaceUnmarshaler to allow Options to be parsed go-yaml.

Jump to

Keyboard shortcuts

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