Add logrus example (#1124)
* add logrus logger example Co-authored-by: Lining Guan <lining.guan@grabtaxi.com>pull/1128/head
parent
545ffd1084
commit
09be032575
@ -0,0 +1,100 @@ |
||||
package log |
||||
|
||||
import ( |
||||
"io" |
||||
"os" |
||||
|
||||
"github.com/go-kratos/kratos/v2/log" |
||||
"github.com/sirupsen/logrus" |
||||
) |
||||
|
||||
var _ log.Logger = (*LogrusLogger)(nil) |
||||
|
||||
type LogrusLogger struct { |
||||
log *logrus.Logger |
||||
} |
||||
|
||||
func NewLogrusLogger(options ...Option) log.Logger { |
||||
logger := logrus.New() |
||||
logger.Level = logrus.DebugLevel |
||||
logger.Out = os.Stdout |
||||
logger.Formatter = &logrus.JSONFormatter{} |
||||
for _, option := range options { |
||||
option(logger) |
||||
} |
||||
|
||||
return &LogrusLogger{ |
||||
log: logger, |
||||
} |
||||
} |
||||
|
||||
func (l *LogrusLogger) 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 |
||||
} |
||||
|
||||
type Option func(log *logrus.Logger) |
||||
|
||||
func Level(level logrus.Level) Option { |
||||
return func(log *logrus.Logger) { |
||||
log.Level = level |
||||
} |
||||
} |
||||
|
||||
func Output(w io.Writer) Option { |
||||
return func(log *logrus.Logger) { |
||||
log.Out = w |
||||
} |
||||
} |
||||
|
||||
func Formatter(formatter logrus.Formatter) Option { |
||||
return func(log *logrus.Logger) { |
||||
log.Formatter = formatter |
||||
} |
||||
} |
@ -0,0 +1,35 @@ |
||||
package log |
||||
|
||||
import ( |
||||
"bytes" |
||||
"strings" |
||||
"testing" |
||||
|
||||
"github.com/go-kratos/kratos/v2/log" |
||||
"github.com/sirupsen/logrus" |
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestLoggerLog(t *testing.T) { |
||||
tests := map[string]struct { |
||||
level logrus.Level |
||||
formatter logrus.Formatter |
||||
logLevel log.Level |
||||
kvs []interface{} |
||||
want string |
||||
}{ |
||||
"json format": {level: logrus.InfoLevel, formatter: &logrus.JSONFormatter{}, logLevel: log.LevelInfo, kvs: []interface{}{"case", "json format", "msg", "1"}, want: `{"case":"json format","level":"info","msg":"1"`}, |
||||
"level unmatch": {level: logrus.InfoLevel, formatter: &logrus.JSONFormatter{}, logLevel: log.LevelDebug, kvs: []interface{}{"case", "level unmatch", "msg", "1"}, want: ""}, |
||||
"no tags": {level: logrus.InfoLevel, formatter: &logrus.JSONFormatter{}, logLevel: log.LevelInfo, kvs: []interface{}{"msg", "1"}, want: `{"level":"info","msg":"1"`}, |
||||
} |
||||
|
||||
for name, test := range tests { |
||||
t.Run(name, func(t *testing.T) { |
||||
output := new(bytes.Buffer) |
||||
logger := NewLogrusLogger(Level(test.level), Formatter(test.formatter), Output(output)) |
||||
_ = logger.Log(test.logLevel, test.kvs...) |
||||
|
||||
assert.True(t, strings.HasPrefix(output.String(), test.want)) |
||||
}) |
||||
} |
||||
} |
Loading…
Reference in new issue