Skip to content

Commit cb8fe31

Browse files
author
Jason Yellick
committed
[FAB-1333] Make orderer logging configurable
https://jira.hyperledger.org/browse/FAB-1333 The orderer code has taken an ad-hoc approach to logging during construction. This changeset unifies the logging across the common components, solo, and kafka. It deliberately does not touch the sbft portion of the package so as not to disrupt its development until it is ready to move onto the common config and common code. Note that this moves flogging into the common package, and splits the LoggingInit into two pieces. The peer assumes a single global viper and peer specific config structures. This dependency leaked into flogging, but in reality, the spec should probably be passed from the peer to flogging, rather than having flogging interrogate the configuration itself. This would however have balooned the changeset, so it is left as is for now. Revision 3: Per feedback from Kostas, fixed the stuttering which was prevalent in the flogging package. Revision 4: Fix linting errors Revision 6: Fix rebasing bugs which balooned the diff Change-Id: I3ed21a3a001fb1e9c6b28be700203b84c69ee9f0 Signed-off-by: Jason Yellick <[email protected]>
1 parent c20fd9d commit cb8fe31

31 files changed

+363
-333
lines changed

bddtests/docker-compose-orderer-base.yml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ services:
1010
- ORDERER_GENERAL_BATCHSIZE_MAXMESSAGECOUNT=10
1111
- ORDERER_GENERAL_MAXWINDOWSIZE=1000
1212
- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
13+
- ORDERER_GENERAL_LOGLEVEL=debug
1314
- ORDERER_GENERAL_GENESISIFILE=${ORDERER_GENERAL_GENESISIFILE}
1415
- ORDERER_RAMLEDGER_HISTORY_SIZE=100
1516
volumes:

core/flogging/logging.go common/flogging/logging.go

+40-30
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,41 @@ import (
2626
)
2727

2828
// A logger to log logging logs!
29-
var loggingLogger = logging.MustGetLogger("logging")
29+
var logger = logging.MustGetLogger("logging")
3030

31-
var loggingDefaultFormat = "%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}"
32-
var loggingDefaultOutput = os.Stderr
31+
var defaultFormat = "%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}"
32+
var defaultOutput = os.Stderr
3333

3434
// The default logging level, in force until LoggingInit() is called or in
3535
// case of configuration errors.
36-
var loggingDefaultLevel = logging.INFO
36+
var fallbackDefaultLevel = logging.INFO
3737

38-
// LoggingInit is a 'hook' called at the beginning of command processing to
38+
// LoggingInitFromViper is a 'hook' called at the beginning of command processing to
3939
// parse logging-related options specified either on the command-line or in
4040
// config files. Command-line options take precedence over config file
4141
// options, and can also be passed as suitably-named environment variables. To
4242
// change module logging levels at runtime call `logging.SetLevel(level,
4343
// module)`. To debug this routine include logging=debug as the first
4444
// term of the logging specification.
45-
func LoggingInit(command string) {
46-
// Parse the logging specification in the form
47-
// [<module>[,<module>...]=]<level>[:[<module>[,<module>...]=]<level>...]
48-
defaultLevel := loggingDefaultLevel
49-
var err error
45+
// TODO this initialization is specific to the peer config format. The viper
46+
// references should be removed, and this path should be moved into the peer
47+
func InitFromViper(command string) {
5048
spec := viper.GetString("logging_level")
5149
if spec == "" {
5250
spec = viper.GetString("logging." + command)
5351
}
52+
defaultLevel := InitFromSpec(spec)
53+
logger.Debugf("Setting default logging level to %s for command '%s'", defaultLevel, command)
54+
}
55+
56+
// LoggingInit initializes the logging based on the supplied spec, it is exposed externally
57+
// so that consumers of the flogging package who do not wish to use the default config structure
58+
// may parse their own logging specification
59+
func InitFromSpec(spec string) logging.Level {
60+
// Parse the logging specification in the form
61+
// [<module>[,<module>...]=]<level>[:[<module>[,<module>...]=]<level>...]
62+
defaultLevel := fallbackDefaultLevel
63+
var err error
5464
if spec != "" {
5565
fields := strings.Split(spec, ":")
5666
for _, field := range fields {
@@ -60,46 +70,46 @@ func LoggingInit(command string) {
6070
// Default level
6171
defaultLevel, err = logging.LogLevel(field)
6272
if err != nil {
63-
loggingLogger.Warningf("Logging level '%s' not recognized, defaulting to %s : %s", field, loggingDefaultLevel, err)
64-
defaultLevel = loggingDefaultLevel // NB - 'defaultLevel' was overwritten
73+
logger.Warningf("Logging level '%s' not recognized, defaulting to %s : %s", field, defaultLevel, err)
74+
defaultLevel = fallbackDefaultLevel // NB - 'defaultLevel' was overwritten
6575
}
6676
case 2:
6777
// <module>[,<module>...]=<level>
6878
if level, err := logging.LogLevel(split[1]); err != nil {
69-
loggingLogger.Warningf("Invalid logging level in '%s' ignored", field)
79+
logger.Warningf("Invalid logging level in '%s' ignored", field)
7080
} else if split[0] == "" {
71-
loggingLogger.Warningf("Invalid logging override specification '%s' ignored - no module specified", field)
81+
logger.Warningf("Invalid logging override specification '%s' ignored - no module specified", field)
7282
} else {
7383
modules := strings.Split(split[0], ",")
7484
for _, module := range modules {
7585
logging.SetLevel(level, module)
76-
loggingLogger.Debugf("Setting logging level for module '%s' to %s", module, level)
86+
logger.Debugf("Setting logging level for module '%s' to %s", module, level)
7787
}
7888
}
7989
default:
80-
loggingLogger.Warningf("Invalid logging override '%s' ignored; Missing ':' ?", field)
90+
logger.Warningf("Invalid logging override '%s' ignored; Missing ':' ?", field)
8191
}
8292
}
8393
}
8494
// Set the default logging level for all modules
8595
logging.SetLevel(defaultLevel, "")
86-
loggingLogger.Debugf("Setting default logging level to %s for command '%s'", defaultLevel, command)
96+
return defaultLevel
8797
}
8898

89-
// DefaultLoggingLevel returns the fallback value for loggers to use if parsing fails
90-
func DefaultLoggingLevel() logging.Level {
91-
return loggingDefaultLevel
99+
// DefaultLevel returns the fallback value for loggers to use if parsing fails
100+
func DefaultLevel() logging.Level {
101+
return fallbackDefaultLevel
92102
}
93103

94104
// Initiate 'leveled' logging using the default format and output location
95105
func init() {
96-
SetLoggingFormat(loggingDefaultFormat, loggingDefaultOutput)
106+
SetLoggingFormat(defaultFormat, defaultOutput)
97107
}
98108

99109
// SetLoggingFormat sets the logging format and the location of the log output
100110
func SetLoggingFormat(formatString string, output io.Writer) {
101111
if formatString == "" {
102-
formatString = loggingDefaultFormat
112+
formatString = defaultFormat
103113
}
104114
format := logging.MustStringFormatter(formatString)
105115

@@ -111,32 +121,32 @@ func SetLoggingFormat(formatString string, output io.Writer) {
111121
func initLoggingBackend(logFormatter logging.Formatter, output io.Writer) {
112122
backend := logging.NewLogBackend(output, "", 0)
113123
backendFormatter := logging.NewBackendFormatter(backend, logFormatter)
114-
logging.SetBackend(backendFormatter).SetLevel(loggingDefaultLevel, "")
124+
logging.SetBackend(backendFormatter).SetLevel(fallbackDefaultLevel, "")
115125
}
116126

117-
// GetModuleLogLevel gets the current logging level for the specified module
118-
func GetModuleLogLevel(module string) (string, error) {
127+
// GetModuleLevel gets the current logging level for the specified module
128+
func GetModuleLevel(module string) (string, error) {
119129
// logging.GetLevel() returns the logging level for the module, if defined.
120130
// otherwise, it returns the default logging level, as set by
121131
// flogging/logging.go
122132
level := logging.GetLevel(module).String()
123133

124-
loggingLogger.Debugf("Module '%s' logger enabled for log level: %s", module, level)
134+
logger.Debugf("Module '%s' logger enabled for log level: %s", module, level)
125135

126136
return level, nil
127137
}
128138

129-
// SetModuleLogLevel sets the logging level for the specified module. This is
139+
// SetModuleLevel sets the logging level for the specified module. This is
130140
// currently only called from admin.go but can be called from anywhere in the
131141
// code on a running peer to dynamically change the log level for the module.
132-
func SetModuleLogLevel(module string, logLevel string) (string, error) {
142+
func SetModuleLevel(module string, logLevel string) (string, error) {
133143
level, err := logging.LogLevel(logLevel)
134144

135145
if err != nil {
136-
loggingLogger.Warningf("Invalid logging level: %s - ignored", logLevel)
146+
logger.Warningf("Invalid logging level: %s - ignored", logLevel)
137147
} else {
138148
logging.SetLevel(logging.Level(level), module)
139-
loggingLogger.Debugf("Module '%s' logger enabled for log level: %s", module, level)
149+
logger.Debugf("Module '%s' logger enabled for log level: %s", module, level)
140150
}
141151

142152
logLevelString := level.String()

common/flogging/logging_test.go

+232
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package flogging_test
18+
19+
import (
20+
"os"
21+
"testing"
22+
23+
"github.com/hyperledger/fabric/common/flogging"
24+
"github.com/op/go-logging"
25+
"github.com/spf13/viper"
26+
)
27+
28+
func TestLevelDefault(t *testing.T) {
29+
viper.Reset()
30+
31+
flogging.InitFromViper("")
32+
33+
assertDefaultLevel(t, flogging.DefaultLevel())
34+
}
35+
36+
func TestLevelOtherThanDefault(t *testing.T) {
37+
viper.Reset()
38+
viper.Set("logging_level", "warning")
39+
40+
flogging.InitFromViper("")
41+
42+
assertDefaultLevel(t, logging.WARNING)
43+
}
44+
45+
func TestLevelForSpecificModule(t *testing.T) {
46+
viper.Reset()
47+
viper.Set("logging_level", "core=info")
48+
49+
flogging.InitFromViper("")
50+
51+
assertModuleLevel(t, "core", logging.INFO)
52+
}
53+
54+
func TestLeveltForMultipleModules(t *testing.T) {
55+
viper.Reset()
56+
viper.Set("logging_level", "core=warning:test=debug")
57+
58+
flogging.InitFromViper("")
59+
60+
assertModuleLevel(t, "core", logging.WARNING)
61+
assertModuleLevel(t, "test", logging.DEBUG)
62+
}
63+
64+
func TestLevelForMultipleModulesAtSameLevel(t *testing.T) {
65+
viper.Reset()
66+
viper.Set("logging_level", "core,test=warning")
67+
68+
flogging.InitFromViper("")
69+
70+
assertModuleLevel(t, "core", logging.WARNING)
71+
assertModuleLevel(t, "test", logging.WARNING)
72+
}
73+
74+
func TestLevelForModuleWithDefault(t *testing.T) {
75+
viper.Reset()
76+
viper.Set("logging_level", "info:test=warning")
77+
78+
flogging.InitFromViper("")
79+
80+
assertDefaultLevel(t, logging.INFO)
81+
assertModuleLevel(t, "test", logging.WARNING)
82+
}
83+
84+
func TestLevelForModuleWithDefaultAtEnd(t *testing.T) {
85+
viper.Reset()
86+
viper.Set("logging_level", "test=warning:info")
87+
88+
flogging.InitFromViper("")
89+
90+
assertDefaultLevel(t, logging.INFO)
91+
assertModuleLevel(t, "test", logging.WARNING)
92+
}
93+
94+
func TestLevelForSpecificCommand(t *testing.T) {
95+
viper.Reset()
96+
viper.Set("logging.node", "error")
97+
98+
flogging.InitFromViper("node")
99+
100+
assertDefaultLevel(t, logging.ERROR)
101+
}
102+
103+
func TestLevelForUnknownCommandGoesToDefault(t *testing.T) {
104+
viper.Reset()
105+
106+
flogging.InitFromViper("unknown command")
107+
108+
assertDefaultLevel(t, flogging.DefaultLevel())
109+
}
110+
111+
func TestLevelInvalid(t *testing.T) {
112+
viper.Reset()
113+
viper.Set("logging_level", "invalidlevel")
114+
115+
flogging.InitFromViper("")
116+
117+
assertDefaultLevel(t, flogging.DefaultLevel())
118+
}
119+
120+
func TestLevelInvalidModules(t *testing.T) {
121+
viper.Reset()
122+
viper.Set("logging_level", "core=invalid")
123+
124+
flogging.InitFromViper("")
125+
126+
assertDefaultLevel(t, flogging.DefaultLevel())
127+
}
128+
129+
func TestLevelInvalidEmptyModule(t *testing.T) {
130+
viper.Reset()
131+
viper.Set("logging_level", "=warning")
132+
133+
flogging.InitFromViper("")
134+
135+
assertDefaultLevel(t, flogging.DefaultLevel())
136+
}
137+
138+
func TestLevelInvalidModuleSyntax(t *testing.T) {
139+
viper.Reset()
140+
viper.Set("logging_level", "type=warn=again")
141+
142+
flogging.InitFromViper("")
143+
144+
assertDefaultLevel(t, flogging.DefaultLevel())
145+
}
146+
147+
func TestGetModuleLevelDefault(t *testing.T) {
148+
level, _ := flogging.GetModuleLevel("peer")
149+
150+
// peer should be using the default log level at this point
151+
if level != "INFO" {
152+
t.FailNow()
153+
}
154+
}
155+
156+
func TestGetModuleLevelDebug(t *testing.T) {
157+
flogging.SetModuleLevel("peer", "DEBUG")
158+
level, _ := flogging.GetModuleLevel("peer")
159+
160+
// ensure that the log level has changed to debug
161+
if level != "DEBUG" {
162+
t.FailNow()
163+
}
164+
}
165+
166+
func TestGetModuleLevelInvalid(t *testing.T) {
167+
flogging.SetModuleLevel("peer", "invalid")
168+
level, _ := flogging.GetModuleLevel("peer")
169+
170+
// ensure that the log level didn't change after invalid log level specified
171+
if level != "DEBUG" {
172+
t.FailNow()
173+
}
174+
}
175+
176+
func TestSetModuleLevel(t *testing.T) {
177+
flogging.SetModuleLevel("peer", "WARNING")
178+
179+
// ensure that the log level has changed to warning
180+
assertModuleLevel(t, "peer", logging.WARNING)
181+
}
182+
183+
func TestSetModuleLevelInvalid(t *testing.T) {
184+
flogging.SetModuleLevel("peer", "invalid")
185+
186+
// ensure that the log level didn't change after invalid log level specified
187+
assertModuleLevel(t, "peer", logging.WARNING)
188+
}
189+
190+
func ExampleSetLoggingFormat() {
191+
// initializes logging backend for testing and sets
192+
// time to 1970-01-01 00:00:00.000 UTC
193+
logging.InitForTesting(flogging.DefaultLevel())
194+
195+
logFormat := "%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x} %{message}"
196+
flogging.SetLoggingFormat(logFormat, os.Stdout)
197+
198+
logger := logging.MustGetLogger("floggingTest")
199+
logger.Infof("test")
200+
201+
// Output:
202+
// 1970-01-01 00:00:00.000 UTC [floggingTest] ExampleSetLoggingFormat -> INFO 001 test
203+
}
204+
205+
func ExampleSetLoggingFormat_second() {
206+
// initializes logging backend for testing and sets
207+
// time to 1970-01-01 00:00:00.000 UTC
208+
logging.InitForTesting(flogging.DefaultLevel())
209+
210+
logFormat := "%{time:15:04:05.000} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x} %{message}"
211+
flogging.SetLoggingFormat(logFormat, os.Stdout)
212+
213+
logger := logging.MustGetLogger("floggingTest")
214+
logger.Infof("test")
215+
216+
// Output:
217+
// 00:00:00.000 [floggingTest] ExampleSetLoggingFormat_second -> INFO 001 test
218+
}
219+
220+
func assertDefaultLevel(t *testing.T, expectedLevel logging.Level) {
221+
assertModuleLevel(t, "", expectedLevel)
222+
}
223+
224+
func assertModuleLevel(t *testing.T, module string, expectedLevel logging.Level) {
225+
assertEquals(t, expectedLevel, logging.GetLevel(module))
226+
}
227+
228+
func assertEquals(t *testing.T, expected interface{}, actual interface{}) {
229+
if expected != actual {
230+
t.Errorf("Expected: %v, Got: %v", expected, actual)
231+
}
232+
}

0 commit comments

Comments
 (0)