package logrus import ( "github.com/sirupsen/logrus" "github.com/go-kratos/kratos/v2/log" ) 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 case log.LevelFatal: logrusLevel = logrus.FatalLevel 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 }