feat: add option for injecting additional log fields#62
Open
dominicbarnes wants to merge 2 commits intosimukti:masterfrom
Open
feat: add option for injecting additional log fields#62dominicbarnes wants to merge 2 commits intosimukti:masterfrom
dominicbarnes wants to merge 2 commits intosimukti:masterfrom
Conversation
Owner
Hi @dominicbarnes, I think it's better (and simpler) that you create a custom Let's take zerolog adapter as a base and modify it: type zerologAdapter struct {
logger zerolog.Logger
additionalFields map[string]interface{}
}
func NewWithAdditionalFields(logger zerolog.Logger, additionalFields map[string]interface{}) sqldblogger.Logger {
return &zerologAdapter{logger: logger, additionalFields: additionalFields}
}
func (zl *zerologAdapter) Log(_ context.Context, level sqldblogger.Level, msg string, data map[string]interface{}) {
var lvl zerolog.Level
switch level {
case sqldblogger.LevelError:
lvl = zerolog.ErrorLevel
case sqldblogger.LevelInfo:
lvl = zerolog.InfoLevel
case sqldblogger.LevelDebug:
lvl = zerolog.DebugLevel
case sqldblogger.LevelTrace:
lvl = zerolog.TraceLevel
default:
lvl = zerolog.DebugLevel
}
for k, v := range zl.additionalFields {
if _, ok := data[k]; !ok {
data[k] = v
}
}
zl.logger.WithLevel(lvl).Fields(data).Msg(msg)
} |
Author
|
I think both options would be valuable, seems like being able to generalize for any logger is preferred to needing to inject for each individual logger. That being said, you would only ever need 1 logger for any implementation, so maybe it's a moot point. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I wanted the ability to inject some additional log fields at init time. Building on
Optionseemed like a logic fit for this functionality, which I've called "additional fields".This should allow a user to use something like this when initializing their logger:
It should be noted that I intentionally append the user-defined fields first so that in the event of a conflict, the internal ones will take precedence, which seemed like the right thing to do behaviorally.