Skip to content

Commit 755f79d

Browse files
committed
[FAB-3009] Clean up core.yaml logging section
This CR handles a number of core.yaml cleanup operations for the logging section. It removes unused parameters, adds new parameters for the 'gossip' and 'ledger' modules (that will be utilized in a future CR), and updates the comments (which were outdated). It also ensures that 'logging.peer' is used to set the default log level when the CORE_LOGGING_LEVEL environment variable is not set. Change-Id: Ib94d4b991570536b4fb195b472f381c565fd13ba Signed-off-by: Will Lahti <[email protected]>
1 parent c1684d0 commit 755f79d

File tree

6 files changed

+63
-29
lines changed

6 files changed

+63
-29
lines changed

peer/clilogging/common.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package clilogging
1818

1919
import (
2020
"github.com/hyperledger/fabric/core/errors"
21+
"github.com/hyperledger/fabric/peer/common"
2122

22-
"github.com/op/go-logging"
2323
"github.com/spf13/cobra"
2424
)
2525

@@ -44,10 +44,7 @@ func checkLoggingCmdParams(cmd *cobra.Command, args []string) error {
4444
err = errors.ErrorWithCallstack("LOG", "400", "No log level provided.")
4545
} else {
4646
// check that log level is valid. if not, err is set
47-
_, err = logging.LogLevel(args[1])
48-
if err != nil {
49-
err = errors.ErrorWithCallstack("LOG", "400", "Invalid log level provided - %s", args[1])
50-
}
47+
err = common.CheckLogLevel(args[1])
5148
}
5249
}
5350

peer/clilogging/logging.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
const loggingFuncName = "logging"
2828

29-
var logger = logging.MustGetLogger("loggingCmd")
29+
var logger = logging.MustGetLogger("cli/logging")
3030

3131
// Cmd returns the cobra command for Logging
3232
func Cmd() *cobra.Command {

peer/common/common.go

+21
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/hyperledger/fabric/msp"
2929
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
3030
pb "github.com/hyperledger/fabric/protos/peer"
31+
logging "github.com/op/go-logging"
3132
"github.com/spf13/viper"
3233
)
3334

@@ -105,11 +106,31 @@ func SetLogLevelFromViper(module string) error {
105106
var err error
106107
if module != "" {
107108
logLevelFromViper := viper.GetString("logging." + module)
109+
err = CheckLogLevel(logLevelFromViper)
110+
if err != nil {
111+
if module == "error" {
112+
// if 'logging.error' not found in core.yaml or an invalid level has
113+
// been entered, set default to debug to ensure the callstack is
114+
// appended to all CallStackErrors
115+
logLevelFromViper = "debug"
116+
} else {
117+
return err
118+
}
119+
}
108120
_, err = flogging.SetModuleLevel(module, logLevelFromViper)
109121
}
110122
return err
111123
}
112124

125+
// CheckLogLevel checks that a given log level string is valid
126+
func CheckLogLevel(level string) error {
127+
_, err := logging.LogLevel(level)
128+
if err != nil {
129+
err = errors.ErrorWithCallstack("LOG", "400", "Invalid log level provided - %s", level)
130+
}
131+
return err
132+
}
133+
113134
// GetDefaultSigner return a default Signer(Default/PERR) for cli
114135
func GetDefaultSigner() (msp.SigningIdentity, error) {
115136
signer, err := mspmgmt.GetLocalMSP().GetDefaultSigningIdentity()

peer/core.yaml

+14-17
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
###############################################################################
66
logging:
77

8-
# Default logging levels are specified here for each of the three peer
9-
# commands 'node', 'network' and 'chaincode'. For commands that have
10-
# subcommands, the defaults also apply to all subcommands of the command.
8+
# Default logging levels are specified here.
9+
1110
# Valid logging levels are case-insensitive strings chosen from
1211

1312
# CRITICAL | ERROR | WARNING | NOTICE | INFO | DEBUG
@@ -21,22 +20,20 @@ logging:
2120
# 2. The environment variable CORE_LOGGING_LEVEL otherwise applies to
2221
# all peer commands if defined as a non-empty string.
2322
#
24-
# 3. The environment variables CORE_LOGGING_[NODE|NETWORK|CHAINCODE]
25-
# otherwise apply to the respective peer commands if defined as non-empty
26-
# strings.
27-
#
28-
# 4. Otherwise, the specifications below apply.
29-
#
30-
# Developers: Please see fabric/docs/Setup/logging-control.md for more
31-
# options.
32-
peer: warning
33-
node: info
34-
network: warning
35-
version: warning
36-
protoutils: debug
37-
error: warning
23+
# 3. Otherwise, the specifications below apply.
24+
25+
# Default for all modules running within the scope of a peer
26+
peer: info
27+
28+
# Override levels for various 'peer' modules
29+
gossip: info
30+
ledger: info
3831
msp: warning
3932

33+
# Error handling framework log level - when set to DEBUG, a callstack will
34+
# be appended to all errors generated using the fabric/core/errors package
35+
error: debug
36+
4037
format: '%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}'
4138

4239
###############################################################################

peer/main.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,15 @@ const cmdRoot = "core"
4949
var mainCmd = &cobra.Command{
5050
Use: "peer",
5151
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
52-
flogging.InitFromSpec(viper.GetString("logging_level"))
52+
// check for CORE_LOGGING_LEVEL environment variable, which should override
53+
// all other log settings
54+
loggingSpec := viper.GetString("logging_level")
55+
56+
if loggingSpec == "" {
57+
// if CORE_LOGGING_LEVEL not set, use the value for 'peer' from core.yaml
58+
loggingSpec = viper.GetString("logging.peer")
59+
}
60+
flogging.InitFromSpec(loggingSpec)
5361

5462
return core.CacheConfiguration()
5563
},

peer/node/start.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ var nodeStartCmd = &cobra.Command{
8080
Short: "Starts the node.",
8181
Long: `Starts a node that interacts with the network.`,
8282
RunE: func(cmd *cobra.Command, args []string) error {
83+
// initialize the log level for the "error" module to the value of
84+
// `logging.error` in core.yaml. this is necessary to ensure that the stack
85+
// is automatically appended to error messages (if set to debug)
86+
common.SetLogLevelFromViper("error")
8387
return serve(args)
8488
},
8589
}
@@ -259,11 +263,18 @@ func serve(args []string) error {
259263
logger.Infof("Started peer with ID=[%s], network ID=[%s], address=[%s]",
260264
peerEndpoint.Id, viper.GetString("peer.networkId"), peerEndpoint.Address)
261265

262-
// sets the logging level for the 'error' and 'msp' modules to the
263-
// values from core.yaml. they can also be updated dynamically using
264-
// "peer logging setlevel <module-name> <log-level>"
265-
common.SetLogLevelFromViper("error")
266-
common.SetLogLevelFromViper("msp")
266+
// if CORE_LOGGING_LEVEL environment variable is not set, set the logging
267+
// level for specific modules defined in core.yaml.
268+
// TODO Add calls to set 'gossip' and 'ledger' once all other packages
269+
// switch to `flogging.MustGetLogger` (from `logging.MustGetLogger`) as those
270+
// modules need the regular expression capabilities enabled by the `flogging`
271+
// package
272+
if viper.GetString("logging_level") == "" {
273+
err = common.SetLogLevelFromViper("msp")
274+
if err != nil {
275+
logger.Warningf("Error setting log level for module 'msp': %s", err.Error())
276+
}
277+
}
267278

268279
// TODO This check is here to preserve the old functionality until all
269280
// other packages switch to `flogging.MustGetLogger` (from

0 commit comments

Comments
 (0)