1 개요[ | ]
- sigs.k8s.io/controller-runtime/pkg/log/zap
Go
Copy
package main
import (
uberzap "go.uber.org/zap"
"go.uber.org/zap/zapcore"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
func main() {
encoderConfig := zapcore.EncoderConfig{
TimeKey: "T",
LevelKey: "L",
NameKey: "N",
CallerKey: zapcore.OmitKey,
MessageKey: "msg",
StacktraceKey: zapcore.OmitKey,
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
cfg := uberzap.Config{
Encoding: "console",
EncoderConfig: encoderConfig,
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
Level: uberzap.NewAtomicLevelAt(uberzap.InfoLevel),
}
// Build the customized zap.Logger
logger, err := cfg.Build()
if err != nil {
panic(err)
}
defer logger.Sync() // Ensure logs are flushed
// Create a controller-runtime logger leveraging the zap logger with a custom configuration
ctrl.SetLogger(zap.New(func(o *zap.Options) {
o.Development = true
o.ZapOpts = append(o.ZapOpts, uberzap.WrapCore(func(core zapcore.Core) zapcore.Core {
return logger.Core()
}))
}))
// Use controller-runtime logging
ctrl.Log.Info("Something noteworthy happened!", "context", "example")
}
2 같이 보기[ | ]
편집자 Jmnote
로그인하시면 댓글을 쓸 수 있습니다.