storagenode/pieces: capture logger time Key in zapwrapper

Updates https://github.com/storj/storj/issues/5349

Change-Id: I426f38c0ae0f93d498317e3f66ba4f5724620758
This commit is contained in:
Clement Sam 2023-05-05 20:53:56 +00:00
parent 6ac72e42cd
commit ec8bfe6b94

View File

@ -6,8 +6,10 @@ package lazyfilewalker
import (
"bytes"
"encoding/json"
"io"
"strconv"
"strings"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
@ -17,6 +19,8 @@ type zapWrapper struct {
Log *zap.Logger
}
var _ io.Writer = (*zapWrapper)(nil)
// Write writes the provided bytes to the underlying logger
// returns the length of the bytes.
//
@ -40,6 +44,7 @@ type zapLogger struct {
Message string `json:"M"`
Stack string `json:"S"`
Name string `json:"N"`
Time time.Time `json:"T"`
LogMap map[string]interface{} `json:"-"`
}
@ -72,6 +77,7 @@ func (w *zapWrapper) log(b []byte) error {
delete(logger.LogMap, "M")
delete(logger.LogMap, "S")
delete(logger.LogMap, "N")
delete(logger.LogMap, "T")
log := w.Log.Named(logger.Name)
if ce := log.Check(logger.Level, logger.Message); ce != nil {
@ -82,6 +88,10 @@ func (w *zapWrapper) log(b []byte) error {
ce.Caller = *caller
}
if !logger.Time.IsZero() {
ce.Time = logger.Time
}
var fields []zapcore.Field
for key, val := range logger.LogMap {
fields = append(fields, zap.Any(key, val))