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 ¶
- func ContextWithValues(ctx context.Context, pairs ...string) context.Context
- func Debug(ctx context.Context) *zerolog.Event
- func Error(ctx context.Context) *zerolog.Event
- func Info(ctx context.Context) *zerolog.Event
- func Log(ctx context.Context) *zerolog.Event
- func Set(l *zerolog.Logger)
- func Test(ctx context.Context, t testing.TB) context.Context
- func Trace(ctx context.Context) *zerolog.Event
- func Warn(ctx context.Context) *zerolog.Event
- func WithLevel(ctx context.Context, l zerolog.Level) *zerolog.Event
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextWithValues ¶ added in v1.1.0
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 Set ¶
Set configures the logger used by this package.
This function is unsafe to use concurrently with the other functions of this package.
func Test ¶
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.
Types ¶
This section is empty.