2021-11-03 19:25:37 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
2021-04-19 11:35:38 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-11-03 19:25:37 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 11:35:38 +08:00
|
|
|
//
|
2021-11-03 19:25:37 +08:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2021-10-12 19:52:43 +08:00
|
|
|
|
2021-03-04 16:01:30 +08:00
|
|
|
package logutil
|
2021-02-19 15:37:04 +08:00
|
|
|
|
|
|
|
import (
|
2021-12-15 20:35:09 +08:00
|
|
|
"context"
|
2021-02-19 15:37:04 +08:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
2021-03-04 16:01:30 +08:00
|
|
|
|
2021-02-19 15:37:04 +08:00
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/grpclog"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type zapWrapper struct {
|
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
2021-11-30 19:45:41 +08:00
|
|
|
// Info logs a message at InfoLevel.
|
2021-02-19 15:37:04 +08:00
|
|
|
func (w *zapWrapper) Info(args ...interface{}) {
|
|
|
|
w.logger.WithOptions(zap.AddCallerSkip(1)).Sugar().Info(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *zapWrapper) Infoln(args ...interface{}) {
|
|
|
|
w.logger.WithOptions(zap.AddCallerSkip(1)).Sugar().Info(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w zapWrapper) Infof(format string, args ...interface{}) {
|
|
|
|
w.logger.WithOptions(zap.AddCallerSkip(1)).Sugar().Infof(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w zapWrapper) Warning(args ...interface{}) {
|
|
|
|
w.logger.WithOptions(zap.AddCallerSkip(1)).Sugar().Warn(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w zapWrapper) Warningln(args ...interface{}) {
|
|
|
|
w.logger.WithOptions(zap.AddCallerSkip(1)).Sugar().Warn(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *zapWrapper) Warningf(format string, args ...interface{}) {
|
|
|
|
w.logger.WithOptions(zap.AddCallerSkip(1)).Sugar().Warnf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w zapWrapper) Error(args ...interface{}) {
|
|
|
|
w.logger.WithOptions(zap.AddCallerSkip(1)).Sugar().Error(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *zapWrapper) Errorln(args ...interface{}) {
|
|
|
|
w.logger.WithOptions(zap.AddCallerSkip(1)).Sugar().Error(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w zapWrapper) Errorf(format string, args ...interface{}) {
|
|
|
|
w.logger.WithOptions(zap.AddCallerSkip(1)).Sugar().Errorf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *zapWrapper) Fatal(args ...interface{}) {
|
|
|
|
w.logger.WithOptions(zap.AddCallerSkip(1)).Sugar().Fatal(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w zapWrapper) Fatalln(args ...interface{}) {
|
|
|
|
w.logger.WithOptions(zap.AddCallerSkip(1)).Sugar().Fatal(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *zapWrapper) Fatalf(format string, args ...interface{}) {
|
|
|
|
w.logger.WithOptions(zap.AddCallerSkip(1)).Sugar().Fatalf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// V reports whether verbosity level l is at least the requested verbose level.
|
|
|
|
// grpc LoggerV2
|
|
|
|
// 0=info, 1=warning, 2=error, 3=fatal
|
|
|
|
// zap
|
|
|
|
// -1=debug, 0=info, 1=warning, 2=error, 3=dpanic, 4=panic, 5=fatal
|
|
|
|
func (w *zapWrapper) V(l int) bool {
|
|
|
|
zapLevel := l
|
|
|
|
if l == 3 {
|
|
|
|
zapLevel = 5
|
|
|
|
}
|
|
|
|
return w.logger.Core().Enabled(zapcore.Level(zapLevel))
|
|
|
|
}
|
|
|
|
|
2021-03-04 16:01:30 +08:00
|
|
|
// LogPanic logs the panic reason and stack, then exit the process.
|
|
|
|
// Commonly used with a `defer`.
|
|
|
|
func LogPanic() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
log.Fatal("panic", zap.Reflect("recover", e))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 15:37:04 +08:00
|
|
|
var once sync.Once
|
|
|
|
var _globalZapWrapper atomic.Value
|
|
|
|
|
|
|
|
const defaultLogLevel = "info"
|
|
|
|
|
|
|
|
func init() {
|
2021-03-04 16:01:30 +08:00
|
|
|
conf := &log.Config{Level: defaultLogLevel, File: log.FileLogConfig{}}
|
|
|
|
lg, _, _ := log.InitLogger(conf)
|
2021-02-19 15:37:04 +08:00
|
|
|
_globalZapWrapper.Store(&zapWrapper{
|
|
|
|
logger: lg,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-12 19:52:43 +08:00
|
|
|
// SetupLogger is used to initialize the log with config.
|
2021-03-04 16:01:30 +08:00
|
|
|
func SetupLogger(cfg *log.Config) {
|
2021-02-19 15:37:04 +08:00
|
|
|
once.Do(func() {
|
|
|
|
// initialize logger
|
2021-03-04 16:01:30 +08:00
|
|
|
logger, p, err := log.InitLogger(cfg, zap.AddStacktrace(zap.ErrorLevel))
|
2021-02-19 15:37:04 +08:00
|
|
|
if err == nil {
|
2021-03-04 16:01:30 +08:00
|
|
|
log.ReplaceGlobals(logger, p)
|
2021-02-19 15:37:04 +08:00
|
|
|
} else {
|
2021-03-04 16:01:30 +08:00
|
|
|
log.Fatal("initialize logger error", zap.Error(err))
|
2021-02-19 15:37:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// initialize grpc and etcd logger
|
|
|
|
c := *cfg
|
|
|
|
c.Level = defaultLogLevel
|
2021-03-04 16:01:30 +08:00
|
|
|
lg, _, err := log.InitLogger(&c)
|
2021-02-19 15:37:04 +08:00
|
|
|
if err != nil {
|
2021-03-04 16:01:30 +08:00
|
|
|
log.Fatal("initialize grpc/etcd logger error", zap.Error(err))
|
2021-02-19 15:37:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
wrapper := &zapWrapper{lg}
|
|
|
|
grpclog.SetLoggerV2(wrapper)
|
|
|
|
_globalZapWrapper.Store(wrapper)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-12 19:52:43 +08:00
|
|
|
// GetZapWrapper returns the stored zapWrapper object.
|
2021-02-19 15:37:04 +08:00
|
|
|
func GetZapWrapper() *zapWrapper {
|
|
|
|
return _globalZapWrapper.Load().(*zapWrapper)
|
|
|
|
}
|
2021-12-15 20:35:09 +08:00
|
|
|
|
|
|
|
type logKey int
|
|
|
|
|
|
|
|
const logCtxKey logKey = iota
|
|
|
|
|
2021-12-31 14:25:52 +08:00
|
|
|
// WithField adds given kv field to the logger in ctx
|
2021-12-15 20:35:09 +08:00
|
|
|
func WithField(ctx context.Context, key string, value string) context.Context {
|
|
|
|
logger := log.L()
|
|
|
|
if ctxLogger, ok := ctx.Value(logCtxKey).(*zap.Logger); ok {
|
|
|
|
logger = ctxLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
return context.WithValue(ctx, logCtxKey, logger.With(zap.String(key, value)))
|
|
|
|
}
|
|
|
|
|
2021-12-31 14:25:52 +08:00
|
|
|
// WithReqID adds given reqID field to the logger in ctx
|
2021-12-15 20:35:09 +08:00
|
|
|
func WithReqID(ctx context.Context, reqID int64) context.Context {
|
|
|
|
logger := log.L()
|
|
|
|
if ctxLogger, ok := ctx.Value(logCtxKey).(*zap.Logger); ok {
|
|
|
|
logger = ctxLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
return context.WithValue(ctx, logCtxKey, logger.With(zap.Int64("reqID", reqID)))
|
|
|
|
}
|
|
|
|
|
2021-12-31 14:25:52 +08:00
|
|
|
// WithModule adds given module field to the logger in ctx
|
2021-12-15 20:35:09 +08:00
|
|
|
func WithModule(ctx context.Context, module string) context.Context {
|
|
|
|
logger := log.L()
|
|
|
|
if ctxLogger, ok := ctx.Value(logCtxKey).(*zap.Logger); ok {
|
|
|
|
logger = ctxLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
return context.WithValue(ctx, logCtxKey, logger.With(zap.String("module", module)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithLogger(ctx context.Context, logger *zap.Logger) context.Context {
|
|
|
|
if logger == nil {
|
|
|
|
logger = log.L()
|
|
|
|
}
|
|
|
|
return context.WithValue(ctx, logCtxKey, logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Logger(ctx context.Context) *zap.Logger {
|
|
|
|
if ctxLogger, ok := ctx.Value(logCtxKey).(*zap.Logger); ok {
|
|
|
|
return ctxLogger
|
|
|
|
}
|
|
|
|
return log.L()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BgLogger() *zap.Logger {
|
|
|
|
return log.L()
|
|
|
|
}
|