-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Make level configurable for NewStdLog #439
Comments
No opposition from me, but I probably won't get to this for a while. |
I needed this for exact case that was mentioned ( // logWriter is wrapper around Logger that implements io.Writer interface.
type logWriter struct {
logFunc func(msg string, fields ...zapcore.Field)
}
// Write implements io.Writer for logWriter.
// It will never return error.
func (l *logWriter) Write(p []byte) (n int, err error) {
l.logFunc(string(p))
return len(p), nil
}
// ToWriter creates and returns io.Writer that logs all messages written to in
// by using provided Logger and log level.
// If unsupported log level is provided as parameter there will be not errors,
// but Warn level is used.
func ToWriter(logger *zap.Logger, level zapcore.Level, options ...zap.Option) io.Writer {
if logger == nil {
return ioutil.Discard
}
var logFunc func(msg string, fields ...zapcore.Field)
// since we are introducing new level of indirection, if provided
// implementation is zap.Logger, we know how to indicate to it to skip
// this level when reporting origin or the log message.
logger = logger.WithOptions(zap.AddCallerSkip(2)).WithOptions(options...)
switch level {
case zapcore.DebugLevel:
logFunc = logger.Debug
case zapcore.InfoLevel:
logFunc = logger.Info
case zapcore.WarnLevel:
logFunc = logger.Warn
case zapcore.ErrorLevel:
logFunc = logger.Error
case zapcore.PanicLevel:
logFunc = logger.Panic
case zapcore.DPanicLevel:
logFunc = logger.DPanic
case zapcore.FatalLevel:
logFunc = logger.Fatal
default:
logFunc = logger.Warn
}
return &logWriter{
logFunc: logFunc,
}
}
// NewStdLogger creates and returns new logger from standard library that
// writes all messages to provided zap logger. All messages will be logged
// under specified log level.
//
// zap.NewStdLog exist, but it does not allow specifying log level or custom
// options.
func NewStdLogger(logger *zap.Logger, level zapcore.Level, options ...zap.Option) *stdlog.Logger {
writer := ToWriter(logger.WithOptions(zap.AddCallerSkip(2)), level, options...)
return stdlog.New(writer, "", 0)
} |
I'm happy to take a PR for a I'm less convinced by the need to expose an |
@akshayjshah Of course, writer does not have to be public. I had to create it since I am using one library that writes stuff to output (stderr by default) and I can provide custom writer to it. I wanted to capture its output under specific logger name, so I wrote code above. Then I needed I understand that you do not want So, would you like me to write |
😻 I'd love a PR that implements |
With this change new function is added called NewStdLogAt. It has same behaviour as NewStdLog, but it allows providing log level to use by returned *log.Logger.
With this change new function is added called NewStdLogAt. It has same behaviour as NewStdLog, but it allows providing log level to use by returned *log.Logger.
This is (finally) resolved! It'll be included in the upcoming 1.7 release. |
I appreciate you. |
For things like
net/http#Server.ErrorLog
, it might make sense for all log lines to be at the error level. Since we can add fields and/or name the logger it's not a huge deal to pick these lines out of stream, but it kind of reduces the utility of levels as a catch all this-is-something-that-should-be-seen-by-a-human field.Something to consider? Understand if it's too minor of a change, honestly having no issues working around it today.
The text was updated successfully, but these errors were encountered: