blob: 300363cd25bab2b855817bbf1c17ba43dcc9a8ba [file] [log] [blame]
Abhay Kumara2ae5992025-11-10 14:02:24 +00001// Copyright 2016 The etcd Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package clientv3
16
17import (
18 "log"
19 "os"
20
21 "go.uber.org/zap/zapcore"
22 "go.uber.org/zap/zapgrpc"
23 "google.golang.org/grpc/grpclog"
24
25 "go.etcd.io/etcd/client/pkg/v3/logutil"
26)
27
28func init() {
29 // We override grpc logger only when the environment variable is set
30 // in order to not interfere by default with user's code or other libraries.
31 if os.Getenv("ETCD_CLIENT_DEBUG") != "" {
32 lg, err := logutil.CreateDefaultZapLogger(etcdClientDebugLevel())
33 if err != nil {
34 panic(err)
35 }
36 lg = lg.Named("etcd-client")
37 grpclog.SetLoggerV2(zapgrpc.NewLogger(lg))
38 }
39}
40
41// SetLogger sets grpc logger.
42//
43// Deprecated: use grpclog.SetLoggerV2 directly or grpc_zap.ReplaceGrpcLoggerV2.
44func SetLogger(l grpclog.LoggerV2) {
45 grpclog.SetLoggerV2(l)
46}
47
48// etcdClientDebugLevel translates ETCD_CLIENT_DEBUG into zap log level.
49func etcdClientDebugLevel() zapcore.Level {
50 envLevel := os.Getenv("ETCD_CLIENT_DEBUG")
51 if envLevel == "" || envLevel == "true" {
52 return zapcore.InfoLevel
53 }
54 var l zapcore.Level
55 if err := l.Set(envLevel); err != nil {
56 log.Print("Invalid value for environment variable 'ETCD_CLIENT_DEBUG'. Using default level: 'info'")
57 return zapcore.InfoLevel
58 }
59 return l
60}