-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Specify log level by parameter #1084
Comments
Hey @adrianlungu, thanks for the suggestion! Could you give some more detail about the use case you're trying to solve for? |
Hey @mway , We use
This allows us to use different levels based on the status code without having to switch between different logger functions or loggers. Here is an example of where we use this: |
It is possible to use logger.Check(level, "message").Write(fields...) The For performance reasons, it's recommended to check the returned entry before writing (avoids passing the varargs fields unnecessarily): if ce := logger.Check(level, "message"); ce != nil {
ce.Write(fields...)
} That said, i think adding logger.Log(level, "message", fields...) |
@prashantv yes, that is what we currently use, i.e. func logWithLevel(logger *zap.Logger, level zapcore.Level, msg string, fields ...zapcore.Field) {
if ce := logger.Check(level, msg); ce != nil {
ce.Write(fields...)
}
} |
Having logger.Log(level, "message", fields...) Also means that functions such as func (log *Logger) Info(msg string, fields ...Field) {
if ce := log.check(InfoLevel, msg); ce != nil {
ce.Write(fields...)
}
} could be written as func (log *Logger) Info(msg string, fields ...Field) {
log.Log(InfoLevel, msg, fields)
} although I wouldn't say it's a change that brings that much benefit |
The calls would likely be inlined, so that would just result in code deduplication which is always nice. Edit: Worth noting that if we were to reuse this internally, we would need to be careful not to break the caller skip for stack traces. (Even with mid-stack inlining, all inline points are treated as logical stack frames and are still reported by the runtime.) |
Adds a Logger.Log method which logs a message and fields at the provided level. log.Log(zap.InfoLevel, msg, fields...) // is the same as log.Info(msg, fields...) Resolve #1084
Hello!
Would it be possible to have something like the following added to the logger ?
this would allow anyone to also make dynamic calls i.e.
log.Log(ErrorLevel, "message", fields...)
.Thanks!
The text was updated successfully, but these errors were encountered: