logging

package
v1.18.1 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2025 License: Apache-2.0 Imports: 18 Imported by: 1,080

Documentation

Index

Constants

View Source
const (
	Syslog    = "syslog"
	LevelOpt  = "level"
	FormatOpt = "format"
	WriterOpt = "writer"

	StdOutOpt = "stdout"
	StdErrOpt = "stderr"

	LogFormatText          LogFormat = "text"
	LogFormatTextTimestamp LogFormat = "text-ts"
	LogFormatJSON          LogFormat = "json"
	LogFormatJSONTimestamp LogFormat = "json-ts"

	// DefaultLogFormatTimestamp is the string representation of the default log
	// format including timestamps.
	// We don't use this for general runtime logs since kubernetes log capture handles those.
	// This is only used for applications such as CNI which is written to disk so we have no
	// way to correlate with other logs.
	DefaultLogFormatTimestamp LogFormat = LogFormatTextTimestamp

	// DefaultLogLevel is the default log level we want to use for our logs.
	DefaultLogLevel = slog.LevelInfo
)
View Source
const (
	SLevel    = "syslog.level"
	SNetwork  = "syslog.network"
	SAddress  = "syslog.address"
	SSeverity = "syslog.severity"
	SFacility = "syslog.facility"
	STag      = "syslog.tag"
)

Variables

View Source
var (
	LevelPanic = slog.LevelError + 8
	LevelFatal = LevelPanic + 2
)
View Source
var DefaultSlogLogger = slog.New(defaultMultiSlogHandler)

Default slog logger. Will be overwritten once initializeSlog is called.

Functions

func AddHandlers added in v1.18.0

func AddHandlers(hooks ...slog.Handler)

AddHandlers adds additional logrus hook to default logger

func Fatal added in v1.18.0

func Fatal(logger FieldLogger, msg string, args ...any)

func GetSlogLevel added in v1.18.0

func GetSlogLevel(logger FieldLogger) slog.Level

GetSlogLevel returns the log level of the given sloger.

func NewMultiSlogHandler added in v1.18.0

func NewMultiSlogHandler(handler slog.Handler) *multiSlogHandler

NewMultiSlogHandler creates a slog.Handler that supports multiple underlying handlers, such as to output text and json.

func Panic added in v1.18.0

func Panic(logger FieldLogger, msg string, args ...any)

func ParseLevel added in v1.18.0

func ParseLevel(lvl string) (slog.Level, error)

ParseLevel takes a string level and returns the slog log level constant.

func RegisterExitHandler added in v1.18.0

func RegisterExitHandler(handler func())

func ReplaceAttrFn added in v1.18.0

func ReplaceAttrFn(groups []string, a slog.Attr) slog.Attr

func ReplaceAttrFnWithoutTimestamp added in v1.18.0

func ReplaceAttrFnWithoutTimestamp(groups []string, a slog.Attr) slog.Attr

func SetDefaultLogLevel

func SetDefaultLogLevel()

SetDefaultLogLevel updates the DefaultLogger with the DefaultLogLevel

func SetLogLevel

func SetLogLevel(logLevel slog.Level)

SetLogLevel updates the DefaultLogger with a new slog.Level

func SetLogLevelToDebug

func SetLogLevelToDebug()

SetLogLevelToDebug updates the DefaultLogger with the logrus.DebugLevel

func SetSlogLevel added in v1.18.0

func SetSlogLevel(logLevel slog.Level)

SetSlogLevel updates the DefaultSlogLogger with a new slog.Level

func SetupLogging

func SetupLogging(loggers []string, logOpts LogOptions, tag string, debug bool) error

SetupLogging sets up each logging service provided in loggers and configures each logger with the provided logOpts.

Types

type FieldLogger added in v1.18.0

type FieldLogger interface {
	Handler() slog.Handler
	With(args ...any) *slog.Logger
	WithGroup(name string) *slog.Logger
	Enabled(ctx context.Context, level slog.Level) bool
	Log(ctx context.Context, level slog.Level, msg string, args ...any)
	LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)
	Debug(msg string, args ...any)
	DebugContext(ctx context.Context, msg string, args ...any)
	Info(msg string, args ...any)
	InfoContext(ctx context.Context, msg string, args ...any)
	Warn(msg string, args ...any)
	WarnContext(ctx context.Context, msg string, args ...any)
	Error(msg string, args ...any)
	ErrorContext(ctx context.Context, msg string, args ...any)
}

type Limiter

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

Limiter is a wrapper around rate.Limiter that does not panic when the limiter is uninitialized. The wrapping also allows more logging specific functionality to be added later without changing all the call sites.

func NewLimiter

func NewLimiter(interval time.Duration, burst int) Limiter

NewLimiter returns a new Limiter allowing log messages to be emitted on average once every 'interval' and upto 'burst' messages during any 'interval'.

func (Limiter) Allow

func (ll Limiter) Allow() bool

Allow returns true if the log message is allowed under the configured rate limit.

type LogFormat

type LogFormat string

type LogOptions

type LogOptions map[string]string

LogOptions maps configuration key-value pairs related to logging.

func (LogOptions) GetLogFormat

func (o LogOptions) GetLogFormat() LogFormat

GetLogFormat returns the log format specified in the provided LogOptions. If it is not set in the options or is invalid, it will return the default format.

func (LogOptions) GetLogLevel

func (o LogOptions) GetLogLevel() (level slog.Level)

GetLogLevel returns the log level specified in the provided LogOptions. If it is not set in the options, it will return the default level.

type SyslogHook added in v1.18.0

type SyslogHook struct {
	Writer        *syslog.Writer
	SyslogNetwork string
	SyslogRaddr   string
	// contains filtered or unexported fields
}

SyslogHook to send logs via syslog.

func NewSyslogHook added in v1.18.0

func NewSyslogHook(network, raddr string, priority syslog.Priority, tag string, slogLevel slog.Level) (*SyslogHook, error)

func (*SyslogHook) Enabled added in v1.18.0

func (hook *SyslogHook) Enabled(ctx context.Context, level slog.Level) bool

func (*SyslogHook) Handle added in v1.18.0

func (hook *SyslogHook) Handle(ctx context.Context, r slog.Record) error

func (*SyslogHook) WithAttrs added in v1.18.0

func (hook *SyslogHook) WithAttrs(attrs []slog.Attr) slog.Handler

func (*SyslogHook) WithGroup added in v1.18.0

func (hook *SyslogHook) WithGroup(name string) slog.Handler

Directories

Path Synopsis
Package logfields defines common logging fields which are used across packages
Package logfields defines common logging fields which are used across packages

Jump to

Keyboard shortcuts

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