Skip to content

Commit 7132dd5

Browse files
committed
[FAB-2351] Update loggers to flogging.MustGetLogger
This CR updates all loggers throughout the code base to use `flogging.MustGetLogger`. This function wraps `logging.MustGetLogger` and tracks the logger modules defined in the system. This enables the ability to set log levels for modules using regular expressions. and make it easy to change the levels for any module and all its submodules with a single command (e.g. gossip, ledger, msp). Change-Id: If5d3229ea2312adb56fc21bfdafbed3d967cf1df Signed-off-by: Will Lahti <[email protected]>
1 parent 95d13d2 commit 7132dd5

File tree

69 files changed

+191
-195
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+191
-195
lines changed

bccsp/factory/factory.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"sync"
2121

2222
"github.com/hyperledger/fabric/bccsp"
23-
"github.com/op/go-logging"
23+
"github.com/hyperledger/fabric/common/flogging"
2424
)
2525

2626
var (
@@ -41,7 +41,7 @@ var (
4141
// Factories' Initialization Error
4242
factoriesInitError error
4343

44-
logger = logging.MustGetLogger("bccsp")
44+
logger = flogging.MustGetLogger("bccsp")
4545
)
4646

4747
// BCCSPFactory is used to get instances of the BCCSP interface.

bccsp/pkcs11/impl.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ import (
3232

3333
"github.com/hyperledger/fabric/bccsp"
3434
"github.com/hyperledger/fabric/bccsp/utils"
35+
"github.com/hyperledger/fabric/common/flogging"
3536
"github.com/miekg/pkcs11"
36-
"github.com/op/go-logging"
37+
3738
"golang.org/x/crypto/sha3"
3839
)
3940

4041
var (
41-
logger = logging.MustGetLogger("bccsp_p11")
42+
logger = flogging.MustGetLogger("bccsp_p11")
4243
sessionCacheSize = 10
4344
)
4445

bccsp/sw/impl.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ import (
3636

3737
"github.com/hyperledger/fabric/bccsp"
3838
"github.com/hyperledger/fabric/bccsp/utils"
39-
"github.com/op/go-logging"
39+
"github.com/hyperledger/fabric/common/flogging"
4040
"golang.org/x/crypto/sha3"
4141
)
4242

4343
var (
44-
logger = logging.MustGetLogger("bccsp_sw")
44+
logger = flogging.MustGetLogger("bccsp_sw")
4545
)
4646

4747
// NewDefaultSecurityLevel returns a new instance of the software-based BCCSP

common/cauthdsl/cauthdsl.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ package cauthdsl
1919
import (
2020
"fmt"
2121

22+
"github.com/hyperledger/fabric/common/flogging"
2223
"github.com/hyperledger/fabric/msp"
2324
cb "github.com/hyperledger/fabric/protos/common"
2425
mb "github.com/hyperledger/fabric/protos/msp"
25-
"github.com/op/go-logging"
2626
)
2727

28-
var cauthdslLogger = logging.MustGetLogger("cauthdsl")
28+
var cauthdslLogger = flogging.MustGetLogger("cauthdsl")
2929

3030
// compile recursively builds a go evaluatable function corresponding to the policy specified
3131
func compile(policy *cb.SignaturePolicy, identities []*mb.MSPPrincipal, deserializer msp.IdentityDeserializer) (func([]*cb.SignedData, []bool) bool, error) {

common/config/msp/config_util.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import (
2323
mspprotos "github.com/hyperledger/fabric/protos/msp"
2424
"github.com/hyperledger/fabric/protos/utils"
2525

26-
logging "github.com/op/go-logging"
26+
"github.com/hyperledger/fabric/common/flogging"
2727
)
2828

29-
var logger = logging.MustGetLogger("configvalues/msp")
29+
var logger = flogging.MustGetLogger("configvalues/msp")
3030

3131
const (
3232
// ReadersPolicyKey is the key used for the read policy

common/config/proposer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ import (
2121
"sync"
2222

2323
"github.com/golang/protobuf/proto"
24-
logging "github.com/op/go-logging"
24+
"github.com/hyperledger/fabric/common/flogging"
2525
)
2626

27-
var logger = logging.MustGetLogger("common/config")
27+
var logger = flogging.MustGetLogger("common/config")
2828

2929
// ValueDeserializer provides a mechanism to retrieve proto messages to deserialize config values into
3030
type ValueDeserializer interface {

common/configtx/manager.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@ import (
2222
"regexp"
2323

2424
"github.com/hyperledger/fabric/common/configtx/api"
25+
"github.com/hyperledger/fabric/common/flogging"
2526
cb "github.com/hyperledger/fabric/protos/common"
2627
"github.com/hyperledger/fabric/protos/utils"
27-
28-
logging "github.com/op/go-logging"
2928
)
3029

31-
var logger = logging.MustGetLogger("common/configtx")
30+
var logger = flogging.MustGetLogger("common/configtx")
3231

3332
// Constraints for valid chain IDs
3433
var (

common/configtx/test/helper.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@ import (
2525
"github.com/hyperledger/fabric/common/configtx"
2626
genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
2727
"github.com/hyperledger/fabric/common/configtx/tool/provisional"
28+
"github.com/hyperledger/fabric/common/flogging"
2829
"github.com/hyperledger/fabric/common/genesis"
2930
"github.com/hyperledger/fabric/msp"
3031
cb "github.com/hyperledger/fabric/protos/common"
3132
mspproto "github.com/hyperledger/fabric/protos/msp"
32-
33-
logging "github.com/op/go-logging"
3433
)
3534

36-
var logger = logging.MustGetLogger("common/configtx/test")
35+
var logger = flogging.MustGetLogger("common/configtx/test")
3736

3837
const (
3938
// AcceptAllPolicyKey is the key of the AcceptAllPolicy.

common/configtx/tool/configtxgen/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/hyperledger/fabric/common/configtx"
2828
genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
2929
"github.com/hyperledger/fabric/common/configtx/tool/provisional"
30+
"github.com/hyperledger/fabric/common/flogging"
3031
"github.com/hyperledger/fabric/msp"
3132
cb "github.com/hyperledger/fabric/protos/common"
3233
"github.com/hyperledger/fabric/protos/utils"
@@ -35,7 +36,7 @@ import (
3536
logging "github.com/op/go-logging"
3637
)
3738

38-
var logger = logging.MustGetLogger("common/configtx/tool")
39+
var logger = flogging.MustGetLogger("common/configtx/tool")
3940

4041
func doOutputBlock(pgen provisional.Generator, channelID string, outputBlock string) error {
4142
logger.Info("Generating genesis block")

common/configtx/tool/localconfig/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ import (
2323
"strings"
2424
"time"
2525

26+
"github.com/hyperledger/fabric/common/flogging"
2627
"github.com/hyperledger/fabric/common/viperutil"
2728

28-
"github.com/op/go-logging"
2929
"github.com/spf13/viper"
3030

3131
bccsp "github.com/hyperledger/fabric/bccsp/factory"
3232
)
3333

34-
var logger = logging.MustGetLogger("configtx/tool/localconfig")
34+
var logger = flogging.MustGetLogger("configtx/tool/localconfig")
3535

3636
const (
3737
// SampleInsecureProfile references the sample profile which does not include any MSPs and uses solo for ordering.

common/configtx/tool/provisional/provisional.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,17 @@ import (
2626
configvaluesmsp "github.com/hyperledger/fabric/common/config/msp"
2727
"github.com/hyperledger/fabric/common/configtx"
2828
genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
29+
"github.com/hyperledger/fabric/common/flogging"
2930
"github.com/hyperledger/fabric/common/genesis"
3031
"github.com/hyperledger/fabric/common/policies"
3132
"github.com/hyperledger/fabric/msp"
3233
"github.com/hyperledger/fabric/orderer/common/bootstrap"
3334
cb "github.com/hyperledger/fabric/protos/common"
3435
ab "github.com/hyperledger/fabric/protos/orderer"
3536
pb "github.com/hyperledger/fabric/protos/peer"
36-
37-
logging "github.com/op/go-logging"
3837
)
3938

40-
var logger = logging.MustGetLogger("common/configtx/tool/provisional")
39+
var logger = flogging.MustGetLogger("common/configtx/tool/provisional")
4140

4241
// Generator can either create an orderer genesis block or config template
4342
type Generator interface {

common/flogging/logging.go

+21-18
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ var (
4242

4343
lock sync.RWMutex
4444
once sync.Once
45-
46-
// IsSetLevelByRegExpEnabled allows the setting of log levels using a regular
47-
// expression, instead of one module at a time, when set to true.
48-
// TODO Remove once all other packages have switched to
49-
// `flogging.MustGetLogger` from `logging.MustGetLogger`.
50-
IsSetLevelByRegExpEnabled bool
5145
)
5246

5347
func init() {
@@ -57,7 +51,6 @@ func init() {
5751

5852
// Reset sets to logging to the defaults defined in this package.
5953
func Reset() {
60-
IsSetLevelByRegExpEnabled = false // redundant since the default for booleans is `false` but added for clarity
6154
modules = make(map[string]string)
6255
lock = sync.RWMutex{}
6356

@@ -101,29 +94,29 @@ func GetModuleLevel(module string) string {
10194
// regular expression. Can be used to dynamically change the log level for the
10295
// module.
10396
func SetModuleLevel(moduleRegExp string, level string) (string, error) {
104-
return setModuleLevel(moduleRegExp, level, false)
97+
// special case - `error` module, which is used to enable/disable callstacks
98+
// in error messages, shouldn't use regular expressions
99+
if moduleRegExp == "error" {
100+
return setModuleLevel(moduleRegExp, level, false, false)
101+
}
102+
return setModuleLevel(moduleRegExp, level, true, false)
105103
}
106104

107-
func setModuleLevel(moduleRegExp string, level string, revert bool) (string, error) {
108-
105+
func setModuleLevel(moduleRegExp string, level string, isRegExp bool, revert bool) (string, error) {
109106
var re *regexp.Regexp
110107
logLevel, err := logging.LogLevel(level)
111108
if err != nil {
112109
logger.Warningf("Invalid logging level '%s' - ignored", level)
113110
} else {
114-
// TODO This check is here to preserve the old functionality until all
115-
// other packages switch to `flogging.MustGetLogger` (from
116-
// `logging.MustGetLogger`).
117-
if !IsSetLevelByRegExpEnabled || revert {
118-
logging.SetLevel(logging.Level(logLevel), moduleRegExp)
119-
logger.Debugf("Module '%s' logger enabled for log level '%s'", moduleRegExp, logLevel)
111+
if !isRegExp || revert {
112+
logging.SetLevel(logLevel, moduleRegExp)
113+
logger.Debugf("Module '%s' logger enabled for log level '%s'", moduleRegExp, level)
120114
} else {
121115
re, err = regexp.Compile(moduleRegExp)
122116
if err != nil {
123117
logger.Warningf("Invalid regular expression: %s", moduleRegExp)
124118
return "", err
125119
}
126-
127120
lock.Lock()
128121
defer lock.Unlock()
129122
for module := range modules {
@@ -190,6 +183,15 @@ func InitFromSpec(spec string) string {
190183
}
191184

192185
logging.SetLevel(levelAll, "") // set the logging level for all modules
186+
187+
// iterate through modules to reload their level in the modules map based on
188+
// the new default level
189+
for k := range modules {
190+
MustGetLogger(k)
191+
}
192+
// register flogging logger in the modules map
193+
MustGetLogger(pkgLogID)
194+
193195
return levelAll.String()
194196
}
195197

@@ -226,10 +228,11 @@ func RevertToPeerStartupLevels() error {
226228
lock.RLock()
227229
defer lock.RUnlock()
228230
for key := range peerStartModules {
229-
_, err := setModuleLevel(key, peerStartModules[key], true)
231+
_, err := setModuleLevel(key, peerStartModules[key], false, true)
230232
if err != nil {
231233
return err
232234
}
233235
}
236+
logger.Info("Log levels reverted to the levels defined at the end of peer startup")
234237
return nil
235238
}

common/flogging/logging_test.go

+26-32
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ type testCase struct {
3232
args []string
3333
expectedLevels []string
3434
modules []string
35-
withRegEx bool
3635
revert bool
3736
shouldErr bool
3837
}
@@ -47,38 +46,37 @@ func TestSetModuleLevel(t *testing.T) {
4746
var tc []testCase
4847

4948
tc = append(tc,
50-
testCase{"Valid", []string{"a", "warning"}, []string{"WARNING"}, []string{"a"}, false, false, false},
49+
testCase{"Valid", []string{"a", "warning"}, []string{"WARNING"}, []string{"a"}, false, false},
5150
// Same as before
52-
testCase{"Invalid", []string{"a", "foo"}, []string{"WARNING"}, []string{"a"}, false, false, false},
51+
testCase{"Invalid", []string{"a", "foo"}, []string{flogging.DefaultLevel()}, []string{"a"}, false, false},
52+
// Test setting the "error" module
53+
testCase{"Error", []string{"error", "warning"}, []string{"WARNING"}, []string{"error"}, false, false},
5354
// Tests with regular expressions
5455
testCase{"RegexModuleWithSubmodule", []string{"foo", "warning"}, []string{"WARNING", "WARNING", flogging.DefaultLevel()},
55-
[]string{"foo", "foo/bar", "baz"}, true, false, false},
56+
[]string{"foo", "foo/bar", "baz"}, false, false},
5657
// Set the level for modules that contain "foo" or "baz"
5758
testCase{"RegexOr", []string{"foo|baz", "debug"}, []string{"DEBUG", "DEBUG", "DEBUG", flogging.DefaultLevel()},
58-
[]string{"foo", "foo/bar", "baz", "random"}, true, false, false},
59+
[]string{"foo", "foo/bar", "baz", "random"}, false, false},
5960
// Set the level for modules that end with "bar"
6061
testCase{"RegexSuffix", []string{"bar$", "error"}, []string{"ERROR", flogging.DefaultLevel()},
61-
[]string{"foo/bar", "bar/baz"}, true, false, false},
62+
[]string{"foo/bar", "bar/baz"}, false, false},
6263
testCase{"RegexComplex", []string{"^[a-z]+\\/[a-z]+#.+$", "warning"}, []string{flogging.DefaultLevel(), flogging.DefaultLevel(), "WARNING", "WARNING", "WARNING"},
63-
[]string{"gossip/util", "orderer/util", "gossip/gossip#0.0.0.0:7051", "gossip/conn#-1", "orderer/conn#0.0.0.0:7051"}, true, false, false},
64+
[]string{"gossip/util", "orderer/util", "gossip/gossip#0.0.0.0:7051", "gossip/conn#-1", "orderer/conn#0.0.0.0:7051"}, false, false},
6465
testCase{"RegexInvalid", []string{"(", "warning"}, []string{flogging.DefaultLevel()},
65-
[]string{"foo"}, true, false, true},
66+
[]string{"foo"}, false, true},
6667
testCase{"RevertLevels", []string{"revertmodule1", "warning", "revertmodule2", "debug"}, []string{"WARNING", "DEBUG", "DEBUG"},
67-
[]string{"revertmodule1", "revertmodule2", "revertmodule2/submodule"}, true, true, false},
68+
[]string{"revertmodule1", "revertmodule2", "revertmodule2/submodule"}, true, false},
6869
)
6970

7071
assert := assert.New(t)
7172

7273
for i := 0; i < len(tc); i++ {
7374
t.Run(tc[i].name, func(t *testing.T) {
74-
if tc[i].withRegEx {
75-
for j := 0; j < len(tc[i].modules); j++ {
76-
flogging.MustGetLogger(tc[i].modules[j])
77-
}
78-
if tc[i].revert {
79-
flogging.SetPeerStartupModulesMap()
80-
}
81-
flogging.IsSetLevelByRegExpEnabled = true // enable for call below
75+
for j := 0; j < len(tc[i].modules); j++ {
76+
flogging.MustGetLogger(tc[i].modules[j])
77+
}
78+
if tc[i].revert {
79+
flogging.SetPeerStartupModulesMap()
8280
}
8381
for k := 0; k < len(tc[i].args); k = k + 2 {
8482
_, err := flogging.SetModuleLevel(tc[i].args[k], tc[i].args[k+1])
@@ -95,11 +93,7 @@ func TestSetModuleLevel(t *testing.T) {
9593
assert.Equal(flogging.GetPeerStartupLevel(tc[i].modules[m]), flogging.GetModuleLevel(tc[i].modules[m]))
9694
}
9795
}
98-
if tc[i].withRegEx {
99-
// Force reset (a) in case the next test is non-regex, (b) so as
100-
// to reset the modules map and reuse module names.
101-
flogging.Reset()
102-
}
96+
flogging.Reset()
10397
})
10498
}
10599
}
@@ -130,25 +124,25 @@ func TestInitFromSpec(t *testing.T) {
130124
// MODULES
131125

132126
tc = append(tc,
133-
testCase{"SingleModuleLevel", []string{"a=info"}, []string{"INFO"}, []string{"a"}, false, false, false},
134-
testCase{"MultipleModulesMultipleLevels", []string{"a=info:b=debug"}, []string{"INFO", "DEBUG"}, []string{"a", "b"}, false, false, false},
135-
testCase{"MultipleModulesSameLevel", []string{"a,b=warning"}, []string{"WARNING", "WARNING"}, []string{"a", "b"}, false, false, false},
127+
testCase{"SingleModuleLevel", []string{"a=info"}, []string{"INFO"}, []string{"a"}, false, false},
128+
testCase{"MultipleModulesMultipleLevels", []string{"a=info:b=debug"}, []string{"INFO", "DEBUG"}, []string{"a", "b"}, false, false},
129+
testCase{"MultipleModulesSameLevel", []string{"a,b=warning"}, []string{"WARNING", "WARNING"}, []string{"a", "b"}, false, false},
136130
)
137131

138132
// MODULES + DEFAULT
139133

140134
tc = append(tc,
141-
testCase{"GlobalDefaultAndSingleModuleLevel", []string{"info:a=warning"}, []string{"INFO", "WARNING"}, []string{"", "a"}, false, false, false},
142-
testCase{"SingleModuleLevelAndGlobalDefaultAtEnd", []string{"a=warning:info"}, []string{"WARNING", "INFO"}, []string{"a", ""}, false, false, false},
135+
testCase{"GlobalDefaultAndSingleModuleLevel", []string{"info:a=warning"}, []string{"INFO", "WARNING"}, []string{"", "a"}, false, false},
136+
testCase{"SingleModuleLevelAndGlobalDefaultAtEnd", []string{"a=warning:info"}, []string{"WARNING", "INFO"}, []string{"a", ""}, false, false},
143137
)
144138

145139
// INVALID INPUT
146140

147141
tc = append(tc,
148-
testCase{"InvalidLevel", []string{"foo"}, []string{flogging.DefaultLevel()}, []string{""}, false, false, false},
149-
testCase{"InvalidLevelForSingleModule", []string{"a=foo"}, []string{flogging.DefaultLevel()}, []string{""}, false, false, false},
150-
testCase{"EmptyModuleEqualsLevel", []string{"=warning"}, []string{flogging.DefaultLevel()}, []string{""}, false, false, false},
151-
testCase{"InvalidModuleSyntax", []string{"a=b=c"}, []string{flogging.DefaultLevel()}, []string{""}, false, false, false},
142+
testCase{"InvalidLevel", []string{"foo"}, []string{flogging.DefaultLevel()}, []string{""}, false, false},
143+
testCase{"InvalidLevelForSingleModule", []string{"a=foo"}, []string{flogging.DefaultLevel()}, []string{""}, false, false},
144+
testCase{"EmptyModuleEqualsLevel", []string{"=warning"}, []string{flogging.DefaultLevel()}, []string{""}, false, false},
145+
testCase{"InvalidModuleSyntax", []string{"a=b=c"}, []string{flogging.DefaultLevel()}, []string{""}, false, false},
152146
)
153147

154148
assert := assert.New(t)
@@ -173,7 +167,7 @@ func ExampleInitBackend() {
173167
formatSpec := "%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x} %{message}"
174168
flogging.InitBackend(flogging.SetFormat(formatSpec), os.Stdout)
175169

176-
logger := logging.MustGetLogger("testModule")
170+
logger := flogging.MustGetLogger("testModule")
177171
logger.Info("test output")
178172

179173
// Output:

0 commit comments

Comments
 (0)