improve CtxErr handling (#4914)
#### Does this PR need a docs update or release note? - [x] ⛔ No #### Type of change - [x] 🧹 Tech Debt/Cleanup #### Test Plan - [x] ⚡ Unit test - [x] 💚 E2E
This commit is contained in:
parent
c1d0c5bb8d
commit
26b1299927
@ -3,6 +3,7 @@ package logger
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"maps"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
@ -472,7 +473,7 @@ func SetWithSettings(
|
|||||||
return context.WithValue(ctx, ctxKey, logger)
|
return context.WithValue(ctx, ctxKey, logger)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ctxNoClues(ctx context.Context) *zap.SugaredLogger {
|
func zslFromCtx(ctx context.Context) *zap.SugaredLogger {
|
||||||
l := ctx.Value(ctxKey)
|
l := ctx.Value(ctxKey)
|
||||||
if l == nil {
|
if l == nil {
|
||||||
l = singleton(Settings{}.EnsureDefaults())
|
l = singleton(Settings{}.EnsureDefaults())
|
||||||
@ -483,7 +484,7 @@ func ctxNoClues(ctx context.Context) *zap.SugaredLogger {
|
|||||||
|
|
||||||
// Ctx retrieves the logger embedded in the context.
|
// Ctx retrieves the logger embedded in the context.
|
||||||
func Ctx(ctx context.Context) *zap.SugaredLogger {
|
func Ctx(ctx context.Context) *zap.SugaredLogger {
|
||||||
return ctxNoClues(ctx).With(clues.In(ctx).Slice()...)
|
return zslFromCtx(ctx).With(clues.In(ctx).Slice()...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CtxStack retrieves the logger embedded in the context, and adds the
|
// CtxStack retrieves the logger embedded in the context, and adds the
|
||||||
@ -497,13 +498,20 @@ func CtxStack(ctx context.Context, skip int) *zap.SugaredLogger {
|
|||||||
// CtxErr retrieves the logger embedded in the context
|
// CtxErr retrieves the logger embedded in the context
|
||||||
// and packs all of the structured data in the error inside it.
|
// and packs all of the structured data in the error inside it.
|
||||||
func CtxErr(ctx context.Context, err error) *zap.SugaredLogger {
|
func CtxErr(ctx context.Context, err error) *zap.SugaredLogger {
|
||||||
// don't add the ctx clues or else values will duplicate between
|
ctxVals := clues.In(ctx).Map()
|
||||||
// the err clues and ctx clues.
|
errVals := clues.InErr(err).Map()
|
||||||
return ctxNoClues(ctx).
|
|
||||||
With(
|
maps.Copy(ctxVals, errVals)
|
||||||
"error", err,
|
|
||||||
"error_labels", clues.Labels(err)).
|
zsl := zslFromCtx(ctx).
|
||||||
With(clues.InErr(err).Slice()...)
|
With("error", err).
|
||||||
|
With("error_labels", clues.Labels(err))
|
||||||
|
|
||||||
|
for k, v := range ctxVals {
|
||||||
|
zsl = zsl.With(k, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return zsl
|
||||||
}
|
}
|
||||||
|
|
||||||
// CtxErrStack retrieves the logger embedded in the context
|
// CtxErrStack retrieves the logger embedded in the context
|
||||||
@ -511,12 +519,7 @@ func CtxErr(ctx context.Context, err error) *zap.SugaredLogger {
|
|||||||
// If skip is non-zero, it skips the stack calls starting from the
|
// If skip is non-zero, it skips the stack calls starting from the
|
||||||
// first. Skip always adds +1 to account for this wrapper.
|
// first. Skip always adds +1 to account for this wrapper.
|
||||||
func CtxErrStack(ctx context.Context, err error, skip int) *zap.SugaredLogger {
|
func CtxErrStack(ctx context.Context, err error, skip int) *zap.SugaredLogger {
|
||||||
return ctxNoClues(ctx).
|
return CtxErr(ctx, err).With(zap.StackSkip("trace", skip+1))
|
||||||
With(
|
|
||||||
"error", err,
|
|
||||||
"error_labels", clues.Labels(err)).
|
|
||||||
With(zap.StackSkip("trace", skip+1)).
|
|
||||||
With(clues.InErr(err).Slice()...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flush writes out all buffered logs.
|
// Flush writes out all buffered logs.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user