From 44382daa5564c31cd8b7cb2e7da2ec5623a322a3 Mon Sep 17 00:00:00 2001 From: maura fortino Date: Wed, 14 Jun 2023 16:54:25 -0400 Subject: [PATCH] added staticlink check and updated setLogger to log kvs --- basculeLogging.go | 18 +++++++++++++++--- primaryHandler_test.go | 1 + 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/basculeLogging.go b/basculeLogging.go index c4fffb13..3ff81d25 100644 --- a/basculeLogging.go +++ b/basculeLogging.go @@ -2,6 +2,7 @@ package main import ( "context" + "fmt" "net/http" "strings" "time" @@ -29,8 +30,9 @@ func setLogger(logger *zap.Logger) func(delegate http.Handler) http.Handler { func(w http.ResponseWriter, r *http.Request) { kvs := []interface{}{"requestHeaders", sanitizeHeaders(r.Header), "requestURL", r.URL.EscapedPath(), "method", r.Method} 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)) }) } } @@ -38,4 +40,14 @@ func setLogger(logger *zap.Logger) func(delegate http.Handler) http.Handler { func getLogger(ctx context.Context) *zap.Logger { logger := sallust.Get(ctx).With(zap.Time("ts", time.Now().UTC()), zap.Any("caller", zap.WithCaller(true))) return logger -} \ No newline at end of file +} + +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) + +} diff --git a/primaryHandler_test.go b/primaryHandler_test.go index 8e1833da..35963043 100644 --- a/primaryHandler_test.go +++ b/primaryHandler_test.go @@ -8,6 +8,7 @@ import ( "github.com/stretchr/testify/require" "github.com/xmidt-org/webpa-common/v2/adapter" + // nolint:staticcheck "github.com/xmidt-org/webpa-common/v2/xmetrics" )