You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.2 KiB
69 lines
1.2 KiB
package logrus
|
|
|
|
import (
|
|
"github.com/go-kratos/kratos/v2/log"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var _ log.Logger = (*Logger)(nil)
|
|
|
|
type Logger struct {
|
|
log *logrus.Logger
|
|
}
|
|
|
|
func NewLogger(logger *logrus.Logger) log.Logger {
|
|
return &Logger{
|
|
log: logger,
|
|
}
|
|
}
|
|
|
|
func (l *Logger) Log(level log.Level, keyvals ...interface{}) (err error) {
|
|
var (
|
|
logrusLevel logrus.Level
|
|
fields logrus.Fields = make(map[string]interface{})
|
|
msg string
|
|
)
|
|
|
|
switch level {
|
|
case log.LevelDebug:
|
|
logrusLevel = logrus.DebugLevel
|
|
case log.LevelInfo:
|
|
logrusLevel = logrus.InfoLevel
|
|
case log.LevelWarn:
|
|
logrusLevel = logrus.WarnLevel
|
|
case log.LevelError:
|
|
logrusLevel = logrus.ErrorLevel
|
|
default:
|
|
logrusLevel = logrus.DebugLevel
|
|
}
|
|
|
|
if logrusLevel > l.log.Level {
|
|
return
|
|
}
|
|
|
|
if len(keyvals) == 0 {
|
|
return nil
|
|
}
|
|
if len(keyvals)%2 != 0 {
|
|
keyvals = append(keyvals, "")
|
|
}
|
|
for i := 0; i < len(keyvals); i += 2 {
|
|
key, ok := keyvals[i].(string)
|
|
if !ok {
|
|
continue
|
|
}
|
|
if key == logrus.FieldKeyMsg {
|
|
msg, _ = keyvals[i+1].(string)
|
|
continue
|
|
}
|
|
fields[key] = keyvals[i+1]
|
|
}
|
|
|
|
if len(fields) > 0 {
|
|
l.log.WithFields(fields).Log(logrusLevel, msg)
|
|
} else {
|
|
l.log.Log(logrusLevel, msg)
|
|
}
|
|
|
|
return
|
|
}
|
|
|