zerologr

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2021 License: Apache-2.0 Imports: 2 Imported by: 0

README

Zerologr

Go Reference test Go Report Card

A logr LogSink implementation using Zerolog.

Usage

import (
    "os"

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

func main() {
    zerologr.NameFieldName = "logger"
    zerologr.NameSeparator = "/"

    zl := zerolog.New(os.Stderr)
    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.

Levels in logr correspond to custom debug levels in Zerolog. Any given level in logr is represents by zerologLevel = 1 - logrLevel.

For example V(2) is equivalent to Zerolog's TraceLevel, while V(1) is equivalent to Zerolog's DebugLevel.

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.

Levels in logr correspond to custom debug levels in Zerolog. Any given level in logr is represents by `zerologLevel = 1 - logrLevel`. For example V(2) is equivalent to Zerolog's TraceLevel, while V(1) is equivalent to Zerolog's DebugLevel. Zerolog's usual "level" field is disabled globally and replaced with "v", whose 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 = "/"
)

Functions

This section is empty.

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 added in v1.1.0

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.

type Underlier added in v1.1.0

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