Merge pull request #4320 from guggero/refactor-log-writer

config+log: remove global RootLogWriter variable
This commit is contained in:
Conner Fromknecht 2020-05-27 19:25:32 -07:00 committed by GitHub
commit 3faf8bad3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 110 additions and 75 deletions

@ -250,6 +250,10 @@ type Config struct {
DB *lncfg.DB `group:"db" namespace:"db"` DB *lncfg.DB `group:"db" namespace:"db"`
// LogWriter is the root logger that all of the daemon's subloggers are
// hooked up to.
LogWriter *build.RotatingLogWriter
// registeredChains keeps track of all chains that have been registered // registeredChains keeps track of all chains that have been registered
// with the daemon. // with the daemon.
registeredChains *chainRegistry registeredChains *chainRegistry
@ -360,6 +364,7 @@ func DefaultConfig() Config {
}, },
MaxOutgoingCltvExpiry: htlcswitch.DefaultMaxOutgoingCltvExpiry, MaxOutgoingCltvExpiry: htlcswitch.DefaultMaxOutgoingCltvExpiry,
MaxChannelFeeAllocation: htlcswitch.DefaultMaxLinkFeeAllocation, MaxChannelFeeAllocation: htlcswitch.DefaultMaxLinkFeeAllocation,
LogWriter: build.NewRotatingLogWriter(),
DB: lncfg.DefaultDB(), DB: lncfg.DefaultDB(),
registeredChains: newChainRegistry(), registeredChains: newChainRegistry(),
} }
@ -928,15 +933,22 @@ func ValidateConfig(cfg Config, usageMessage string) (*Config, error) {
cfg.registeredChains.PrimaryChain().String(), cfg.registeredChains.PrimaryChain().String(),
normalizeNetwork(activeNetParams.Name)) normalizeNetwork(activeNetParams.Name))
// A log writer must be passed in, otherwise we can't function and would
// run into a panic later on.
if cfg.LogWriter == nil {
return nil, fmt.Errorf("log writer missing in config")
}
// Special show command to list supported subsystems and exit. // Special show command to list supported subsystems and exit.
if cfg.DebugLevel == "show" { if cfg.DebugLevel == "show" {
fmt.Println("Supported subsystems", fmt.Println("Supported subsystems",
RootLogWriter.SupportedSubsystems()) cfg.LogWriter.SupportedSubsystems())
os.Exit(0) os.Exit(0)
} }
// Initialize logging at the default logging level. // Initialize logging at the default logging level.
err = RootLogWriter.InitLogRotator( SetupLoggers(cfg.LogWriter)
err = cfg.LogWriter.InitLogRotator(
filepath.Join(cfg.LogDir, defaultLogFilename), filepath.Join(cfg.LogDir, defaultLogFilename),
cfg.MaxLogFileSize, cfg.MaxLogFiles, cfg.MaxLogFileSize, cfg.MaxLogFiles,
) )
@ -948,7 +960,7 @@ func ValidateConfig(cfg Config, usageMessage string) (*Config, error) {
} }
// Parse, validate, and set debug log level(s). // Parse, validate, and set debug log level(s).
err = build.ParseAndSetDebugLevels(cfg.DebugLevel, RootLogWriter) err = build.ParseAndSetDebugLevels(cfg.DebugLevel, cfg.LogWriter)
if err != nil { if err != nil {
err = fmt.Errorf("%s: %v", funcName, err.Error()) err = fmt.Errorf("%s: %v", funcName, err.Error())
_, _ = fmt.Fprintln(os.Stderr, err) _, _ = fmt.Fprintln(os.Stderr, err)

2
lnd.go

@ -190,7 +190,7 @@ type rpcListeners func() ([]*ListenerWithSignal, func(), error)
func Main(cfg *Config, lisCfg ListenerCfg, shutdownChan <-chan struct{}) error { func Main(cfg *Config, lisCfg ListenerCfg, shutdownChan <-chan struct{}) error {
defer func() { defer func() {
ltndLog.Info("Shutdown complete") ltndLog.Info("Shutdown complete")
err := RootLogWriter.Close() err := cfg.LogWriter.Close()
if err != nil { if err != nil {
ltndLog.Errorf("Could not close log rotator: %v", err) ltndLog.Errorf("Could not close log rotator: %v", err)
} }

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)
} }

@ -5298,7 +5298,7 @@ func (r *rpcServer) DebugLevel(ctx context.Context,
if req.Show { if req.Show {
return &lnrpc.DebugLevelResponse{ return &lnrpc.DebugLevelResponse{
SubSystems: strings.Join( SubSystems: strings.Join(
RootLogWriter.SupportedSubsystems(), " ", r.cfg.LogWriter.SupportedSubsystems(), " ",
), ),
}, nil }, nil
} }
@ -5307,7 +5307,7 @@ func (r *rpcServer) DebugLevel(ctx context.Context,
// Otherwise, we'll attempt to set the logging level using the // Otherwise, we'll attempt to set the logging level using the
// specified level spec. // specified level spec.
err := build.ParseAndSetDebugLevels(req.LevelSpec, RootLogWriter) err := build.ParseAndSetDebugLevels(req.LevelSpec, r.cfg.LogWriter)
if err != nil { if err != nil {
return nil, err return nil, err
} }