1 // Copyright (c) 2013-2017 The btcsuite developers
2 // Copyright (c) 2017 The Decred developers
3 // Use of this source code is governed by an ISC
4 // license that can be found in the LICENSE file.
13 "github.com/btcsuite/btcd/addrmgr"
14 "github.com/btcsuite/btcd/blockchain"
15 "github.com/btcsuite/btcd/blockchain/indexers"
16 "github.com/btcsuite/btcd/connmgr"
17 "github.com/btcsuite/btcd/database"
18 "github.com/btcsuite/btcd/mempool"
19 "github.com/btcsuite/btcd/mining"
20 "github.com/btcsuite/btcd/mining/cpuminer"
21 "github.com/btcsuite/btcd/netsync"
22 "github.com/btcsuite/btcd/peer"
23 "github.com/btcsuite/btcd/txscript"
24 "github.com/btcsuite/btclog"
25 "github.com/jrick/logrotate/rotator"
28 // logWriter implements an io.Writer that outputs to both standard output and
29 // the write-end pipe of an initialized log rotator.
30 type logWriter struct{}
32 func (logWriter) Write(p []byte) (n int, err error) {
38 // Loggers per subsystem. A single backend logger is created and all subsytem
39 // loggers created from it will write to the backend. When adding new
40 // subsystems, add the subsystem logger variable here and to the
41 // subsystemLoggers map.
43 // Loggers can not be used before the log rotator has been initialized with a
44 // log file. This must be performed early during application startup by calling
47 // backendLog is the logging backend used to create all subsystem loggers.
48 // The backend must not be used before the log rotator has been initialized,
49 // or data races and/or nil pointer dereferences will occur.
50 backendLog = btclog.NewBackend(logWriter{})
52 // logRotator is one of the logging outputs. It should be closed on
53 // application shutdown.
54 logRotator *rotator.Rotator
56 adxrLog = backendLog.Logger("ADXR")
57 amgrLog = backendLog.Logger("AMGR")
58 cmgrLog = backendLog.Logger("CMGR")
59 bcdbLog = backendLog.Logger("BCDB")
60 btcdLog = backendLog.Logger("BTCD")
61 chanLog = backendLog.Logger("CHAN")
62 discLog = backendLog.Logger("DISC")
63 indxLog = backendLog.Logger("INDX")
64 minrLog = backendLog.Logger("MINR")
65 peerLog = backendLog.Logger("PEER")
66 rpcsLog = backendLog.Logger("RPCS")
67 scrpLog = backendLog.Logger("SCRP")
68 srvrLog = backendLog.Logger("SRVR")
69 syncLog = backendLog.Logger("SYNC")
70 txmpLog = backendLog.Logger("TXMP")
73 // Initialize package-global logger variables.
75 addrmgr.UseLogger(amgrLog)
76 connmgr.UseLogger(cmgrLog)
77 database.UseLogger(bcdbLog)
78 blockchain.UseLogger(chanLog)
79 indexers.UseLogger(indxLog)
80 mining.UseLogger(minrLog)
81 cpuminer.UseLogger(minrLog)
82 peer.UseLogger(peerLog)
83 txscript.UseLogger(scrpLog)
84 netsync.UseLogger(syncLog)
85 mempool.UseLogger(txmpLog)
88 // subsystemLoggers maps each subsystem identifier to its associated logger.
89 var subsystemLoggers = map[string]btclog.Logger{
107 // initLogRotator initializes the logging rotater to write logs to logFile and
108 // create roll files in the same directory. It must be called before the
109 // package-global log rotater variables are used.
110 func initLogRotator(logFile string) {
111 logDir, _ := filepath.Split(logFile)
112 err := os.MkdirAll(logDir, 0700)
114 fmt.Fprintf(os.Stderr, "failed to create log directory: %v\n", err)
117 r, err := rotator.New(logFile, 10*1024, false, 3)
119 fmt.Fprintf(os.Stderr, "failed to create file rotator: %v\n", err)
126 // setLogLevel sets the logging level for provided subsystem. Invalid
127 // subsystems are ignored. Uninitialized subsystems are dynamically created as
129 func setLogLevel(subsystemID string, logLevel string) {
130 // Ignore invalid subsystems.
131 logger, ok := subsystemLoggers[subsystemID]
136 // Defaults to info if the log level is invalid.
137 level, _ := btclog.LevelFromString(logLevel)
138 logger.SetLevel(level)
141 // setLogLevels sets the log level for all subsystem loggers to the passed
142 // level. It also dynamically creates the subsystem loggers as needed, so it
143 // can be used to initialize the logging system.
144 func setLogLevels(logLevel string) {
145 // Configure all sub-systems with the new logging level. Dynamically
146 // create loggers as needed.
147 for subsystemID := range subsystemLoggers {
148 setLogLevel(subsystemID, logLevel)
152 // directionString is a helper function that returns a string that represents
153 // the direction of a connection (inbound or outbound).
154 func directionString(inbound bool) string {
161 // pickNoun returns the singular or plural form of a noun depending
163 func pickNoun(n uint64, singular, plural string) string {