log+config: remove global RootLogWriter variable

This commit is contained in:
Oliver Gugger 2020-05-25 18:34:00 +02:00
parent 70772ab99b
commit 75f916b026
No known key found for this signature in database
GPG Key ID: 8E4256593F177720
2 changed files with 93 additions and 69 deletions

@ -947,6 +947,7 @@ func ValidateConfig(cfg Config, usageMessage string) (*Config, error) {
} }
// Initialize logging at the default logging level. // Initialize logging at the default logging level.
SetupLoggers(cfg.LogWriter)
err = cfg.LogWriter.InitLogRotator( err = cfg.LogWriter.InitLogRotator(
filepath.Join(cfg.LogDir, defaultLogFilename), filepath.Join(cfg.LogDir, defaultLogFilename),
cfg.MaxLogFileSize, cfg.MaxLogFiles, cfg.MaxLogFileSize, cfg.MaxLogFiles,

161
log.go

@ -39,89 +39,112 @@ import (
"google.golang.org/grpc" "google.golang.org/grpc"
) )
// Loggers per subsystem. A single backend logger is created and all subsystem // replaceableLogger is a thin wrapper around a logger that is used so the
// loggers created from it will write to the backend. When adding new // logger can be replaced easily without some black pointer magic.
// subsystems, add the subsystem logger variable here and to the type replaceableLogger struct {
// subsystemLoggers map. btclog.Logger
// subsystem string
}
// 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 RootLogWriter.InitLogRotator. // calling InitLogRotator() on the main log writer instance in the config.
var ( var (
// RootLogWriter is the main parent log writer all sub loggers should be // lndPkgLoggers is a list of all lnd package level loggers that are
// appended to. // registered. They are tracked here so they can be replaced once the
RootLogWriter = build.NewRotatingLogWriter() // SetupLoggers function is called with the final root logger.
lndPkgLoggers []*replaceableLogger
// addLndPkgLogger is a helper function that creates a new replaceable
// main lnd package level logger and adds it to the list of loggers that
// are replaced again later, once the final root logger is ready.
addLndPkgLogger = func(subsystem string) *replaceableLogger {
l := &replaceableLogger{
Logger: build.NewSubLogger(subsystem, nil),
subsystem: subsystem,
}
lndPkgLoggers = append(lndPkgLoggers, l)
return l
}
// Loggers that need to be accessible from the lnd package can be placed // Loggers that need to be accessible from the lnd package can be placed
// here. Loggers that are only used in sub modules can be added directly // here. Loggers that are only used in sub modules can be added directly
// by using the addSubLogger method. // by using the addSubLogger method. We declare all loggers so we never
ltndLog = build.NewSubLogger("LTND", RootLogWriter.GenSubLogger) // run into a nil reference if they are used early. But the SetupLoggers
peerLog = build.NewSubLogger("PEER", RootLogWriter.GenSubLogger) // function should always be called as soon as possible to finish
rpcsLog = build.NewSubLogger("RPCS", RootLogWriter.GenSubLogger) // setting them up properly with a root logger.
srvrLog = build.NewSubLogger("SRVR", RootLogWriter.GenSubLogger) ltndLog = addLndPkgLogger("LTND")
fndgLog = build.NewSubLogger("FNDG", RootLogWriter.GenSubLogger) peerLog = addLndPkgLogger("PEER")
utxnLog = build.NewSubLogger("UTXN", RootLogWriter.GenSubLogger) rpcsLog = addLndPkgLogger("RPCS")
brarLog = build.NewSubLogger("BRAR", RootLogWriter.GenSubLogger) srvrLog = addLndPkgLogger("SRVR")
atplLog = build.NewSubLogger("ATPL", RootLogWriter.GenSubLogger) fndgLog = addLndPkgLogger("FNDG")
utxnLog = addLndPkgLogger("UTXN")
brarLog = addLndPkgLogger("BRAR")
atplLog = addLndPkgLogger("ATPL")
) )
// Initialize package-global logger variables. // SetupLoggers initializes all package-global logger variables.
func init() { func SetupLoggers(root *build.RotatingLogWriter) {
setSubLogger("LTND", ltndLog, signal.UseLogger) // Now that we have the proper root logger, we can replace the
setSubLogger("ATPL", atplLog, autopilot.UseLogger) // placeholder lnd package loggers.
setSubLogger("PEER", peerLog) for _, l := range lndPkgLoggers {
setSubLogger("RPCS", rpcsLog) l.Logger = build.NewSubLogger(l.subsystem, root.GenSubLogger)
setSubLogger("SRVR", srvrLog) SetSubLogger(root, l.subsystem, l.Logger)
setSubLogger("FNDG", fndgLog) }
setSubLogger("UTXN", utxnLog)
setSubLogger("BRAR", brarLog)
addSubLogger("LNWL", lnwallet.UseLogger) // Some of the loggers declared in the main lnd package are also used
addSubLogger("DISC", discovery.UseLogger) // in sub packages.
addSubLogger("NTFN", chainntnfs.UseLogger) signal.UseLogger(ltndLog)
addSubLogger("CHDB", channeldb.UseLogger) autopilot.UseLogger(atplLog)
addSubLogger("HSWC", htlcswitch.UseLogger)
addSubLogger("CMGR", connmgr.UseLogger)
addSubLogger("BTCN", neutrino.UseLogger)
addSubLogger("CNCT", contractcourt.UseLogger)
addSubLogger("SPHX", sphinx.UseLogger)
addSubLogger("SWPR", sweep.UseLogger)
addSubLogger("SGNR", signrpc.UseLogger)
addSubLogger("WLKT", walletrpc.UseLogger)
addSubLogger("ARPC", autopilotrpc.UseLogger)
addSubLogger("INVC", invoices.UseLogger)
addSubLogger("NANN", netann.UseLogger)
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("CHFD", chanfunding.UseLogger)
addSubLogger(routing.Subsystem, routing.UseLogger, localchans.UseLogger) AddSubLogger(root, "LNWL", lnwallet.UseLogger)
addSubLogger(routerrpc.Subsystem, routerrpc.UseLogger) AddSubLogger(root, "DISC", discovery.UseLogger)
addSubLogger(chanfitness.Subsystem, chanfitness.UseLogger) AddSubLogger(root, "NTFN", chainntnfs.UseLogger)
addSubLogger(verrpc.Subsystem, verrpc.UseLogger) AddSubLogger(root, "CHDB", channeldb.UseLogger)
AddSubLogger(root, "HSWC", htlcswitch.UseLogger)
AddSubLogger(root, "CMGR", connmgr.UseLogger)
AddSubLogger(root, "BTCN", neutrino.UseLogger)
AddSubLogger(root, "CNCT", contractcourt.UseLogger)
AddSubLogger(root, "SPHX", sphinx.UseLogger)
AddSubLogger(root, "SWPR", sweep.UseLogger)
AddSubLogger(root, "SGNR", signrpc.UseLogger)
AddSubLogger(root, "WLKT", walletrpc.UseLogger)
AddSubLogger(root, "ARPC", autopilotrpc.UseLogger)
AddSubLogger(root, "INVC", invoices.UseLogger)
AddSubLogger(root, "NANN", netann.UseLogger)
AddSubLogger(root, "WTWR", watchtower.UseLogger)
AddSubLogger(root, "NTFR", chainrpc.UseLogger)
AddSubLogger(root, "IRPC", invoicesrpc.UseLogger)
AddSubLogger(root, "CHNF", channelnotifier.UseLogger)
AddSubLogger(root, "CHBU", chanbackup.UseLogger)
AddSubLogger(root, "PROM", monitoring.UseLogger)
AddSubLogger(root, "WTCL", wtclient.UseLogger)
AddSubLogger(root, "PRNF", peernotifier.UseLogger)
AddSubLogger(root, "CHFD", chanfunding.UseLogger)
AddSubLogger(root, routing.Subsystem, routing.UseLogger, localchans.UseLogger)
AddSubLogger(root, routerrpc.Subsystem, routerrpc.UseLogger)
AddSubLogger(root, chanfitness.Subsystem, chanfitness.UseLogger)
AddSubLogger(root, verrpc.Subsystem, verrpc.UseLogger)
} }
// addSubLogger is a helper method to conveniently create and register the // AddSubLogger is a helper method to conveniently create and register the
// logger of one or more sub systems. // logger of one or more sub systems.
func addSubLogger(subsystem string, useLoggers ...func(btclog.Logger)) { func AddSubLogger(root *build.RotatingLogWriter, subsystem string,
// Create and register just a single logger to prevent them from
// overwriting each other internally.
logger := build.NewSubLogger(subsystem, RootLogWriter.GenSubLogger)
setSubLogger(subsystem, logger, useLoggers...)
}
// setSubLogger is a helper method to conveniently register the logger of a sub
// system.
func setSubLogger(subsystem string, logger btclog.Logger,
useLoggers ...func(btclog.Logger)) { useLoggers ...func(btclog.Logger)) {
RootLogWriter.RegisterSubLogger(subsystem, logger) // Create and register just a single logger to prevent them from
// overwriting each other internally.
logger := build.NewSubLogger(subsystem, root.GenSubLogger)
SetSubLogger(root, subsystem, logger, useLoggers...)
}
// SetSubLogger is a helper method to conveniently register the logger of a sub
// system.
func SetSubLogger(root *build.RotatingLogWriter, subsystem string,
logger btclog.Logger, useLoggers ...func(btclog.Logger)) {
root.RegisterSubLogger(subsystem, logger)
for _, useLogger := range useLoggers { for _, useLogger := range useLoggers {
useLogger(logger) useLogger(logger)
} }