Skip to content

Commit 5ca0611

Browse files
committed
Add ability to customize chaincode container log format
This change adds a parameter to core.yaml called "chaincode.logFormat" which can be used to set the log format for chaincode containers. It also replaces "logging.chaincode" with a new parameter called "chaincode.logLevel" to keep the chaincode logging parameters grouped together. https://jira.hyperledger.org/browse/FAB-1936 Change-Id: I7f2ad29e2b91e9c98f41bea499735120a81e16b0 Signed-off-by: Will Lahti <[email protected]>
1 parent 2dd7a75 commit 5ca0611

File tree

4 files changed

+50
-19
lines changed

4 files changed

+50
-19
lines changed

core/chaincode/chaincode_support.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func NewChaincodeSupport(getPeerEndpoint func() (*pb.PeerEndpoint, error), userr
173173
replacer := strings.NewReplacer(".", "_")
174174
viper.SetEnvKeyReplacer(replacer)
175175

176-
chaincodeLogLevelString := viper.GetString("logging.chaincode")
176+
chaincodeLogLevelString := viper.GetString("chaincode.logLevel")
177177
chaincodeLogLevel, err := logging.LogLevel(chaincodeLogLevelString)
178178

179179
if err == nil {
@@ -182,6 +182,7 @@ func NewChaincodeSupport(getPeerEndpoint func() (*pb.PeerEndpoint, error), userr
182182
chaincodeLogger.Warningf("Chaincode logging level %s is invalid; defaulting to %s", chaincodeLogLevelString, flogging.DefaultLevel().String())
183183
theChaincodeSupport.chaincodeLogLevel = flogging.DefaultLevel().String()
184184
}
185+
theChaincodeSupport.logFormat = viper.GetString("chaincode.logFormat")
185186

186187
return theChaincodeSupport
187188
}
@@ -206,6 +207,7 @@ type ChaincodeSupport struct {
206207
peerTLSSvrHostOrd string
207208
keepalive time.Duration
208209
chaincodeLogLevel string
210+
logFormat string
209211
}
210212

211213
// DuplicateChaincodeHandlerError returned if attempt to register same chaincodeID while a stream already exists.
@@ -345,7 +347,11 @@ func (chaincodeSupport *ChaincodeSupport) getArgsAndEnv(cccid *ccprovider.CCCont
345347
}
346348

347349
if chaincodeSupport.chaincodeLogLevel != "" {
348-
envs = append(envs, "CORE_LOGGING_CHAINCODE="+chaincodeSupport.chaincodeLogLevel)
350+
envs = append(envs, "CORE_CHAINCODE_LOGLEVEL="+chaincodeSupport.chaincodeLogLevel)
351+
}
352+
353+
if chaincodeSupport.logFormat != "" {
354+
envs = append(envs, "CORE_CHAINCODE_LOGFORMAT="+chaincodeSupport.logFormat)
349355
}
350356
switch cLang {
351357
case pb.ChaincodeSpec_GOLANG, pb.ChaincodeSpec_CAR:

core/chaincode/shim/chaincode.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/golang/protobuf/proto"
3131
"github.com/golang/protobuf/ptypes/timestamp"
3232
"github.com/hyperledger/fabric/bccsp/factory"
33+
"github.com/hyperledger/fabric/common/flogging"
3334
"github.com/hyperledger/fabric/core/comm"
3435
pb "github.com/hyperledger/fabric/protos/peer"
3536
"github.com/hyperledger/fabric/protos/utils"
@@ -41,6 +42,7 @@ import (
4142

4243
// Logger for the shim package.
4344
var chaincodeLogger = logging.MustGetLogger("shim")
45+
var logOutput = os.Stderr
4446

4547
const (
4648
minUnicodeRuneValue = 0 //U+0000
@@ -70,12 +72,7 @@ var peerAddress string
7072
func Start(cc Chaincode) error {
7173
// If Start() is called, we assume this is a standalone chaincode and set
7274
// up formatted logging.
73-
format := logging.MustStringFormatter("%{time:15:04:05.000} [%{module}] %{level:.4s} : %{message}")
74-
backend := logging.NewLogBackend(os.Stderr, "", 0)
75-
backendFormatter := logging.NewBackendFormatter(backend, format)
76-
logging.SetBackend(backendFormatter).SetLevel(logging.Level(shimLoggingLevel), "shim")
77-
78-
SetChaincodeLoggingLevel()
75+
SetupChaincodeLogging()
7976

8077
err := factory.InitFactories(&factory.DefaultOpts)
8178
if err != nil {
@@ -121,18 +118,23 @@ func IsEnabledForLogLevel(logLevel string) bool {
121118
return chaincodeLogger.IsEnabledFor(lvl)
122119
}
123120

124-
// SetChaincodeLoggingLevel sets the chaincode logging level to the value
125-
// of CORE_LOGGING_CHAINCODE set from core.yaml by chaincode_support.go
126-
func SetChaincodeLoggingLevel() {
121+
// SetupChaincodeLogging sets the chaincode logging format and the level
122+
// to the values of CORE_CHAINCODE_LOGFORMAT and CORE_CHAINCODE_LOGLEVEL set
123+
// from core.yaml by chaincode_support.go
124+
func SetupChaincodeLogging() {
127125
viper.SetEnvPrefix("CORE")
128126
viper.AutomaticEnv()
129127
replacer := strings.NewReplacer(".", "_")
130128
viper.SetEnvKeyReplacer(replacer)
131129

132-
chaincodeLogLevelString := viper.GetString("logging.chaincode")
130+
logFormat := viper.GetString("chaincode.logFormat")
131+
flogging.SetLoggingFormat(logFormat, logOutput)
132+
133+
chaincodeLogLevelString := viper.GetString("chaincode.logLevel")
133134
if chaincodeLogLevelString == "" {
134135
shimLogLevelDefault := logging.Level(shimLoggingLevel)
135136
chaincodeLogger.Infof("Chaincode log level not provided; defaulting to: %s", shimLogLevelDefault)
137+
SetLoggingLevel(shimLoggingLevel)
136138
} else {
137139
chaincodeLogLevel, err := LogLevel(chaincodeLogLevelString)
138140
if err == nil {
@@ -620,7 +622,7 @@ const (
620622
LogCritical = LoggingLevel(logging.CRITICAL)
621623
)
622624

623-
var shimLoggingLevel = LogDebug // Necessary for correct initialization; See Start()
625+
var shimLoggingLevel = LogInfo // Necessary for correct initialization; See Start()
624626

625627
// SetLoggingLevel allows a Go language chaincode to set the logging level of
626628
// its shim.

core/chaincode/shim/mockstub_test.go

+25-5
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,34 @@ func TestMockStateRangeQueryIterator_openEnded(t *testing.T) {
8181
}
8282
}
8383

84-
// TestSetChaincodeLoggingLevel uses the utlity function defined in chaincode.go to
85-
// set the chaincodeLogger's logging level
86-
func TestSetChaincodeLoggingLevel(t *testing.T) {
84+
// TestSetupChaincodeLogging uses the utlity function defined in chaincode.go to
85+
// set the chaincodeLogger's logging format and level
86+
func TestSetupChaincodeLogging_blankLevel(t *testing.T) {
87+
// set log level to a non-default level
88+
testLogLevelString := ""
89+
testLogFormat := "%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}"
90+
91+
viper.Set("chaincode.logLevel", testLogLevelString)
92+
viper.Set("chaincode.logFormat", testLogFormat)
93+
94+
SetupChaincodeLogging()
95+
96+
if !IsEnabledForLogLevel("info") {
97+
t.FailNow()
98+
}
99+
}
100+
101+
// TestSetupChaincodeLogging uses the utlity function defined in chaincode.go to
102+
// set the chaincodeLogger's logging format and level
103+
func TestSetupChaincodeLogging(t *testing.T) {
87104
// set log level to a non-default level
88105
testLogLevelString := "debug"
89-
viper.Set("logging.chaincode", testLogLevelString)
106+
testLogFormat := "%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}"
107+
108+
viper.Set("chaincode.logLevel", testLogLevelString)
109+
viper.Set("chaincode.logFormat", testLogFormat)
90110

91-
SetChaincodeLoggingLevel()
111+
SetupChaincodeLogging()
92112

93113
if !IsEnabledForLogLevel(testLogLevelString) {
94114
t.FailNow()

peer/core.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ logging:
3232
peer: warning
3333
node: info
3434
network: warning
35-
chaincode: warning
3635
version: warning
3736
protoutils: debug
3837
error: warning
@@ -336,6 +335,10 @@ chaincode:
336335
vscc: enable
337336
qscc: enable
338337

338+
# logging section for the chaincode container
339+
logLevel: warning
340+
logFormat: '%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}'
341+
339342
###############################################################################
340343
#
341344
# Ledger section - ledger configuration encompases both the blockchain

0 commit comments

Comments
 (0)