From bfcda6e205391214417fcd84ec9f62c7bac738e7 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Thu, 20 Sep 2018 03:28:35 -0700 Subject: [PATCH] log: initialize subloggers w/ build.LogWriter and build.NewSubLogger --- log.go | 58 ++++++++++++++++++++++++---------------------------------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/log.go b/log.go index 01ba080f..1fe1b658 100644 --- a/log.go +++ b/log.go @@ -12,8 +12,10 @@ import ( "github.com/btcsuite/btclog" "github.com/jrick/logrotate/rotator" "github.com/lightninglabs/neutrino" + "github.com/lightningnetwork/lightning-onion" "github.com/lightningnetwork/lnd/autopilot" + "github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/contractcourt" @@ -24,16 +26,6 @@ import ( "github.com/lightningnetwork/lnd/signal" ) -// logWriter implements an io.Writer that outputs to both standard output and -// the write-end pipe of an initialized log rotator. -type logWriter struct{} - -func (logWriter) Write(p []byte) (n int, err error) { - os.Stdout.Write(p) - logRotatorPipe.Write(p) - return len(p), nil -} - // Loggers per subsystem. A single backend logger is created and all subsystem // loggers created from it will write to the backend. When adding new // subsystems, add the subsystem logger variable here and to the @@ -43,38 +35,36 @@ func (logWriter) Write(p []byte) (n int, err error) { // log file. This must be performed early during application startup by // calling initLogRotator. var ( + logWriter = &build.LogWriter{} + // backendLog is the logging backend used to create all subsystem // loggers. The backend must not be used before the log rotator has // been initialized, or data races and/or nil pointer dereferences will // occur. - backendLog = btclog.NewBackend(logWriter{}) + backendLog = btclog.NewBackend(logWriter) // logRotator is one of the logging outputs. It should be closed on // application shutdown. logRotator *rotator.Rotator - // logRotatorPipe is the write-end pipe for writing to the log rotator. - // It is written to by the Write method of the logWriter type. - logRotatorPipe *io.PipeWriter - - ltndLog = backendLog.Logger("LTND") - lnwlLog = backendLog.Logger("LNWL") - peerLog = backendLog.Logger("PEER") - discLog = backendLog.Logger("DISC") - rpcsLog = backendLog.Logger("RPCS") - srvrLog = backendLog.Logger("SRVR") - ntfnLog = backendLog.Logger("NTFN") - chdbLog = backendLog.Logger("CHDB") - fndgLog = backendLog.Logger("FNDG") - hswcLog = backendLog.Logger("HSWC") - utxnLog = backendLog.Logger("UTXN") - brarLog = backendLog.Logger("BRAR") - cmgrLog = backendLog.Logger("CMGR") - crtrLog = backendLog.Logger("CRTR") - btcnLog = backendLog.Logger("BTCN") - atplLog = backendLog.Logger("ATPL") - cnctLog = backendLog.Logger("CNCT") - sphxLog = backendLog.Logger("SPHX") + ltndLog = build.NewSubLogger("LTND", backendLog.Logger) + 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) ) // Initialize package-global logger variables. @@ -134,8 +124,8 @@ func initLogRotator(logFile string, MaxLogFileSize int, MaxLogFiles int) { pr, pw := io.Pipe() go r.Run(pr) + logWriter.RotatorPipe = pw logRotator = r - logRotatorPipe = pw } // setLogLevel sets the logging level for provided subsystem. Invalid