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.
53 lines
956 B
53 lines
956 B
2 years ago
|
package zap
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"github.com/go-kratos/kratos/v2/log"
|
||
|
"go.uber.org/zap"
|
||
|
)
|
||
|
|
||
|
var _ log.Logger = (*Logger)(nil)
|
||
|
|
||
|
type Logger struct {
|
||
|
log *zap.Logger
|
||
|
}
|
||
|
|
||
|
func NewLogger(zlog *zap.Logger) *Logger {
|
||
|
return &Logger{zlog}
|
||
|
}
|
||
|
|
||
|
func (l *Logger) Log(level log.Level, keyvals ...interface{}) error {
|
||
|
if len(keyvals) == 0 || len(keyvals)%2 != 0 {
|
||
|
l.log.Warn(fmt.Sprint("Keyvalues must appear in pairs: ", keyvals))
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
var data []zap.Field
|
||
|
for i := 0; i < len(keyvals); i += 2 {
|
||
|
data = append(data, zap.Any(fmt.Sprint(keyvals[i]), keyvals[i+1]))
|
||
|
}
|
||
|
|
||
|
switch level {
|
||
|
case log.LevelDebug:
|
||
|
l.log.Debug("", data...)
|
||
|
case log.LevelInfo:
|
||
|
l.log.Info("", data...)
|
||
|
case log.LevelWarn:
|
||
|
l.log.Warn("", data...)
|
||
|
case log.LevelError:
|
||
|
l.log.Error("", data...)
|
||
|
case log.LevelFatal:
|
||
|
l.log.Fatal("", data...)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (l *Logger) Sync() error {
|
||
|
return l.log.Sync()
|
||
|
}
|
||
|
|
||
|
func (l *Logger) Close() error {
|
||
|
return l.Sync()
|
||
|
}
|