zerologr

package module
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2023 License: Apache-2.0 Imports: 3 Imported by: 107

README

Zerologr

Go Reference test Go Report Card

A logr LogSink implementation using Zerolog.

Usage

import (
    "os"

    "github.com/go-logr/logr"
    "github.com/go-logr/zerologr"
    "github.com/rs/zerolog"
)

func main() {
    zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs

    zerologr.NameFieldName = "logger"
    zerologr.NameSeparator = "/"
    zerologr.SetMaxV(1)

    zl := zerolog.New(os.Stderr)
    zl = zl.With().Caller().Timestamp().Logger()
    var log logr.Logger = zerologr.New(&zl)

    log.Info("Logr in action!", "the answer", 42)
}

Implementation Details

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

V-levels in logr correspond to levels in Zerolog as zerologLevel = 1 - logrV. logr.V(0) is equivalent to zerolog.InfoLevel or 1; logr.V(1) is equivalent to zerolog.DebugLevel or 0 (default global level in Zerolog); logr.V(2) is equivalent to zerolog.TraceLevel or -1. Higher than 2 V-level is possible but misses some features in Zerolog, e.g. Hooks and Sampling. V-level value is a number and is only logged on Info(), not Error().

Documentation

Overview

Package zerologr defines an implementation of the github.com/go-logr/logr interfaces built on top of Zerolog (https://github.com/rs/zerolog).

Usage

A new logr.Logger can be constructed from an existing zerolog.Logger using the New function:

log := zerologr.New(someZeroLogger)

Implementation Details

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

V-levels in logr correspond to levels in Zerolog as `zerologLevel = 1 - logrV`. `logr.V(0)` is equivalent to `zerolog.InfoLevel` or 1; `logr.V(1)` is equivalent to `zerolog.DebugLevel` or 0 (default global level in Zerolog); `logr.V(2)` is equivalent to `zerolog.TraceLevel` or -1. Higher than 2 V-level is possible but misses some features in Zerolog, e.g. Hooks and Sampling. V-level value is a number and is only logged on Info(), not Error().

Index

Constants

This section is empty.

Variables

View Source
var (
	// NameFieldName is the field key for logr.WithName.
	NameFieldName = "logger"
	// NameSeparator separates names for logr.WithName.
	NameSeparator = "/"
	// VerbosityFieldName is the field key for logr.Info verbosity. If set to "",
	// its value is not emitted.
	VerbosityFieldName = "v"

	// RenderArgsHook mutates the list of key-value pairs passed directly to
	// logr.Info and logr.Error. If set to nil, it is disabled.
	RenderArgsHook = DefaultRender
	// RenderValuesHook mutates the list of key-value pairs saved via logr.WithValues.
	// If set to nil, it is disabled.
	RenderValuesHook = DefaultRender
)

Functions

func DefaultRender added in v1.2.0

func DefaultRender(keysAndValues []interface{}) []interface{}

DefaultRender supports logr.Marshaler and fmt.Stringer.

func SetMaxV added in v1.2.3

func SetMaxV(level int)

SetMaxV updates Zerolog's global level. Default max V-level is 1 (DebugLevel). The range of max V-level is 0 through 129 inclusive, but higher than 2 V-level misses some features in Zerolog, e.g. Hooks and Sampling.

Types

type LogSink

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

LogSink implements logr.LogSink and logr.CallDepthLogSink.

func NewLogSink

func NewLogSink(l *zerolog.Logger) *LogSink

NewLogSink returns a logr.LogSink implemented by Zerolog.

func (*LogSink) Enabled

func (ls *LogSink) Enabled(level int) bool

Enabled tests whether this LogSink is enabled at the specified V-level.

func (*LogSink) Error

func (ls *LogSink) Error(err error, msg string, keysAndValues ...interface{})

Error logs an error, with the given message and key/value pairs as context.

func (*LogSink) GetUnderlying

func (ls *LogSink) GetUnderlying() *zerolog.Logger

GetUnderlying returns the zerolog.Logger underneath this logSink.

func (*LogSink) Info

func (ls *LogSink) Info(level int, msg string, keysAndValues ...interface{})

Info logs a non-error message at specified V-level with the given key/value pairs as context.

func (*LogSink) Init

func (ls *LogSink) Init(ri logr.RuntimeInfo)

Init receives runtime info about the logr library.

func (LogSink) WithCallDepth

func (ls LogSink) WithCallDepth(depth int) logr.LogSink

WithCallDepth returns a new LogSink that offsets the call stack by adding specified depths.

func (LogSink) WithName

func (ls LogSink) WithName(name string) logr.LogSink

WithName returns a new LogSink with the specified name appended in NameFieldName. Name elements are separated by NameSeparator.

func (LogSink) WithValues

func (ls LogSink) WithValues(keysAndValues ...interface{}) logr.LogSink

WithValues returns a new LogSink with additional key/value pairs.

type Logger

type Logger = logr.Logger

Logger is type alias of logr.Logger.

func New

func New(l *zerolog.Logger) Logger

New returns a logr.Logger with logr.LogSink implemented by Zerolog. Local level is mutated to allow max V-level if not set explicitly, so SetMaxV alone can control Zerolog's level. Use NewLogSink directly if local level mutation is undesirable.

type Underlier

type Underlier interface {
	GetUnderlying() *zerolog.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