zapr

package
v0.0.0-...-26d83ff Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package zapr defines an implementation of the github.com/go-logr/logr interfaces built on top of Zap (go.uber.org/zap).

Usage

A new logr.Logger can be constructed from an existing zap.Logger using the NewLogger function:

log := zapr.NewLogger(someZapLogger)

Implementation Details

For the most part, concepts in Zap correspond directly with those in logr.

Unlike Zap, all fields *must* be in the form of sugared fields -- it's illegal to pass a strongly-typed Zap field in a key position to any of the log methods.

Levels in logr correspond to custom debug levels in Zap. Any given level in logr is represents by its inverse in zap (`zapLevel = -1*logrLevel`). For example V(2) is equivalent to log level -2 in Zap, while V(1) is equivalent to Zap's DebugLevel.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogger

func NewLogger(l *zap.Logger) logr.Logger

NewLogger creates a new logr.Logger using the given Zap Logger to log.

func NewLoggerWithOptions

func NewLoggerWithOptions(l *zap.Logger, opts ...Option) logr.Logger

NewLoggerWithOptions creates a new logr.Logger using the given Zap Logger to log and applies additional options.

Types

type Option

type Option func(*zapLogger)

Option is one additional parameter for NewLoggerWithOptions.

func AllowZapFields

func AllowZapFields(allowed bool) Option

AllowZapFields controls whether strongly-typed Zap fields may be passed instead of a key/value pair. This is disabled by default because it breaks implementation agnosticism.

func DPanicOnBugs

func DPanicOnBugs(enabled bool) Option

DPanicOnBugs controls whether extra log messages are emitted for invalid log calls with zap's DPanic method. Depending on the configuration of the zap logger, the program then panics after emitting the log message which is useful in development because such invalid log calls are bugs in the program. The log messages explain why a call was invalid (for example, non-string key). Emitting them is enabled by default.

func ErrorKey

func ErrorKey(key string) Option

ErrorKey replaces the default "error" field name used for the error in Logger.Error calls.

func LogInfoLevel

func LogInfoLevel(key string) Option

LogInfoLevel controls whether a numeric log level is added to Info log message. The empty string disables this, a non-empty string is the key for the additional field. Errors and internal panic messages do not have a log level and thus are always logged without this extra field.

func VerbosityLevel

func VerbosityLevel(level int) Option

VerbosityLevel sets the verbosity level of the logger. It is used to determine whether a log message is actually logged. The default is 0 which means that no debug log messages are logged. A higher value means that more log messages are logged.

type Underlier

type Underlier interface {
	GetUnderlying() *zap.Logger
}

Underlier exposes access to the underlying logging implementation. Since callers only have a logr.Logger, they have to know which implementation is in use, so this interface is less of an abstraction and more of way to test type conversion.

Jump to

Keyboard shortcuts

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