Skip to content

Commit ecfca45

Browse files
committed
Add ability to customize peer logging format
The logging.format string defined in peer/core.yaml will be used to format all logging messages on the peer. See https://godoc.org/github.com/op/go-logging#NewStringFormatter for more details on customizing the format string. Change-Id: I57312099836867206956fd6229f3d1d6e9734c11 Signed-off-by: Will Lahti <[email protected]>
1 parent 46f7af0 commit ecfca45

File tree

4 files changed

+65
-8
lines changed

4 files changed

+65
-8
lines changed

core/flogging/logging.go

+21-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package flogging
1818

1919
import (
20+
"io"
2021
"os"
2122
"strings"
2223

@@ -27,6 +28,9 @@ import (
2728
// A logger to log logging logs!
2829
var loggingLogger = logging.MustGetLogger("logging")
2930

31+
var loggingDefaultFormat = "%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}"
32+
var loggingDefaultOutput = os.Stderr
33+
3034
// The default logging level, in force until LoggingInit() is called or in
3135
// case of configuration errors.
3236
var loggingDefaultLevel = logging.INFO
@@ -87,15 +91,26 @@ func DefaultLoggingLevel() logging.Level {
8791
return loggingDefaultLevel
8892
}
8993

90-
// Initiate 'leveled' logging to stderr.
94+
// Initiate 'leveled' logging using the default format and output location
9195
func init() {
96+
SetLoggingFormat(loggingDefaultFormat, loggingDefaultOutput)
97+
}
9298

93-
format := logging.MustStringFormatter(
94-
"%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}",
95-
)
99+
// SetLoggingFormat sets the logging format and the location of the log output
100+
func SetLoggingFormat(formatString string, output io.Writer) {
101+
if formatString == "" {
102+
formatString = loggingDefaultFormat
103+
}
104+
format := logging.MustStringFormatter(formatString)
105+
106+
initLoggingBackend(format, output)
107+
}
96108

97-
backend := logging.NewLogBackend(os.Stderr, "", 0)
98-
backendFormatter := logging.NewBackendFormatter(backend, format)
109+
// initialize the logging backend based on the provided logging formatter
110+
// and I/O writer
111+
func initLoggingBackend(logFormatter logging.Formatter, output io.Writer) {
112+
backend := logging.NewLogBackend(output, "", 0)
113+
backendFormatter := logging.NewBackendFormatter(backend, logFormatter)
99114
logging.SetBackend(backendFormatter).SetLevel(loggingDefaultLevel, "")
100115
}
101116

core/flogging/logging_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package flogging_test
1818

1919
import (
20+
"os"
2021
"testing"
2122

2223
"github.com/hyperledger/fabric/core/flogging"
@@ -186,6 +187,36 @@ func TestSetModuleLoggingLevelInvalid(t *testing.T) {
186187
assertModuleLoggingLevel(t, "peer", logging.WARNING)
187188
}
188189

190+
func ExampleSetLoggingFormat() {
191+
// initializes logging backend for testing and sets
192+
// time to 1970-01-01 00:00:00.000 UTC
193+
logging.InitForTesting(flogging.DefaultLoggingLevel())
194+
195+
logFormat := "%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x} %{message}"
196+
flogging.SetLoggingFormat(logFormat, os.Stdout)
197+
198+
logger := logging.MustGetLogger("floggingTest")
199+
logger.Infof("test")
200+
201+
// Output:
202+
// 1970-01-01 00:00:00.000 UTC [floggingTest] ExampleSetLoggingFormat -> INFO 001 test
203+
}
204+
205+
func ExampleSetLoggingFormat_second() {
206+
// initializes logging backend for testing and sets
207+
// time to 1970-01-01 00:00:00.000 UTC
208+
logging.InitForTesting(flogging.DefaultLoggingLevel())
209+
210+
logFormat := "%{time:15:04:05.000} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x} %{message}"
211+
flogging.SetLoggingFormat(logFormat, os.Stdout)
212+
213+
logger := logging.MustGetLogger("floggingTest")
214+
logger.Infof("test")
215+
216+
// Output:
217+
// 00:00:00.000 [floggingTest] ExampleSetLoggingFormat_second -> INFO 001 test
218+
}
219+
189220
func assertDefaultLoggingLevel(t *testing.T, expectedLevel logging.Level) {
190221
assertModuleLoggingLevel(t, "", expectedLevel)
191222
}

peer/core.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ logging:
7272
protoutils: debug
7373
error: warning
7474

75+
format: '%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}'
76+
7577
###############################################################################
7678
#
7779
# Peer section
@@ -417,9 +419,9 @@ ledger:
417419
username:
418420
password:
419421
# historyDatabase - options are true or false
420-
# Indicates if the transaction history should be stored in
422+
# Indicates if the transaction history should be stored in
421423
# a querable database such as "CouchDB".
422-
# The stateDatabase must be also stored in CouchDB for
424+
# The stateDatabase must be also stored in CouchDB for
423425
# history to be enabled.
424426
historyDatabase: false
425427

peer/main.go

+9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
_ "net/http/pprof"
3030

3131
"github.com/hyperledger/fabric/core"
32+
"github.com/hyperledger/fabric/core/crypto/primitives"
3233
"github.com/hyperledger/fabric/core/flogging"
3334
"github.com/hyperledger/fabric/peer/chaincode"
3435
"github.com/hyperledger/fabric/peer/clilogging"
@@ -38,6 +39,7 @@ import (
3839
)
3940

4041
var logger = logging.MustGetLogger("main")
42+
var logOutput = os.Stderr
4143

4244
// Constants go here.
4345
const cmdRoot = "core"
@@ -93,6 +95,13 @@ func main() {
9395

9496
runtime.GOMAXPROCS(viper.GetInt("peer.gomaxprocs"))
9597

98+
// initialize logging format from core.yaml
99+
flogging.SetLoggingFormat(viper.GetString("logging.format"), logOutput)
100+
101+
// Init the crypto layer
102+
//TODO: integrate new crypto / idp code
103+
primitives.SetSecurityLevel("SHA2", 256)
104+
96105
// Init the MSP
97106
// TODO: determine the location of this config file
98107
var mspMgrConfigDir string

0 commit comments

Comments
 (0)