log: initialize subloggers w/ build.LogWriter

and build.NewSubLogger
This commit is contained in:
Conner Fromknecht
2018-09-20 03:28:35 -07:00
parent 59b459674d
commit bfcda6e205

58
log.go
View File

@@ -12,8 +12,10 @@ import (
"github.com/btcsuite/btclog" "github.com/btcsuite/btclog"
"github.com/jrick/logrotate/rotator" "github.com/jrick/logrotate/rotator"
"github.com/lightninglabs/neutrino" "github.com/lightninglabs/neutrino"
"github.com/lightningnetwork/lightning-onion" "github.com/lightningnetwork/lightning-onion"
"github.com/lightningnetwork/lnd/autopilot" "github.com/lightningnetwork/lnd/autopilot"
"github.com/lightningnetwork/lnd/build"
"github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/contractcourt" "github.com/lightningnetwork/lnd/contractcourt"
@@ -24,16 +26,6 @@ import (
"github.com/lightningnetwork/lnd/signal" "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 per subsystem. A single backend logger is created and all subsystem
// loggers created from it will write to the backend. When adding new // loggers created from it will write to the backend. When adding new
// subsystems, add the subsystem logger variable here and to the // 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 // log file. This must be performed early during application startup by
// calling initLogRotator. // calling initLogRotator.
var ( var (
logWriter = &build.LogWriter{}
// backendLog is the logging backend used to create all subsystem // backendLog is the logging backend used to create all subsystem
// loggers. The backend must not be used before the log rotator has // loggers. The backend must not be used before the log rotator has
// been initialized, or data races and/or nil pointer dereferences will // been initialized, or data races and/or nil pointer dereferences will
// occur. // occur.
backendLog = btclog.NewBackend(logWriter{}) backendLog = btclog.NewBackend(logWriter)
// logRotator is one of the logging outputs. It should be closed on // logRotator is one of the logging outputs. It should be closed on
// application shutdown. // application shutdown.
logRotator *rotator.Rotator logRotator *rotator.Rotator
// logRotatorPipe is the write-end pipe for writing to the log rotator. ltndLog = build.NewSubLogger("LTND", backendLog.Logger)
// It is written to by the Write method of the logWriter type. lnwlLog = build.NewSubLogger("LNWL", backendLog.Logger)
logRotatorPipe *io.PipeWriter peerLog = build.NewSubLogger("PEER", backendLog.Logger)
discLog = build.NewSubLogger("DISC", backendLog.Logger)
ltndLog = backendLog.Logger("LTND") rpcsLog = build.NewSubLogger("RPCS", backendLog.Logger)
lnwlLog = backendLog.Logger("LNWL") srvrLog = build.NewSubLogger("SRVR", backendLog.Logger)
peerLog = backendLog.Logger("PEER") ntfnLog = build.NewSubLogger("NTFN", backendLog.Logger)
discLog = backendLog.Logger("DISC") chdbLog = build.NewSubLogger("CHDB", backendLog.Logger)
rpcsLog = backendLog.Logger("RPCS") fndgLog = build.NewSubLogger("FNDG", backendLog.Logger)
srvrLog = backendLog.Logger("SRVR") hswcLog = build.NewSubLogger("HSWC", backendLog.Logger)
ntfnLog = backendLog.Logger("NTFN") utxnLog = build.NewSubLogger("UTXN", backendLog.Logger)
chdbLog = backendLog.Logger("CHDB") brarLog = build.NewSubLogger("BRAR", backendLog.Logger)
fndgLog = backendLog.Logger("FNDG") cmgrLog = build.NewSubLogger("CMGR", backendLog.Logger)
hswcLog = backendLog.Logger("HSWC") crtrLog = build.NewSubLogger("CRTR", backendLog.Logger)
utxnLog = backendLog.Logger("UTXN") btcnLog = build.NewSubLogger("BTCN", backendLog.Logger)
brarLog = backendLog.Logger("BRAR") atplLog = build.NewSubLogger("ATPL", backendLog.Logger)
cmgrLog = backendLog.Logger("CMGR") cnctLog = build.NewSubLogger("CNCT", backendLog.Logger)
crtrLog = backendLog.Logger("CRTR") sphxLog = build.NewSubLogger("SPHX", backendLog.Logger)
btcnLog = backendLog.Logger("BTCN")
atplLog = backendLog.Logger("ATPL")
cnctLog = backendLog.Logger("CNCT")
sphxLog = backendLog.Logger("SPHX")
) )
// Initialize package-global logger variables. // Initialize package-global logger variables.
@@ -134,8 +124,8 @@ func initLogRotator(logFile string, MaxLogFileSize int, MaxLogFiles int) {
pr, pw := io.Pipe() pr, pw := io.Pipe()
go r.Run(pr) go r.Run(pr)
logWriter.RotatorPipe = pw
logRotator = r logRotator = r
logRotatorPipe = pw
} }
// setLogLevel sets the logging level for provided subsystem. Invalid // setLogLevel sets the logging level for provided subsystem. Invalid