zlog

package module
v1.1.8 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2023 License: Apache-2.0 Imports: 11 Imported by: 120

Documentation

Overview

Package zlog is a logging facade backed by zerolog.

It uses opentelemetry baggage to generate log contexts.

By default, the package wraps the zerolog global logger. This can be changed via the Set function.

In addition, a testing adapter is provided to keep testing logs orderly.

Example
l := zerolog.New(os.Stdout)
Set(&l)
ctx := context.Background()
m, _ := baggage.NewMember("key", "value1")
b, _ := baggage.FromContext(ctx).SetMember(m)
ctx = baggage.ContextWithBaggage(ctx, b)
Log(ctx).Msg("message")
m, _ = baggage.NewMember("key", "value2")
b, _ = baggage.FromContext(ctx).SetMember(m)
ctx = baggage.ContextWithBaggage(ctx, b)
Log(ctx).Msg("message")
Output:

{"key":"value1","message":"message"}
{"key":"value2","message":"message"}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithValues added in v1.1.0

func ContextWithValues(ctx context.Context, pairs ...string) context.Context

ContextWithValues is a helper for the go.opentelemetry.io/otel/baggage v1 API. It takes pairs of strings and adds them to the Context via the baggage package.

Any trailing value is silently dropped.

Example
l := zerolog.New(os.Stdout)
Set(&l)
ctx := context.Background()
ctx = ContextWithValues(ctx, "key1", "value1")
Log(ctx).Msg("message")
ctx = ContextWithValues(ctx, "key1", "value2", "key2", "value1", "dropme")
Log(ctx).Msg("message")
// Can't capture the output because iteration order isn't guarenteed.
Output:

func Debug

func Debug(ctx context.Context) *zerolog.Event

Debug starts a new message with the debug level.

func Error

func Error(ctx context.Context) *zerolog.Event

Error starts a new message with the error level.

func Info

func Info(ctx context.Context) *zerolog.Event

Info starts a new message with the infor level.

func Log

func Log(ctx context.Context) *zerolog.Event

Log starts a new message with no level.

func Set

func Set(l *zerolog.Logger)

Set configures the logger used by this package.

This function is unsafe to use concurrently with the other functions of this package.

func Test

func Test(ctx context.Context, t testing.TB) context.Context

Test configures and wires up the global logger for testing.

Once called, log messages that do not use a Context returned by a call to Test will cause a panic.

Passing a nil Context will return a Context derived from context.Background.

func Trace

func Trace(ctx context.Context) *zerolog.Event

Trace starts a new message with the trace level.

func Warn

func Warn(ctx context.Context) *zerolog.Event

Warn starts a new message with the warn level.

func WithLevel

func WithLevel(ctx context.Context, l zerolog.Level) *zerolog.Event

WithLevel starts a new message with the specified level.

Types

This section is empty.

Jump to

Keyboard shortcuts

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