Skip to content

Commit

Permalink
added the kvs fields to the logger in context
Browse files Browse the repository at this point in the history
  • Loading branch information
maurafortino committed Jun 9, 2023
1 parent 7f099c7 commit d8136ab
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions basculeLogging.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"fmt"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -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))
})
}
}
Expand All @@ -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)

}

0 comments on commit d8136ab

Please sign in to comment.