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