Better log level handling within micro

Currently every log message from the micro internals are logged with the info
level, we really need to respect the proper defined log level within our log
wrapper package.
This commit is contained in:
Thomas Boerger
2019-12-06 09:33:03 +01:00
parent 1c6919d5fc
commit ecf44c15e0
2 changed files with 76 additions and 15 deletions

View File

@@ -0,0 +1,7 @@
Change: Better log level handling within micro
Currently every log message from the micro internals are logged with the info
level, we really need to respect the proper defined log level within our log
wrapper package.
https://github.com/owncloud/ocis-pkg/issues/2

View File

@@ -1,7 +1,9 @@
package log
import (
"fmt"
"os"
"runtime"
"strings"
"time"
@@ -22,18 +24,28 @@ func NewLogger(opts ...Option) Logger {
switch strings.ToLower(options.Level) {
case "panic":
zerolog.SetGlobalLevel(zerolog.PanicLevel)
mlog.SetLevel(mlog.LevelFatal)
case "fatal":
zerolog.SetGlobalLevel(zerolog.FatalLevel)
mlog.SetLevel(mlog.LevelFatal)
case "error":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
mlog.SetLevel(mlog.LevelError)
case "warn":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
mlog.SetLevel(mlog.LevelWarn)
case "info":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
mlog.SetLevel(mlog.LevelInfo)
case "debug":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
mlog.SetLevel(mlog.LevelDebug)
case "trace":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
mlog.SetLevel(mlog.LevelTrace)
default:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
mlog.SetLevel(mlog.LevelInfo)
}
var logger zerolog.Logger
@@ -58,8 +70,8 @@ func NewLogger(opts ...Option) Logger {
Logger()
mlog.SetLogger(
logWrapper{
logger,
microZerolog{
logger: logger,
},
)
@@ -68,23 +80,65 @@ func NewLogger(opts ...Option) Logger {
}
}
// logWrapper implements the required interface for the go-micro logger.
type logWrapper struct {
// microZerolog implements the required interface for the go-micro logger.
type microZerolog struct {
logger zerolog.Logger
}
// Log makes use of github.com/go-log/log.Log
func (w logWrapper) Log(v ...interface{}) {
tmp := make([]string, len(v))
// Log makes use of github.com/go-log/log.Log.
func (mz microZerolog) Log(v ...interface{}) {
pc := parentCaller()
msg := fmt.Sprint(v...)
for _, row := range v {
tmp = append(tmp, row.(string))
switch {
case strings.HasSuffix(pc, "Fatal"):
mz.logger.Fatal().Msg(msg)
case strings.HasSuffix(pc, "Error"):
mz.logger.Error().Msg(msg)
case strings.HasSuffix(pc, "Info"):
mz.logger.Info().Msg(msg)
case strings.HasSuffix(pc, "Warn"):
mz.logger.Warn().Msg(msg)
case strings.HasSuffix(pc, "Debug"):
mz.logger.Debug().Msg(msg)
case strings.HasSuffix(pc, "Trace"):
mz.logger.Debug().Msg(msg)
default:
mz.logger.Info().Msg(msg)
}
}
// Logf makes use of github.com/go-log/log.Logf.
func (mz microZerolog) Logf(format string, v ...interface{}) {
pc := parentCaller()
msg := fmt.Sprintf(strings.TrimRight(format, "\n"), v...)
switch {
case strings.HasSuffix(pc, "Fatalf"):
mz.logger.Fatal().Msg(msg)
case strings.HasSuffix(pc, "Errorf"):
mz.logger.Error().Msg(msg)
case strings.HasSuffix(pc, "Infof"):
mz.logger.Info().Msg(msg)
case strings.HasSuffix(pc, "Warnf"):
mz.logger.Warn().Msg(msg)
case strings.HasSuffix(pc, "Debugf"):
mz.logger.Debug().Msg(msg)
case strings.HasSuffix(pc, "Tracef"):
mz.logger.Debug().Msg(msg)
default:
mz.logger.Info().Msg(msg)
}
}
// parentCaller tries to detect which log method had been invoked.
func parentCaller() string {
pc, _, _, ok := runtime.Caller(4)
fn := runtime.FuncForPC(pc)
if ok && fn != nil {
return fn.Name()
}
w.logger.Info().Msg(strings.Join(tmp, " "))
}
// Logf makes use of github.com/go-log/log.Logf
func (w logWrapper) Logf(format string, v ...interface{}) {
w.logger.Info().Msgf(strings.TrimRight(format, "\n"), v...)
return ""
}