lnd: refactor sub logger setup to use new rotating log writer

This commit is contained in:
Oliver Gugger 2019-09-23 13:20:14 +02:00
parent d3995a73ae
commit 94f49192cf
No known key found for this signature in database
GPG Key ID: 8E4256593F177720
2 changed files with 62 additions and 112 deletions

5
lnd.go

@ -124,10 +124,11 @@ func Main(lisCfg ListenerCfg) error {
} }
cfg = loadedConfig cfg = loadedConfig
defer func() { defer func() {
if logRotator != nil {
ltndLog.Info("Shutdown complete") ltndLog.Info("Shutdown complete")
/* TODO(guggero) fix after refactor
if logRotator != nil {
logRotator.Close() logRotator.Close()
} }*/
}() }()
// Show version at startup. // Show version at startup.

169
log.go

@ -5,7 +5,6 @@ import (
"github.com/btcsuite/btcd/connmgr" "github.com/btcsuite/btcd/connmgr"
"github.com/btcsuite/btclog" "github.com/btcsuite/btclog"
"github.com/jrick/logrotate/rotator"
"github.com/lightninglabs/neutrino" "github.com/lightninglabs/neutrino"
sphinx "github.com/lightningnetwork/lightning-onion" sphinx "github.com/lightningnetwork/lightning-onion"
"github.com/lightningnetwork/lnd/autopilot" "github.com/lightningnetwork/lnd/autopilot"
@ -44,129 +43,79 @@ import (
// //
// Loggers can not be used before the log rotator has been initialized with a // Loggers can not be used before the log rotator has been initialized with a
// log file. This must be performed early during application startup by // log file. This must be performed early during application startup by
// calling initLogRotator. // calling logWriter.InitLogRotator.
var ( var (
logWriter = &build.LogWriter{} logWriter = build.NewRotatingLogWriter()
// backendLog is the logging backend used to create all subsystem // Loggers that need to be accessible from the lnd package can be placed
// loggers. The backend must not be used before the log rotator has // here. Loggers that are only used in sub modules can be added directly
// been initialized, or data races and/or nil pointer dereferences will // by using the addSubLogger method.
// occur. ltndLog = build.NewSubLogger("LTND", logWriter.GenSubLogger)
backendLog = btclog.NewBackend(logWriter) peerLog = build.NewSubLogger("PEER", logWriter.GenSubLogger)
rpcsLog = build.NewSubLogger("RPCS", logWriter.GenSubLogger)
// logRotator is one of the logging outputs. It should be closed on srvrLog = build.NewSubLogger("SRVR", logWriter.GenSubLogger)
// application shutdown. fndgLog = build.NewSubLogger("FNDG", logWriter.GenSubLogger)
logRotator *rotator.Rotator utxnLog = build.NewSubLogger("UTXN", logWriter.GenSubLogger)
brarLog = build.NewSubLogger("BRAR", logWriter.GenSubLogger)
ltndLog = build.NewSubLogger("LTND", backendLog.Logger) atplLog = build.NewSubLogger("ATPL", logWriter.GenSubLogger)
lnwlLog = build.NewSubLogger("LNWL", backendLog.Logger)
peerLog = build.NewSubLogger("PEER", backendLog.Logger)
discLog = build.NewSubLogger("DISC", backendLog.Logger)
rpcsLog = build.NewSubLogger("RPCS", backendLog.Logger)
srvrLog = build.NewSubLogger("SRVR", backendLog.Logger)
ntfnLog = build.NewSubLogger("NTFN", backendLog.Logger)
chdbLog = build.NewSubLogger("CHDB", backendLog.Logger)
fndgLog = build.NewSubLogger("FNDG", backendLog.Logger)
hswcLog = build.NewSubLogger("HSWC", backendLog.Logger)
utxnLog = build.NewSubLogger("UTXN", backendLog.Logger)
brarLog = build.NewSubLogger("BRAR", backendLog.Logger)
cmgrLog = build.NewSubLogger("CMGR", backendLog.Logger)
crtrLog = build.NewSubLogger("CRTR", backendLog.Logger)
btcnLog = build.NewSubLogger("BTCN", backendLog.Logger)
atplLog = build.NewSubLogger("ATPL", backendLog.Logger)
cnctLog = build.NewSubLogger("CNCT", backendLog.Logger)
sphxLog = build.NewSubLogger("SPHX", backendLog.Logger)
swprLog = build.NewSubLogger("SWPR", backendLog.Logger)
sgnrLog = build.NewSubLogger("SGNR", backendLog.Logger)
wlktLog = build.NewSubLogger("WLKT", backendLog.Logger)
arpcLog = build.NewSubLogger("ARPC", backendLog.Logger)
invcLog = build.NewSubLogger("INVC", backendLog.Logger)
nannLog = build.NewSubLogger("NANN", backendLog.Logger)
wtwrLog = build.NewSubLogger("WTWR", backendLog.Logger)
ntfrLog = build.NewSubLogger("NTFR", backendLog.Logger)
irpcLog = build.NewSubLogger("IRPC", backendLog.Logger)
chnfLog = build.NewSubLogger("CHNF", backendLog.Logger)
chbuLog = build.NewSubLogger("CHBU", backendLog.Logger)
promLog = build.NewSubLogger("PROM", backendLog.Logger)
wtclLog = build.NewSubLogger("WTCL", backendLog.Logger)
prnfLog = build.NewSubLogger("PRNF", backendLog.Logger)
) )
// Initialize package-global logger variables. // Initialize package-global logger variables.
func init() { func init() {
lnwallet.UseLogger(lnwlLog) setSubLogger("LTND", ltndLog, signal.UseLogger)
discovery.UseLogger(discLog) setSubLogger("ATPL", atplLog, autopilot.UseLogger)
chainntnfs.UseLogger(ntfnLog) setSubLogger("PEER", peerLog, nil)
channeldb.UseLogger(chdbLog) setSubLogger("RPCS", rpcsLog, nil)
htlcswitch.UseLogger(hswcLog) setSubLogger("SRVR", srvrLog, nil)
connmgr.UseLogger(cmgrLog) setSubLogger("FNDG", fndgLog, nil)
routing.UseLogger(crtrLog) setSubLogger("UTXN", utxnLog, nil)
neutrino.UseLogger(btcnLog) setSubLogger("BRAR", brarLog, nil)
autopilot.UseLogger(atplLog)
contractcourt.UseLogger(cnctLog) addSubLogger("LNWL", lnwallet.UseLogger)
sphinx.UseLogger(sphxLog) addSubLogger("DISC", discovery.UseLogger)
signal.UseLogger(ltndLog) addSubLogger("NTFN", chainntnfs.UseLogger)
sweep.UseLogger(swprLog) addSubLogger("CHDB", channeldb.UseLogger)
signrpc.UseLogger(sgnrLog) addSubLogger("HSWC", htlcswitch.UseLogger)
walletrpc.UseLogger(wlktLog) addSubLogger("CMGR", connmgr.UseLogger)
autopilotrpc.UseLogger(arpcLog) addSubLogger("CRTR", routing.UseLogger)
invoices.UseLogger(invcLog) addSubLogger("BTCN", neutrino.UseLogger)
netann.UseLogger(nannLog) addSubLogger("CNCT", contractcourt.UseLogger)
watchtower.UseLogger(wtwrLog) addSubLogger("SPHX", sphinx.UseLogger)
chainrpc.UseLogger(ntfrLog) addSubLogger("SWPR", sweep.UseLogger)
invoicesrpc.UseLogger(irpcLog) addSubLogger("SGNR", signrpc.UseLogger)
channelnotifier.UseLogger(chnfLog) addSubLogger("WLKT", walletrpc.UseLogger)
chanbackup.UseLogger(chbuLog) addSubLogger("ARPC", autopilotrpc.UseLogger)
monitoring.UseLogger(promLog) addSubLogger("INVC", invoices.UseLogger)
wtclient.UseLogger(wtclLog) addSubLogger("NANN", netann.UseLogger)
peernotifier.UseLogger(prnfLog) addSubLogger("WTWR", watchtower.UseLogger)
addSubLogger("NTFR", chainrpc.UseLogger)
addSubLogger("IRPC", invoicesrpc.UseLogger)
addSubLogger("CHNF", channelnotifier.UseLogger)
addSubLogger("CHBU", chanbackup.UseLogger)
addSubLogger("PROM", monitoring.UseLogger)
addSubLogger("WTCL", wtclient.UseLogger)
addSubLogger("PRNF", peernotifier.UseLogger)
addSubLogger(routerrpc.Subsystem, routerrpc.UseLogger) addSubLogger(routerrpc.Subsystem, routerrpc.UseLogger)
addSubLogger(wtclientrpc.Subsystem, wtclientrpc.UseLogger) addSubLogger(wtclientrpc.Subsystem, wtclientrpc.UseLogger)
} }
// addSubLogger is a helper method to conveniently register the logger of a sub // addSubLogger is a helper method to conveniently create and register the
// system. // logger of a sub system.
func addSubLogger(subsystem string, useLogger func(btclog.Logger)) { func addSubLogger(subsystem string, useLogger func(btclog.Logger)) {
logger := build.NewSubLogger(subsystem, backendLog.Logger) logger := build.NewSubLogger(subsystem, logWriter.GenSubLogger)
useLogger(logger) setSubLogger(subsystem, logger, useLogger)
subsystemLoggers[subsystem] = logger
} }
// subsystemLoggers maps each subsystem identifier to its associated logger. // setSubLogger is a helper method to conveniently register the logger of a sub
var subsystemLoggers = map[string]btclog.Logger{ // system.
"LTND": ltndLog, func setSubLogger(subsystem string, logger btclog.Logger,
"LNWL": lnwlLog, useLogger func(btclog.Logger)) {
"PEER": peerLog,
"DISC": discLog, logWriter.RegisterSubLogger(subsystem, logger)
"RPCS": rpcsLog, if useLogger != nil {
"SRVR": srvrLog, useLogger(logger)
"NTFN": ntfnLog, }
"CHDB": chdbLog,
"FNDG": fndgLog,
"HSWC": hswcLog,
"UTXN": utxnLog,
"BRAR": brarLog,
"CMGR": cmgrLog,
"CRTR": crtrLog,
"BTCN": btcnLog,
"ATPL": atplLog,
"CNCT": cnctLog,
"SPHX": sphxLog,
"SWPR": swprLog,
"SGNR": sgnrLog,
"WLKT": wlktLog,
"ARPC": arpcLog,
"INVC": invcLog,
"NANN": nannLog,
"WTWR": wtwrLog,
"NTFR": ntfrLog,
"IRPC": irpcLog,
"CHNF": chnfLog,
"CHBU": chbuLog,
"PROM": promLog,
"WTCL": wtclLog,
"PRNF": prnfLog,
} }
// logClosure is used to provide a closure over expensive logging operations so // logClosure is used to provide a closure over expensive logging operations so