logging

package
v1.12.2 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Note(krancour): Numerically speaking, zap supports levels above or below
	// those for it has defined constants. This is how we implement our own
	// Discard and Trace levels.
	DiscardLevel Level = Level(zapcore.FatalLevel + 1)
	ErrorLevel   Level = Level(zapcore.ErrorLevel)
	InfoLevel    Level = Level(zapcore.InfoLevel)
	DebugLevel   Level = Level(zapcore.DebugLevel)
	TraceLevel   Level = DebugLevel - 1

	ConsoleFormat Format = "console"
	JSONFormat    Format = "json"
	DefaultFormat Format = ConsoleFormat

	LogLevelEnvVar  = "LOG_LEVEL"
	LogFormatEnvVar = "LOG_FORMAT"
)

Variables

This section is empty.

Functions

func ContextWithLogger

func ContextWithLogger(ctx context.Context, logger *Logger) context.Context

ContextWithLogger returns a context.Context that has been augmented with the provided *Logger.

Types

type Format

type Format string

func ParseFormat

func ParseFormat(f string) (Format, error)

ParseFormat parses a string representation of a log format and returns the corresponding Format value or an error if it isn't recognized

type Level

type Level int8

func ParseLevel

func ParseLevel(levelStr string) (Level, error)

ParseLevel parses a string representation of a log level and returns the corresponding Level value.

type Logger

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

Logger is a simple wrapper around zap.Logger that provides a more ergonomic API.

func LoggerFromContext

func LoggerFromContext(ctx context.Context) *Logger

LoggerFromContext extracts a *Logger from the provided context.Context and returns it. If no *Logger is found, a global *Logger is returned.

func NewDiscardLoggerOrDie added in v1.8.8

func NewDiscardLoggerOrDie() *Logger

NewDiscardLoggerOrDie returns a new *Logger that discards all log output or panics if there is an error configuring the logger. This is primarily useful for tests.

func NewLogger

func NewLogger(level Level, format Format) (*Logger, error)

NewLogger returns a new *Logger with the provided log level.

func NewLoggerOrDie

func NewLoggerOrDie(level Level, format Format) *Logger

NewLoggerOrDie returns a new *Logger with the provided log level or panics if there is an error configuring the logger.

func Wrap

func Wrap(zapLogger *zap.Logger) *Logger

Wrap returns a new *Logger that wraps the provided zap.Logger.

func (*Logger) Debug

func (l *Logger) Debug(msg string, keysAndValues ...any)

Debug logs a message at the debug level.

func (*Logger) Error

func (l *Logger) Error(err error, msg string, keysAndValues ...any)

Error logs a message at the error level.

func (*Logger) Info

func (l *Logger) Info(msg string, keysAndValues ...any)

Info logs a message at the info level.

func (*Logger) Logr added in v1.8.8

func (l *Logger) Logr() logr.Logger

Logr returns a logr.Logger wrapped in this Logger's underlying zap.Logger for cases where one needs to to pass the a logr.Logger to another library that obviously doesn't know how to work with our custom one.

func (*Logger) Trace

func (l *Logger) Trace(msg string, keysAndValues ...any)

Trace logs a message at the trace level.

func (*Logger) WithValues

func (l *Logger) WithValues(keysAndValues ...any) *Logger

WithValues adds key-value pairs to a logger's context.

func (*Logger) WithoutStackTraces added in v1.12.0

func (l *Logger) WithoutStackTraces() *Logger

WithoutStackTraces returns a copy of a logger that does not attach a stack trace to the records it writes. A stack trace describes the path to the logging call, not the path to whatever went wrong, so it is worth having only where an error is logged near where it arose. Callers that log errors on behalf of code far below them should use this.

Jump to

Keyboard shortcuts

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