diff --git a/internal/logutil/logutil.go b/internal/logutil/logutil.go index 522a5aee77..85ccb17ec6 100644 --- a/internal/logutil/logutil.go +++ b/internal/logutil/logutil.go @@ -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() +} diff --git a/internal/logutil/logutil_test.go b/internal/logutil/logutil_test.go deleted file mode 100644 index 1e5f0ba8fc..0000000000 --- a/internal/logutil/logutil_test.go +++ /dev/null @@ -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("") -}