Debian Codegraph @

otelcol/internal/grpclog/logger.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package grpclog // import "go.opentelemetry.io/collector/otelcol/internal/grpclog"
5
6 import (
7 "go.uber.org/zap"
8 "go.uber.org/zap/zapcore"
9 "go.uber.org/zap/zapgrpc"
10 "google.golang.org/grpc/grpclog"
11 )
12
13 // SetLogger constructs a zapgrpc.Logger instance, and installs it as grpc logger, cloned from baseLogger with
14 // exact configuration. The minimum level of gRPC logs is set to WARN should the loglevel of the collector is set to
15 // INFO to avoid copious logging from grpc framework.
16 func SetLogger(baseLogger *zap.Logger) *zapgrpc.Logger {
17 logger := zapgrpc.NewLogger(baseLogger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
18 var c zapcore.Core
19 var err error
20 loglevel := baseLogger.Level()
21 if loglevel == zapcore.InfoLevel {
22 loglevel = zapcore.WarnLevel
23 }
24 // NewIncreaseLevelCore errors only if the new log level is less than the initial core level.
25 c, err = zapcore.NewIncreaseLevelCore(core, loglevel)
26 // In case of an error changing the level, move on, this happens when using the NopCore
27 if err != nil {
28 c = core
29 }
30 return c.With([]zapcore.Field{zap.Bool("grpc_log", true)})
31 }), zap.AddCallerSkip(5)))
32
33 grpclog.SetLoggerV2(logger)
34 return logger
35 }