log: do not pre-process log results (#8895)

I was digging around in the zero log functions, and the following
functions using the `Fields()` method directly in zerolog, 

- https://github.com/rs/zerolog/blob/v1.27.0/event.go#L161
- e9344a8c50/fields.go (L15)

Have meaningfully equivalent semantics and our pre-processing of
values is getting us much (except forcing zerolog to always sort our
keys, and nooping in the case when you miss the last field.)

With this change also, we can pass maps (or, pratically a single map)
to the logger, which might be a less wacky seeming interface.
This commit is contained in:
Sam Kleinman
2022-06-28 10:40:16 -04:00
committed by GitHub
parent 013b46a6c3
commit 37f9d59969

View File

@@ -61,20 +61,20 @@ func NewDefaultLogger(format, level string) (Logger, error) {
}
func (l defaultLogger) Info(msg string, keyVals ...interface{}) {
l.Logger.Info().Fields(getLogFields(keyVals...)).Msg(msg)
l.Logger.Info().Fields(keyVals).Msg(msg)
}
func (l defaultLogger) Error(msg string, keyVals ...interface{}) {
l.Logger.Error().Fields(getLogFields(keyVals...)).Msg(msg)
l.Logger.Error().Fields(keyVals).Msg(msg)
}
func (l defaultLogger) Debug(msg string, keyVals ...interface{}) {
l.Logger.Debug().Fields(getLogFields(keyVals...)).Msg(msg)
l.Logger.Debug().Fields(keyVals).Msg(msg)
}
func (l defaultLogger) With(keyVals ...interface{}) Logger {
return &defaultLogger{
Logger: l.Logger.With().Fields(getLogFields(keyVals...)).Logger(),
Logger: l.Logger.With().Fields(keyVals).Logger(),
}
}
@@ -99,16 +99,3 @@ func OverrideWithNewLogger(logger Logger, format, level string) error {
ol.Logger = nl.Logger
return nil
}
func getLogFields(keyVals ...interface{}) map[string]interface{} {
if len(keyVals)%2 != 0 {
return nil
}
fields := make(map[string]interface{}, len(keyVals))
for i := 0; i < len(keyVals); i += 2 {
fields[fmt.Sprint(keyVals[i])] = keyVals[i+1]
}
return fields
}