...

Source file src/go.etcd.io/etcd/client/v3/logger.go

Documentation: go.etcd.io/etcd/client/v3

     1  // 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  
    15  package clientv3
    16  
    17  import (
    18  	"log"
    19  	"os"
    20  
    21  	"go.etcd.io/etcd/client/pkg/v3/logutil"
    22  	"go.uber.org/zap/zapcore"
    23  	"go.uber.org/zap/zapgrpc"
    24  	"google.golang.org/grpc/grpclog"
    25  )
    26  
    27  func init() {
    28  	// We override grpc logger only when the environment variable is set
    29  	// in order to not interfere by default with user's code or other libraries.
    30  	if os.Getenv("ETCD_CLIENT_DEBUG") != "" {
    31  		lg, err := logutil.CreateDefaultZapLogger(etcdClientDebugLevel())
    32  		if err != nil {
    33  			panic(err)
    34  		}
    35  		lg = lg.Named("etcd-client")
    36  		grpclog.SetLoggerV2(zapgrpc.NewLogger(lg))
    37  	}
    38  }
    39  
    40  // SetLogger sets grpc logger.
    41  //
    42  // Deprecated: use grpclog.SetLoggerV2 directly or grpc_zap.ReplaceGrpcLoggerV2.
    43  func SetLogger(l grpclog.LoggerV2) {
    44  	grpclog.SetLoggerV2(l)
    45  }
    46  
    47  // etcdClientDebugLevel translates ETCD_CLIENT_DEBUG into zap log level.
    48  func etcdClientDebugLevel() zapcore.Level {
    49  	envLevel := os.Getenv("ETCD_CLIENT_DEBUG")
    50  	if envLevel == "" || envLevel == "true" {
    51  		return zapcore.InfoLevel
    52  	}
    53  	var l zapcore.Level
    54  	if err := l.Set(envLevel); err != nil {
    55  		log.Printf("Invalid value for environment variable 'ETCD_CLIENT_DEBUG'. Using default level: 'info'")
    56  		return zapcore.InfoLevel
    57  	}
    58  	return l
    59  }
    60  

View as plain text