From d8136abfeced555ebd6c1c260bb7eccae44bcabb Mon Sep 17 00:00:00 2001 From: maura fortino Date: Fri, 9 Jun 2023 13:06:54 -0400 Subject: [PATCH] added the kvs fields to the logger in context --- basculeLogging.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/basculeLogging.go b/basculeLogging.go index 26c6ad1..e138e03 100644 --- a/basculeLogging.go +++ b/basculeLogging.go @@ -2,6 +2,7 @@ package main import ( "context" + "fmt" "net/http" "strings" "time" @@ -44,10 +45,10 @@ func setLogger(logger *zap.Logger, lf ...LoggerFunc) func(delegate http.Handler) kvs = f(kvs, r) } } - // nolint:staticcheck kvs, _ = candlelight.AppendTraceInfo(r.Context(), kvs) - ctx := r.WithContext(sallust.With(r.Context(), logger)) - delegate.ServeHTTP(w, ctx) + ctx := r.Context() + ctx = addFieldsToLog(ctx, logger, kvs) + delegate.ServeHTTP(w, r.WithContext(ctx)) }) } } @@ -56,3 +57,13 @@ func getLogger(ctx context.Context) *zap.Logger { logger := sallust.Get(ctx).With(zap.Time("ts", time.Now().UTC())).WithOptions(zap.WithCaller(true)) return logger } + +func addFieldsToLog(ctx context.Context, logger *zap.Logger, kvs []interface{}) context.Context { + + for i := 0; i <= len(kvs)-2; i += 2 { + logger = logger.With(zap.Any(fmt.Sprint(kvs[i]), kvs[i+1])) + } + + return sallust.With(ctx, logger) + +}