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 ¶
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 ¶
Debug starts a new message with the debug level.
func Error ¶
Error starts a new message with the error level.
func Info ¶
Info starts a new message with the infor level.
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.
func Trace ¶
Trace starts a new message with the trace level.
func Warn ¶
Warn starts a new message with the warn level.
Types ¶
This section is empty.