Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove time, level, and message keys in logger provider #123

Merged
merged 7 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions changelog/@unreleased/pr-123.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type: improvement
improvement:
description: |
Removes the time, level and message keys from the loggers returned by implementations of the LoggerProvider interface.
Previously, wlog.Logger instances returned by the LoggerProvider interface set "time" as a top-level key and wlog.LeveledLogger instances returned by the LoggerProvider interface set "time", "level" and "message" as top-level keys. After this change, these top-level keys are no longer set. This allows the underlying logger interfaces to be used by logger implementations that may not set these top-level keys in their output.
Log type implementations that use these loggers may no longer assume that these keys are set and must set them themselves if they are desired in the output. Existing implementations have all been updated to do so, so there should be no behavioral changes.
links:
- https://github.com/palantir/witchcraft-go-logging/pull/123
10 changes: 2 additions & 8 deletions wlog-glog/internal/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,10 @@ func (*gLogger) SetLevel(level wlog.LogLevel) {

func createGLogMsg(msg string, params []wlog.Param) string {
entry := wlog.NewMapLogEntry()
wlog.ApplyParams(entry, params)

var parts []string
if msg != "" {
parts = append(parts, msg)
}
parts = append(parts, paramsToLog(entry)...)
wlog.ApplyParams(entry, wlog.ParamsWithMessage(msg, params))

// TODO: ignore/omit unsafe params?
return strings.Join(parts, ", ")
return strings.Join(paramsToLog(entry), ", ")
}

// paramsToLog returns the parameters to log as strings of the form "<key>: <value>".
Expand Down
5 changes: 3 additions & 2 deletions wlog-zap/internal/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ func (l *zapLogger) SetLevel(level wlog.LogLevel) {

func logOutput(logFn func(string, ...zap.Field), msg string, params []wlog.Param) {
entry := newZapLogEntry()
wlog.ApplyParams(entry, params)
logFn(msg, entry.Fields()...)
wlog.ApplyParams(entry, wlog.ParamsWithMessage(msg, params))
// Empty string is used for the "message" because the message is added to params above if present
logFn("", entry.Fields()...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment that notes that "message" parameter is empty string because it has already been added to the parameters via the ApplyParams call if present?

}

func encodeField(key string, value interface{}, enc zapcore.ObjectEncoder) error {
Expand Down
5 changes: 0 additions & 5 deletions wlog-zap/internal/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"time"

"github.com/palantir/witchcraft-go-logging/wlog"
"github.com/palantir/witchcraft-go-logging/wlog/svclog/svc1log"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
Expand All @@ -33,7 +32,6 @@ type loggerProvider struct{}

func (lp *loggerProvider) NewLogger(w io.Writer) wlog.Logger {
logger, atomicLevel := newZapLogger(w, wlog.InfoLevel, zapcore.EncoderConfig{
TimeKey: wlog.TimeKey,
EncodeTime: rfc3339NanoTimeEncoder,
EncodeDuration: zapcore.NanosDurationEncoder,
})
Expand All @@ -45,12 +43,9 @@ func (lp *loggerProvider) NewLogger(w io.Writer) wlog.Logger {

func (lp *loggerProvider) NewLeveledLogger(w io.Writer, level wlog.LogLevel) wlog.LeveledLogger {
logger, atomicLevel := newZapLogger(w, level, zapcore.EncoderConfig{
TimeKey: wlog.TimeKey,
EncodeTime: rfc3339NanoTimeEncoder,
EncodeDuration: zapcore.NanosDurationEncoder,
LevelKey: svc1log.LevelKey,
EncodeLevel: zapcore.CapitalLevelEncoder,
MessageKey: svc1log.MessageKey,
})
return &zapLogger{
logger: logger,
Expand Down
18 changes: 6 additions & 12 deletions wlog-zerolog/internal/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ package zeroimpl

import (
"reflect"
"time"

"github.com/palantir/witchcraft-go-logging/wlog"
"github.com/palantir/witchcraft-go-logging/wlog-zerolog/internal/marshalers"
"github.com/palantir/witchcraft-go-logging/wlog/svclog/svc1log"
"github.com/rs/zerolog"
)

Expand Down Expand Up @@ -178,35 +176,35 @@ func (l *zeroLogger) Log(params ...wlog.Param) {
if !l.should(zerolog.NoLevel) {
return
}
logOutput(l.logger.Log, "", "", params)
logOutput(l.logger.Log, "", params)
}

func (l *zeroLogger) Debug(msg string, params ...wlog.Param) {
if !l.should(zerolog.DebugLevel) {
return
}
logOutput(l.logger.Log, msg, svc1log.LevelDebugValue, params)
logOutput(l.logger.Log, msg, params)
}

func (l *zeroLogger) Info(msg string, params ...wlog.Param) {
if !l.should(zerolog.InfoLevel) {
return
}
logOutput(l.logger.Log, msg, svc1log.LevelInfoValue, params)
logOutput(l.logger.Log, msg, params)
}

func (l *zeroLogger) Warn(msg string, params ...wlog.Param) {
if !l.should(zerolog.WarnLevel) {
return
}
logOutput(l.logger.Log, msg, svc1log.LevelWarnValue, params)
logOutput(l.logger.Log, msg, params)
}

func (l *zeroLogger) Error(msg string, params ...wlog.Param) {
if !l.should(zerolog.ErrorLevel) {
return
}
logOutput(l.logger.Log, msg, svc1log.LevelErrorValue, params)
logOutput(l.logger.Log, msg, params)
}

func (l *zeroLogger) SetLevel(level wlog.LogLevel) {
Expand All @@ -220,18 +218,14 @@ func reverseParams(params []wlog.Param) {
}
}

func logOutput(newEvt func() *zerolog.Event, msg, levelVal string, params []wlog.Param) {
func logOutput(newEvt func() *zerolog.Event, msg string, params []wlog.Param) {
entry := &zeroLogEntry{
evt: newEvt(),
keys: make(map[string]struct{}),
}
if !entry.evt.Enabled() {
return
}
entry.evt = entry.evt.Str(wlog.TimeKey, time.Now().Format(time.RFC3339Nano))
if levelVal != "" {
entry.evt = entry.evt.Str(svc1log.LevelKey, levelVal)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removal makes it such that the levelVal argument is no longer used -- can you remove it?

}
reverseParams(params)
wlog.ApplyParams(entry, params)
entry.Evt().Msg(msg)
Expand Down
3 changes: 3 additions & 0 deletions wlog/auditlog/audit2log/logger_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package audit2log

import (
"time"

"github.com/palantir/witchcraft-go-logging/wlog"
)

Expand All @@ -39,5 +41,6 @@ func toParams(name string, result AuditResultType, inParams []Param) []wlog.Para
var defaultTypeParam = []wlog.Param{
wlog.NewParam(func(entry wlog.LogEntry) {
entry.StringValue(wlog.TypeKey, TypeValue)
entry.StringValue(wlog.TimeKey, time.Now().Format(time.RFC3339Nano))
}),
}
3 changes: 3 additions & 0 deletions wlog/diaglog/diag1log/logger_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package diag1log

import (
"time"

"github.com/palantir/witchcraft-go-logging/conjure/witchcraft/api/logging"
"github.com/palantir/witchcraft-go-logging/wlog"
)
Expand All @@ -40,5 +42,6 @@ func toParams(diagnostic logging.Diagnostic, inParams []Param) []wlog.Param {
var defaultTypeParam = []wlog.Param{
wlog.NewParam(func(entry wlog.LogEntry) {
entry.StringValue(wlog.TypeKey, TypeValue)
entry.StringValue(wlog.TimeKey, time.Now().Format(time.RFC3339Nano))
}),
}
3 changes: 3 additions & 0 deletions wlog/evtlog/evt2log/logger_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package evt2log

import (
"time"

"github.com/palantir/witchcraft-go-logging/wlog"
)

Expand All @@ -39,5 +41,6 @@ func toParams(evtName string, inParams []Param) []wlog.Param {
var defaultTypeParam = []wlog.Param{
wlog.NewParam(func(entry wlog.LogEntry) {
entry.StringValue(wlog.TypeKey, TypeValue)
entry.StringValue(wlog.TimeKey, time.Now().Format(time.RFC3339Nano))
}),
}
8 changes: 4 additions & 4 deletions wlog/logger_provider_jsonmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,28 @@ func (l *jsonMapLogger) Log(params ...Param) {
func (l *jsonMapLogger) Debug(msg string, params ...Param) {
switch l.level {
case DebugLevel:
l.logOutput(append(params, StringParam("message", msg), StringParam("level", "DEBUG")))
l.logOutput(ParamsWithMessage(msg, params))
}
}

func (l *jsonMapLogger) Info(msg string, params ...Param) {
switch l.level {
case DebugLevel, InfoLevel:
l.logOutput(append(params, StringParam("message", msg), StringParam("level", "INFO")))
l.logOutput(ParamsWithMessage(msg, params))
}
}

func (l *jsonMapLogger) Warn(msg string, params ...Param) {
switch l.level {
case DebugLevel, InfoLevel, WarnLevel:
l.logOutput(append(params, StringParam("message", msg), StringParam("level", "WARN")))
l.logOutput(ParamsWithMessage(msg, params))
}
}

func (l *jsonMapLogger) Error(msg string, params ...Param) {
switch l.level {
case DebugLevel, InfoLevel, WarnLevel, ErrorLevel:
l.logOutput(append(params, StringParam("message", msg), StringParam("level", "ERROR")))
l.logOutput(ParamsWithMessage(msg, params))
}
}

Expand Down
3 changes: 3 additions & 0 deletions wlog/metriclog/metric1log/logger_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package metric1log

import (
"time"

"github.com/palantir/witchcraft-go-logging/wlog"
)

Expand All @@ -39,5 +41,6 @@ func toParams(metricName, metricType string, inParams []Param) []wlog.Param {
var defaultTypeParam = []wlog.Param{
wlog.NewParam(func(entry wlog.LogEntry) {
entry.StringValue(wlog.TypeKey, TypeValue)
entry.StringValue(wlog.TimeKey, time.Now().Format(time.RFC3339Nano))
}),
}
10 changes: 10 additions & 0 deletions wlog/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ func ApplyParams(logger LogEntry, params []Param) {
}
}

// ParamsWithMessage returns a new slice that appends a StringParam with the key "message" and
// value of the provided msg parameter if it is non-empty. If msg is empty, returns the provided slice
// without modification.
func ParamsWithMessage(msg string, params []Param) []Param {
if msg != "" {
return append(params, StringParam("message", msg))
}
return params
}

type paramFunc func(logger LogEntry)

func (f paramFunc) apply(logger LogEntry) {
Expand Down
2 changes: 2 additions & 0 deletions wlog/reqlog/req2log/logger_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package req2log

import (
"strings"
"time"

"github.com/palantir/witchcraft-go-logging/wlog"
"github.com/palantir/witchcraft-go-logging/wlog/extractor"
Expand Down Expand Up @@ -43,6 +44,7 @@ func (l *defaultLogger) Request(r Request) {

l.logger.Log(
wlog.StringParam(wlog.TypeKey, TypeValue),
wlog.StringParam(wlog.TimeKey, time.Now().Format(time.RFC3339Nano)),
wlog.OptionalStringParam(methodKey, r.Request.Method),
wlog.StringParam(protocolKey, r.Request.Proto),
wlog.StringParam(pathKey, reqPath),
Expand Down
51 changes: 41 additions & 10 deletions wlog/svclog/svc1log/logger_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,40 @@
package svc1log

import (
"time"

"github.com/palantir/witchcraft-go-logging/wlog"
)

var (
// Level params declared as variables so that they are only allocated once
debugLevelParam = wlog.NewParam(func(entry wlog.LogEntry) {
entry.StringValue(LevelKey, LevelDebugValue)
})
infoLevelParam = wlog.NewParam(func(entry wlog.LogEntry) {
entry.StringValue(LevelKey, LevelInfoValue)
})
warnLevelParam = wlog.NewParam(func(entry wlog.LogEntry) {
entry.StringValue(LevelKey, LevelWarnValue)
})
errorLevelParam = wlog.NewParam(func(entry wlog.LogEntry) {
entry.StringValue(LevelKey, LevelErrorValue)
})
)

func DebugLevelParam() wlog.Param {
return debugLevelParam
}
func InfoLevelParam() wlog.Param {
return infoLevelParam
}
func WarnLevelParam() wlog.Param {
return warnLevelParam
}
func ErrorLevelParam() wlog.Param {
return errorLevelParam
}

type defaultLogger struct {
loggerCreator func(level wlog.LogLevel) wlog.LeveledLogger

Expand All @@ -26,39 +57,39 @@ type defaultLogger struct {
}

func (l *defaultLogger) Debug(msg string, params ...Param) {
l.logger.Debug(msg, l.toParams(params)...)
l.logger.Debug(msg, toParams(DebugLevelParam(), params)...)
}

func (l *defaultLogger) Info(msg string, params ...Param) {
l.logger.Info(msg, l.toParams(params)...)
l.logger.Info(msg, toParams(InfoLevelParam(), params)...)

}

func (l *defaultLogger) Warn(msg string, params ...Param) {
l.logger.Warn(msg, l.toParams(params)...)
l.logger.Warn(msg, toParams(WarnLevelParam(), params)...)
}

func (l *defaultLogger) Error(msg string, params ...Param) {
l.logger.Error(msg, l.toParams(params)...)
l.logger.Error(msg, toParams(ErrorLevelParam(), params)...)
}

func (l *defaultLogger) SetLevel(level wlog.LogLevel) {
l.logger.SetLevel(level)
}

func (l *defaultLogger) toParams(inParams []Param) []wlog.Param {
if len(inParams) == 0 {
return defaultTypeParam
}
outParams := make([]wlog.Param, len(defaultTypeParam)+len(inParams))
func toParams(level wlog.Param, inParams []Param) []wlog.Param {
outParams := make([]wlog.Param, len(defaultTypeParam)+1+len(inParams))
copy(outParams, defaultTypeParam)
outParams[len(defaultTypeParam)] = level
for idx := range inParams {
outParams[len(defaultTypeParam)+idx] = wlog.NewParam(inParams[idx].apply)
outParams[len(defaultTypeParam)+1+idx] = wlog.NewParam(inParams[idx].apply)
}
return outParams
}

var defaultTypeParam = []wlog.Param{
wlog.NewParam(func(entry wlog.LogEntry) {
entry.StringValue(wlog.TypeKey, TypeValue)
entry.StringValue(wlog.TimeKey, time.Now().Format(time.RFC3339Nano))
}),
}
2 changes: 2 additions & 0 deletions wlog/trclog/trc1log/logger_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package trc1log

import (
"reflect"
"time"

"github.com/palantir/witchcraft-go-logging/wlog"
"github.com/palantir/witchcraft-go-tracing/wtracing"
Expand All @@ -32,6 +33,7 @@ func (l *defaultLogger) Log(span wtracing.SpanModel, params ...Param) {
append([]wlog.Param{
wlog.NewParam(func(entry wlog.LogEntry) {
entry.StringValue(wlog.TypeKey, TypeValue)
entry.StringValue(wlog.TimeKey, time.Now().Format(time.RFC3339Nano))
entry.ObjectValue(SpanKey, span, spanType)
}),
}, l.toParams(params)...)...,
Expand Down