ctxlog
provides super simple logging.
Code:
log := ctxlog.New(os.Stdout)
ctx := ctxlog.Add(context.Background(), "user_id", 23, "foo", "bar")
log.Debug(ctx, "default level is info")
log.Info(ctx, "var_val", time.Unix(0, 0).UTC(), "odd number of fields treated as message")
Output:
{"level":"INFO","user_id":23,"foo":"bar","var_val":"1970-01-01T00:00:00Z","message":"odd number of fields treated as message"}
A global logger is available:
Code:
ctxlog.SetOutput(os.Stdout)
ctx := ctxlog.Add(context.Background(), "foo", "bar")
ctxlog.Info(ctx, "noice")
Output:
{"level":"INFO","foo":"bar","message":"noice"}
Along with all the usual perks:
Code:
log := ctxlog.New(os.Stdout, ctxlog.OptCaller(false))
log.Error(context.Background())
Output:
{"level":"ERROR","caller":"example_test.go:25"}