github.com/Hnampk/fabric@v2.1.1+incompatible/gossip/util/logging.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package util
     8  
     9  import (
    10  	"sync"
    11  
    12  	"github.com/hyperledger/fabric/common/flogging"
    13  	"go.uber.org/zap/zapcore"
    14  )
    15  
    16  // Logger names for logger initialization.
    17  const (
    18  	ChannelLogger     = "gossip.channel"
    19  	CommLogger        = "gossip.comm"
    20  	DiscoveryLogger   = "gossip.discovery"
    21  	ElectionLogger    = "gossip.election"
    22  	GossipLogger      = "gossip.gossip"
    23  	CommMockLogger    = "gossip.comm.mock"
    24  	PullLogger        = "gossip.pull"
    25  	ServiceLogger     = "gossip.service"
    26  	StateLogger       = "gossip.state"
    27  	PrivateDataLogger = "gossip.privdata"
    28  )
    29  
    30  var loggers = make(map[string]Logger)
    31  var lock = sync.Mutex{}
    32  var testMode bool
    33  
    34  // defaultTestSpec is the default logging level for gossip tests
    35  var defaultTestSpec = "WARNING"
    36  
    37  type Logger interface {
    38  	Debug(args ...interface{})
    39  	Debugf(format string, args ...interface{})
    40  	Error(args ...interface{})
    41  	Errorf(format string, args ...interface{})
    42  	Fatal(args ...interface{})
    43  	Fatalf(format string, args ...interface{})
    44  	Info(args ...interface{})
    45  	Infof(format string, args ...interface{})
    46  	Panic(args ...interface{})
    47  	Panicf(format string, args ...interface{})
    48  	Warning(args ...interface{})
    49  	Warningf(format string, args ...interface{})
    50  	IsEnabledFor(l zapcore.Level) bool
    51  	With(args ...interface{}) *flogging.FabricLogger
    52  }
    53  
    54  // GetLogger returns a logger for given gossip logger name and peerID
    55  func GetLogger(name string, peerID string) Logger {
    56  	if peerID != "" && testMode {
    57  		name = name + "#" + peerID
    58  	}
    59  
    60  	lock.Lock()
    61  	defer lock.Unlock()
    62  
    63  	if lgr, ok := loggers[name]; ok {
    64  		return lgr
    65  	}
    66  
    67  	// Logger doesn't exist, create a new one
    68  	lgr := flogging.MustGetLogger(name)
    69  	loggers[name] = lgr
    70  	return lgr
    71  }
    72  
    73  // SetupTestLogging sets the default log levels for gossip unit tests to defaultTestSpec
    74  func SetupTestLogging() {
    75  	SetupTestLoggingWithLevel(defaultTestSpec)
    76  }
    77  
    78  // SetupTestLoggingWithLevel sets the default log levels for gossip unit tests to level
    79  func SetupTestLoggingWithLevel(level string) {
    80  	testMode = true
    81  	flogging.ActivateSpec(level)
    82  }