Skip to content

Commit bd861de

Browse files
committed
[FAB-3232] Fix chaincode container logging
This CR fixes the logging setup for chaincode containers. Previously, the code would only set the level for the 'shim' module and all other loggers in the scope of the container would use the default. With this change in place, CORE_CHAINCODE_LOGGING_LEVEL can be used to set the default logging level for all modules within a chaincode container and CORE_CHAINCODE_LOGGING_SHIM can be used to override the level for the 'shim' logging module. Change-Id: I019fd3995e2af25a170d751628293fb215a748bf Signed-off-by: Will Lahti <[email protected]>
1 parent 58f33ec commit bd861de

File tree

6 files changed

+126
-30
lines changed

6 files changed

+126
-30
lines changed

core/chaincode/chaincode_support.go

+22-10
Original file line numberDiff line numberDiff line change
@@ -185,18 +185,25 @@ func NewChaincodeSupport(getPeerEndpoint func() (*pb.PeerEndpoint, error), userr
185185
replacer := strings.NewReplacer(".", "_")
186186
viper.SetEnvKeyReplacer(replacer)
187187

188-
chaincodeLogLevelString := viper.GetString("chaincode.logLevel")
189-
chaincodeLogLevel, err := logging.LogLevel(chaincodeLogLevelString)
188+
theChaincodeSupport.chaincodeLogLevel = getLogLevelFromViper("level")
189+
theChaincodeSupport.shimLogLevel = getLogLevelFromViper("shim")
190+
theChaincodeSupport.logFormat = viper.GetString("chaincode.logging.format")
191+
192+
return theChaincodeSupport
193+
}
194+
195+
// getLogLevelFromViper gets the chaincode container log levels from viper
196+
func getLogLevelFromViper(module string) string {
197+
levelString := viper.GetString("chaincode.logging." + module)
198+
_, err := logging.LogLevel(levelString)
190199

191200
if err == nil {
192-
theChaincodeSupport.chaincodeLogLevel = chaincodeLogLevel.String()
201+
chaincodeLogger.Debugf("CORE_CHAINCODE_%s set to level %s", strings.ToUpper(module), levelString)
193202
} else {
194-
chaincodeLogger.Warningf("Chaincode logging level %s is invalid; defaulting to %s", chaincodeLogLevelString, flogging.DefaultLevel())
195-
theChaincodeSupport.chaincodeLogLevel = flogging.DefaultLevel()
203+
chaincodeLogger.Warningf("CORE_CHAINCODE_%s has invalid log level %s. defaulting to %s", strings.ToUpper(module), levelString, flogging.DefaultLevel())
204+
levelString = flogging.DefaultLevel()
196205
}
197-
theChaincodeSupport.logFormat = viper.GetString("chaincode.logFormat")
198-
199-
return theChaincodeSupport
206+
return levelString
200207
}
201208

202209
// // ChaincodeStream standard stream for ChaincodeMessage type.
@@ -219,6 +226,7 @@ type ChaincodeSupport struct {
219226
peerTLSSvrHostOrd string
220227
keepalive time.Duration
221228
chaincodeLogLevel string
229+
shimLogLevel string
222230
logFormat string
223231
executetimeout time.Duration
224232
}
@@ -360,11 +368,15 @@ func (chaincodeSupport *ChaincodeSupport) getArgsAndEnv(cccid *ccprovider.CCCont
360368
}
361369

362370
if chaincodeSupport.chaincodeLogLevel != "" {
363-
envs = append(envs, "CORE_CHAINCODE_LOGLEVEL="+chaincodeSupport.chaincodeLogLevel)
371+
envs = append(envs, "CORE_CHAINCODE_LOGGING_LEVEL="+chaincodeSupport.chaincodeLogLevel)
372+
}
373+
374+
if chaincodeSupport.shimLogLevel != "" {
375+
envs = append(envs, "CORE_CHAINCODE_LOGGING_SHIM="+chaincodeSupport.shimLogLevel)
364376
}
365377

366378
if chaincodeSupport.logFormat != "" {
367-
envs = append(envs, "CORE_CHAINCODE_LOGFORMAT="+chaincodeSupport.logFormat)
379+
envs = append(envs, "CORE_CHAINCODE_LOGGING_FORMAT="+chaincodeSupport.logFormat)
368380
}
369381
switch cLang {
370382
case pb.ChaincodeSpec_GOLANG, pb.ChaincodeSpec_CAR:

core/chaincode/chaincodetest.yaml

+8-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,14 @@ chaincode:
407407
escc: enable
408408
vscc: enable
409409

410-
logLevel: debug
410+
# Logging section for the chaincode container
411+
logging:
412+
# Default level for all loggers within the chaincode container
413+
level: info
414+
# Override default level for the 'shim' module
415+
shim: warning
416+
# Format for the chaincode container logs
417+
format: '%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}'
411418

412419
###############################################################################
413420
#

core/chaincode/shim/chaincode.go

+24-8
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,38 @@ func SetupChaincodeLogging() {
133133
viper.SetEnvKeyReplacer(replacer)
134134

135135
// setup system-wide logging backend
136-
logFormat := flogging.SetFormat(viper.GetString("chaincode.logFormat"))
136+
logFormat := flogging.SetFormat(viper.GetString("chaincode.logging.format"))
137137
flogging.InitBackend(logFormat, logOutput)
138138

139-
chaincodeLogLevelString := viper.GetString("chaincode.logLevel")
139+
// set default log level for all modules
140+
chaincodeLogLevelString := viper.GetString("chaincode.logging.level")
140141
if chaincodeLogLevelString == "" {
141-
shimLogLevelDefault := logging.Level(shimLoggingLevel)
142-
chaincodeLogger.Infof("Chaincode log level not provided; defaulting to: %s", shimLogLevelDefault)
143-
SetLoggingLevel(shimLoggingLevel)
142+
chaincodeLogger.Infof("Chaincode log level not provided; defaulting to: %s", flogging.DefaultLevel())
143+
flogging.InitFromSpec(flogging.DefaultLevel())
144144
} else {
145-
chaincodeLogLevel, err := LogLevel(chaincodeLogLevelString)
145+
_, err := LogLevel(chaincodeLogLevelString)
146146
if err == nil {
147-
SetLoggingLevel(chaincodeLogLevel)
147+
flogging.InitFromSpec(chaincodeLogLevelString)
148148
} else {
149-
chaincodeLogger.Warningf("Error: %s for chaincode log level: %s", err, chaincodeLogLevelString)
149+
chaincodeLogger.Warningf("Error: '%s' for chaincode log level: %s; defaulting to %s", err, chaincodeLogLevelString, flogging.DefaultLevel())
150+
flogging.InitFromSpec(flogging.DefaultLevel())
150151
}
151152
}
153+
154+
// override the log level for the shim logging module - note: if this value is
155+
// blank or an invalid log level, then the above call to
156+
// `flogging.InitFromSpec` already set the default log level so no action
157+
// is required here.
158+
shimLogLevelString := viper.GetString("chaincode.logging.shim")
159+
if shimLogLevelString != "" {
160+
shimLogLevel, err := LogLevel(shimLogLevelString)
161+
if err == nil {
162+
SetLoggingLevel(shimLogLevel)
163+
} else {
164+
chaincodeLogger.Warningf("Error: %s for shim log level: %s", err, shimLogLevelString)
165+
}
166+
}
167+
152168
//now that logging is setup, print build level. This will help making sure
153169
//chaincode is matched with peer.
154170
buildLevel := viper.GetString("chaincode.buildlevel")

core/chaincode/shim/mockstub_test.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"reflect"
2323
"testing"
2424

25+
"github.com/hyperledger/fabric/common/flogging"
2526
"github.com/spf13/viper"
2627
)
2728

@@ -88,12 +89,12 @@ func TestSetupChaincodeLogging_blankLevel(t *testing.T) {
8889
testLogLevelString := ""
8990
testLogFormat := "%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}"
9091

91-
viper.Set("chaincode.logLevel", testLogLevelString)
92-
viper.Set("chaincode.logFormat", testLogFormat)
92+
viper.Set("chaincode.logging.level", testLogLevelString)
93+
viper.Set("chaincode.logging.format", testLogFormat)
9394

9495
SetupChaincodeLogging()
9596

96-
if !IsEnabledForLogLevel("info") {
97+
if !IsEnabledForLogLevel(flogging.DefaultLevel()) {
9798
t.FailNow()
9899
}
99100
}
@@ -102,15 +103,17 @@ func TestSetupChaincodeLogging_blankLevel(t *testing.T) {
102103
// set the chaincodeLogger's logging format and level
103104
func TestSetupChaincodeLogging(t *testing.T) {
104105
// set log level to a non-default level
105-
testLogLevelString := "debug"
106+
testLogLevel := "debug"
107+
testShimLogLevel := "warning"
106108
testLogFormat := "%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}"
107109

108-
viper.Set("chaincode.logLevel", testLogLevelString)
109-
viper.Set("chaincode.logFormat", testLogFormat)
110+
viper.Set("chaincode.logging.level", testLogLevel)
111+
viper.Set("chaincode.logging.format", testLogFormat)
112+
viper.Set("chaincode.logging.shim", testShimLogLevel)
110113

111114
SetupChaincodeLogging()
112115

113-
if !IsEnabledForLogLevel(testLogLevelString) {
116+
if !IsEnabledForLogLevel(testShimLogLevel) {
114117
t.FailNow()
115118
}
116119
}

core/chaincode/shim/shim_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ package shim
1818

1919
import (
2020
"os"
21+
"strings"
2122
"testing"
2223

24+
"github.com/hyperledger/fabric/common/flogging"
2325
"github.com/op/go-logging"
26+
"github.com/spf13/viper"
27+
"github.com/stretchr/testify/assert"
2428
)
2529

2630
// Test Go shim functionality that can be tested outside of a real chaincode
@@ -142,3 +146,51 @@ func TestNilEventName(t *testing.T) {
142146
}
143147

144148
}
149+
150+
type testCase struct {
151+
name string
152+
ccLogLevel string
153+
shimLogLevel string
154+
}
155+
156+
func TestSetupChaincodeLogging_shim(t *testing.T) {
157+
var tc []testCase
158+
159+
tc = append(tc,
160+
testCase{"ValidLevels", "debug", "warning"},
161+
testCase{"EmptyLevels", "", ""},
162+
testCase{"BadShimLevel", "debug", "war"},
163+
testCase{"BadCCLevel", "deb", "notice"},
164+
testCase{"EmptyShimLevel", "error", ""},
165+
testCase{"EmptyCCLevel", "", "critical"},
166+
)
167+
168+
assert := assert.New(t)
169+
170+
for i := 0; i < len(tc); i++ {
171+
t.Run(tc[i].name, func(t *testing.T) {
172+
viper.Set("chaincode.logging.level", tc[i].ccLogLevel)
173+
viper.Set("chaincode.logging.shim", tc[i].shimLogLevel)
174+
175+
SetupChaincodeLogging()
176+
177+
_, ccErr := logging.LogLevel(tc[i].ccLogLevel)
178+
_, shimErr := logging.LogLevel(tc[i].shimLogLevel)
179+
if ccErr == nil {
180+
assert.Equal(strings.ToUpper(tc[i].ccLogLevel), flogging.GetModuleLevel("ccLogger"), "Test case '%s' failed", tc[i].name)
181+
if shimErr == nil {
182+
assert.Equal(strings.ToUpper(tc[i].shimLogLevel), flogging.GetModuleLevel("shim"), "Test case '%s' failed", tc[i].name)
183+
} else {
184+
assert.Equal(strings.ToUpper(tc[i].ccLogLevel), flogging.GetModuleLevel("shim"), "Test case '%s' failed", tc[i].name)
185+
}
186+
} else {
187+
assert.Equal(flogging.DefaultLevel(), flogging.GetModuleLevel("ccLogger"), "Test case '%s' failed", tc[i].name)
188+
if shimErr == nil {
189+
assert.Equal(strings.ToUpper(tc[i].shimLogLevel), flogging.GetModuleLevel("shim"), "Test case '%s' failed", tc[i].name)
190+
} else {
191+
assert.Equal(flogging.DefaultLevel(), flogging.GetModuleLevel("shim"), "Test case '%s' failed", tc[i].name)
192+
}
193+
}
194+
})
195+
}
196+
}

sampleconfig/core.yaml

+10-4
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ logging:
3030
gossip: warning
3131
ledger: info
3232
msp: warning
33-
policies: warning
33+
policies: warning
3434
grpc: error
3535

36+
# Format for the peer logs
3637
format: '%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}'
3738

3839
###############################################################################
@@ -332,9 +333,14 @@ chaincode:
332333
vscc: enable
333334
qscc: enable
334335

335-
# logging section for the chaincode container
336-
logLevel: warning
337-
logFormat: '%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}'
336+
# Logging section for the chaincode container
337+
logging:
338+
# Default level for all loggers within the chaincode container
339+
level: info
340+
# Override default level for the 'shim' module
341+
shim: warning
342+
# Format for the chaincode container logs
343+
format: '%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}'
338344

339345
###############################################################################
340346
#

0 commit comments

Comments
 (0)