mirror of
https://gitee.com/milvus-io/milvus.git
synced 2026-01-07 19:31:51 +08:00
Log using context to print more information (#12886)
We need to relate log info with module name and requestID. So I save the logger with these fields in context. And we can pass the context through the call stack. Signed-off-by: sunby <bingyi.sun@zilliz.com> Co-authored-by: sunby <bingyi.sun@zilliz.com>
This commit is contained in:
parent
006a095370
commit
45ba8b5ba1
@ -17,6 +17,7 @@
|
||||
package logutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
@ -147,3 +148,52 @@ func SetupLogger(cfg *log.Config) {
|
||||
func GetZapWrapper() *zapWrapper {
|
||||
return _globalZapWrapper.Load().(*zapWrapper)
|
||||
}
|
||||
|
||||
type logKey int
|
||||
|
||||
const logCtxKey logKey = iota
|
||||
|
||||
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)))
|
||||
}
|
||||
|
||||
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)))
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
@ -1,63 +0,0 @@
|
||||
// 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
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// 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.
|
||||
|
||||
// Copyright 2019 PingCAP, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// 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.
|
||||
|
||||
package logutil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/milvus-io/milvus/internal/log"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestZapWrapper(t *testing.T) {
|
||||
logCfg := &log.Config{}
|
||||
SetupLogger(logCfg)
|
||||
|
||||
wrapper := GetZapWrapper()
|
||||
assert.NotNil(t, wrapper)
|
||||
|
||||
ret := wrapper.V(3)
|
||||
assert.True(t, ret)
|
||||
|
||||
wrapper.Info()
|
||||
wrapper.Infoln()
|
||||
wrapper.Infof("")
|
||||
wrapper.Warning()
|
||||
wrapper.Warningln()
|
||||
wrapper.Warningf("")
|
||||
wrapper.Error()
|
||||
wrapper.Errorln()
|
||||
wrapper.Errorf("")
|
||||
|
||||
// wrapper.Fatal()
|
||||
// wrapper.Fatalln()
|
||||
// wrapper.Fatalf("")
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user