Skip to content

Commit 777bdac

Browse files
committed
FAB-394 Chaincode log level cannot be changed
These changes make sure that the logging.chaincode setting from core.yaml is used to set the chaincode logging level every time the chaincode container starts. The peer reads in the value and passes it to the chaincode via an environment variable as it does for similar values. Fix Issue FAB-394 Change-Id: Ic4b7228c57fc673a97dbfafc1b086ad04c41c05c Signed-off-by: Will Lahti <[email protected]>
1 parent 02a123c commit 777bdac

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

core/chaincode/chaincode_support.go

+23
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"time"
2626

2727
"github.com/golang/protobuf/proto"
28+
logging "github.com/op/go-logging"
2829
"github.com/spf13/viper"
2930
"golang.org/x/net/context"
3031

@@ -34,6 +35,7 @@ import (
3435
"github.com/hyperledger/fabric/core/container/ccintf"
3536
"github.com/hyperledger/fabric/core/crypto"
3637
"github.com/hyperledger/fabric/core/ledger"
38+
"github.com/hyperledger/fabric/flogging"
3739
pb "github.com/hyperledger/fabric/protos"
3840
)
3941

@@ -147,6 +149,21 @@ func NewChaincodeSupport(chainname ChainName, getPeerEndpoint func() (*pb.PeerEn
147149
s.keepalive = time.Duration(t) * time.Second
148150
}
149151

152+
viper.SetEnvPrefix("CORE")
153+
viper.AutomaticEnv()
154+
replacer := strings.NewReplacer(".", "_")
155+
viper.SetEnvKeyReplacer(replacer)
156+
157+
chaincodeLogLevelString := viper.GetString("logging.chaincode")
158+
chaincodeLogLevel, err := logging.LogLevel(chaincodeLogLevelString)
159+
160+
if err == nil {
161+
s.chaincodeLogLevel = chaincodeLogLevel.String()
162+
} else {
163+
chaincodeLogger.Infof("chaincode logging level %s is invalid. defaulting to %s\n", chaincodeLogLevelString, flogging.DefaultLoggingLevel().String())
164+
s.chaincodeLogLevel = flogging.DefaultLoggingLevel().String()
165+
}
166+
150167
return s
151168
}
152169

@@ -172,6 +189,7 @@ type ChaincodeSupport struct {
172189
peerTLSKeyFile string
173190
peerTLSSvrHostOrd string
174191
keepalive time.Duration
192+
chaincodeLogLevel string
175193
}
176194

177195
// DuplicateChaincodeHandlerError returned if attempt to register same chaincodeID while a stream already exists.
@@ -290,6 +308,11 @@ func (chaincodeSupport *ChaincodeSupport) getArgsAndEnv(cID *pb.ChaincodeID, cLa
290308
} else {
291309
envs = append(envs, "CORE_PEER_TLS_ENABLED=false")
292310
}
311+
312+
if chaincodeSupport.chaincodeLogLevel != "" {
313+
envs = append(envs, "CORE_LOGGING_CHAINCODE="+chaincodeSupport.chaincodeLogLevel)
314+
}
315+
293316
switch cLang {
294317
case pb.ChaincodeSpec_GOLANG, pb.ChaincodeSpec_CAR:
295318
//chaincode executable will be same as the name of the chaincode

core/chaincode/shim/chaincode.go

+26-4
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,7 @@ func Start(cc Chaincode) error {
6868
backendFormatter := logging.NewBackendFormatter(backend, format)
6969
logging.SetBackend(backendFormatter).SetLevel(logging.Level(shimLoggingLevel), "shim")
7070

71-
viper.SetEnvPrefix("CORE")
72-
viper.AutomaticEnv()
73-
replacer := strings.NewReplacer(".", "_")
74-
viper.SetEnvKeyReplacer(replacer)
71+
SetChaincodeLoggingLevel()
7572

7673
flag.StringVar(&peerAddress, "peer.address", "", "peer address")
7774

@@ -105,6 +102,31 @@ func Start(cc Chaincode) error {
105102
return err
106103
}
107104

105+
// IsEnabledForLogLevel checks to see if the chaincodeLogger is enabled for a specific logging level
106+
// used primarily for testing
107+
func IsEnabledForLogLevel(logLevel string) bool {
108+
lvl, _ := logging.LogLevel(logLevel)
109+
return chaincodeLogger.IsEnabledFor(lvl)
110+
}
111+
112+
// SetChaincodeLoggingLevel sets the chaincode logging level to the value
113+
// of CORE_LOGGING_CHAINCODE set from core.yaml by chaincode_support.go
114+
func SetChaincodeLoggingLevel() {
115+
viper.SetEnvPrefix("CORE")
116+
viper.AutomaticEnv()
117+
replacer := strings.NewReplacer(".", "_")
118+
viper.SetEnvKeyReplacer(replacer)
119+
120+
chaincodeLogLevelString := viper.GetString("logging.chaincode")
121+
chaincodeLogLevel, err := LogLevel(chaincodeLogLevelString)
122+
123+
if err == nil {
124+
SetLoggingLevel(chaincodeLogLevel)
125+
} else {
126+
chaincodeLogger.Infof("error with chaincode log level: %s level= %s\n", err, chaincodeLogLevelString)
127+
}
128+
}
129+
108130
// StartInProc is an entry point for system chaincodes bootstrap. It is not an
109131
// API for chaincodes.
110132
func StartInProc(env []string, args []string, cc Chaincode, recv <-chan *pb.ChaincodeMessage, send chan<- *pb.ChaincodeMessage) error {

core/chaincode/shim/mockstub_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package shim
1919
import (
2020
"fmt"
2121
"testing"
22+
23+
"github.com/spf13/viper"
2224
)
2325

2426
func TestMockStateRangeQueryIterator(t *testing.T) {
@@ -50,3 +52,17 @@ func TestMockStateRangeQueryIterator(t *testing.T) {
5052
}
5153
}
5254
}
55+
56+
// TestSetChaincodeLoggingLevel uses the utlity function defined in chaincode.go to
57+
// set the chaincodeLogger's logging level
58+
func TestSetChaincodeLoggingLevel(t *testing.T) {
59+
// set log level to a non-default level
60+
testLogLevelString := "debug"
61+
viper.Set("logging.chaincode", testLogLevelString)
62+
63+
SetChaincodeLoggingLevel()
64+
65+
if !IsEnabledForLogLevel(testLogLevelString) {
66+
t.FailNow()
67+
}
68+
}

0 commit comments

Comments
 (0)