@@ -16,6 +16,37 @@ package main
1616
1717import "github.com/hyperledger/fabric/core/chaincode/shim"
1818import pb "github.com/hyperledger/fabric/protos/peer"
19+ import "os"
20+ import "strings"
21+ // enable logging based on either world state or env variable.
22+ // default to INFO if neither have a value.
23+ func EnableLogging (stub shim.ChaincodeStubInterface ) {
24+ var levelStr string
25+ levelBytes , err := stub .GetState ("ComposerLogLevel" )
26+ if err != nil || levelBytes == nil {
27+ var isSet bool
28+ levelStr , isSet = os .LookupEnv ("CORE_CHAINCODE_LOGLEVEL" )
29+ if ! isSet {
30+ levelStr = "INFO"
31+ }
32+ } else {
33+ levelStr = string (levelBytes )
34+ }
35+
36+ level , _ := shim .LogLevel (levelStr )
37+ logger .SetLevel (level )
38+ }
39+
40+ // explicitly set the logging to a specific level
41+ func SetLogging (stub shim.ChaincodeStubInterface , levelStr string ) {
42+ //We could check that the levelStr is valid but
43+ //currently if it isn't then I think shim.LogLevel will return a default of loglevel of Error.
44+ newLevel := strings .ToUpper (levelStr )
45+ stub .PutState ("ComposerLogLevel" , []byte (newLevel ))
46+ level , _ := shim .LogLevel (newLevel )
47+ logger .SetLevel (level )
48+ logger .Warning ("Setting loglevel to" , newLevel )
49+ }
1950
2051// Chaincode is the chaincode class. It is an implementation of the
2152// Chaincode interface.
@@ -36,10 +67,9 @@ func NewChaincode() (result *Chaincode) {
3667// Init is called by the Hyperledger Fabric when the chaincode is deployed.
3768// Init can read from and write to the world state.
3869func (chaincode * Chaincode ) Init (stub shim.ChaincodeStubInterface ) (response pb.Response ) {
39- //TODO: Need to control this via env var and/or api call.
4070 //logging needs to be set here again as the fabric chaincode disables it
4171 //even though it was enabled in main.
42- logger . SetLevel ( shim . LogDebug )
72+ EnableLogging ( stub )
4373 logger .Debug ("Entering Chaincode.Init" , & stub )
4474 defer func () {
4575 logger .Debug ("Exiting Chaincode.Init" , response .Status , response .Message , string (response .Payload ))
@@ -51,6 +81,17 @@ func (chaincode *Chaincode) Init(stub shim.ChaincodeStubInterface) (response pb.
5181
5282 // Execute the init function.
5383 function , arguments := stub .GetFunctionAndParameters ()
84+
85+ // look for -d loglevel and set log and remove
86+ // from arguments
87+ var loglevel string
88+ for i , value := range arguments {
89+ if value == "-d" && (i + 1 ) < len (arguments ) {
90+ loglevel = arguments [i + 1 ]
91+ arguments = append (arguments [:i ], arguments [i + 2 :]... )
92+ }
93+ }
94+ SetLogging (stub , loglevel )
5495 payload , err := composer .Init (stub , function , arguments )
5596 if err != nil {
5697 return shim .Error (err .Error ())
@@ -61,10 +102,9 @@ func (chaincode *Chaincode) Init(stub shim.ChaincodeStubInterface) (response pb.
61102// Invoke is called by the Hyperledger Fabric when the chaincode is invoked.
62103// Invoke can read from and write to the world state.
63104func (chaincode * Chaincode ) Invoke (stub shim.ChaincodeStubInterface ) (response pb.Response ) {
64- //TODO: Need to control this via env var and/or api call.
65105 //logging needs to be set here again as the fabric chaincode disables it
66106 //even though it was enabled in main.
67- logger . SetLevel ( shim . LogDebug )
107+ EnableLogging ( stub )
68108 logger .Debug ("Entering Chaincode.Invoke" , & stub )
69109 defer func () {
70110 logger .Debug ("Exiting Chaincode.Invoke" , response .Status , response .Message , string (response .Payload ))
@@ -76,9 +116,14 @@ func (chaincode *Chaincode) Invoke(stub shim.ChaincodeStubInterface) (response p
76116
77117 // Execute the invoke function.
78118 function , arguments := stub .GetFunctionAndParameters ()
79- payload , err := composer .Invoke (stub , function , arguments )
80- if err != nil {
81- return shim .Error (err .Error ())
119+ if strings .ToLower (function ) == "logging" {
120+ SetLogging (stub , arguments [0 ])
121+ return shim .Success (nil )
122+ } else {
123+ payload , err := composer .Invoke (stub , function , arguments )
124+ if err != nil {
125+ return shim .Error (err .Error ())
126+ }
127+ return shim .Success (payload )
82128 }
83- return shim .Success (payload )
84129}
0 commit comments