mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-24 03:46:37 +02:00
parent
6c70049d2f
commit
48851a1ffd
@ -40,13 +40,14 @@ require (
|
||||
go.opentelemetry.io/otel/sdk v0.20.0
|
||||
go.opentelemetry.io/otel/trace v0.20.0
|
||||
go.uber.org/multierr v1.7.0 // indirect
|
||||
go.uber.org/zap v1.17.0 // indirect
|
||||
go.uber.org/zap v1.17.0
|
||||
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
|
||||
google.golang.org/genproto v0.0.0-20210524171403-669157292da3
|
||||
google.golang.org/grpc v1.38.0
|
||||
google.golang.org/protobuf v1.26.0
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
||||
gopkg.in/ini.v1 v1.62.0 // indirect
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
)
|
||||
|
||||
|
@ -744,6 +744,8 @@ gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno=
|
||||
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU=
|
||||
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
|
||||
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||
|
53
examples/log/zap.go
Normal file
53
examples/log/zap.go
Normal file
@ -0,0 +1,53 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/go-kratos/kratos/v2/log"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
var _ log.Logger = (*ZapLogger)(nil)
|
||||
|
||||
type ZapLogger struct {
|
||||
log *zap.Logger
|
||||
Sync func() error
|
||||
}
|
||||
|
||||
// NewZapLogger return ZapLogger
|
||||
func NewZapLogger(encoder zapcore.EncoderConfig, level zap.AtomicLevel, opts ...zap.Option) *ZapLogger {
|
||||
core := zapcore.NewCore(
|
||||
zapcore.NewConsoleEncoder(encoder),
|
||||
zapcore.NewMultiWriteSyncer(
|
||||
zapcore.AddSync(os.Stdout),
|
||||
), level)
|
||||
zapLogger := zap.New(core, opts...)
|
||||
return &ZapLogger{log: zapLogger, Sync: zapLogger.Sync}
|
||||
}
|
||||
|
||||
// Log Implementation of logger interface
|
||||
func (l *ZapLogger) 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
|
||||
}
|
||||
|
||||
// Zap.Field is used when keyvals pairs appear
|
||||
var data []zap.Field
|
||||
for i := 0; i < len(keyvals); i += 2 {
|
||||
data = append(data, zap.Any(fmt.Sprint(keyvals[i]), fmt.Sprint(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...)
|
||||
}
|
||||
return nil
|
||||
}
|
39
examples/log/zap_test.go
Normal file
39
examples/log/zap_test.go
Normal file
@ -0,0 +1,39 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/go-kratos/kratos/v2/log"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
func TestZapLogger(t *testing.T) {
|
||||
encoder := zapcore.EncoderConfig{
|
||||
TimeKey: "t",
|
||||
LevelKey: "level",
|
||||
NameKey: "logger",
|
||||
CallerKey: "caller",
|
||||
MessageKey: "msg",
|
||||
StacktraceKey: "stack",
|
||||
EncodeTime: zapcore.ISO8601TimeEncoder,
|
||||
LineEnding: zapcore.DefaultLineEnding,
|
||||
EncodeLevel: zapcore.LowercaseLevelEncoder,
|
||||
EncodeDuration: zapcore.SecondsDurationEncoder,
|
||||
EncodeCaller: zapcore.FullCallerEncoder,
|
||||
}
|
||||
logger := NewZapLogger(
|
||||
encoder,
|
||||
zap.NewAtomicLevelAt(zapcore.DebugLevel),
|
||||
zap.AddStacktrace(
|
||||
zap.NewAtomicLevelAt(zapcore.ErrorLevel)),
|
||||
zap.AddCaller(),
|
||||
zap.AddCallerSkip(2),
|
||||
zap.Development(),
|
||||
)
|
||||
zlog := log.NewHelper(logger)
|
||||
zlog.Infow("name", "kratos", "from", "opensource")
|
||||
zlog.Infow("name", "kratos", "from")
|
||||
|
||||
defer logger.Sync()
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user