2019-01-24 14:28:25 +01:00
|
|
|
package lnd
|
2016-03-23 02:43:10 +01:00
|
|
|
|
|
|
|
import (
|
2018-06-05 03:34:16 +02:00
|
|
|
"github.com/btcsuite/btcd/connmgr"
|
2016-03-23 02:43:10 +01:00
|
|
|
"github.com/btcsuite/btclog"
|
2017-05-18 20:48:10 +02:00
|
|
|
"github.com/lightninglabs/neutrino"
|
2019-03-27 11:44:30 +01:00
|
|
|
sphinx "github.com/lightningnetwork/lightning-onion"
|
2017-08-11 06:39:45 +02:00
|
|
|
"github.com/lightningnetwork/lnd/autopilot"
|
2018-09-20 12:28:35 +02:00
|
|
|
"github.com/lightningnetwork/lnd/build"
|
2016-08-04 21:37:50 +02:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2020-11-09 13:07:45 +01:00
|
|
|
"github.com/lightningnetwork/lnd/chainreg"
|
2020-11-09 08:34:49 +01:00
|
|
|
"github.com/lightningnetwork/lnd/chanacceptor"
|
2019-02-09 04:39:38 +01:00
|
|
|
"github.com/lightningnetwork/lnd/chanbackup"
|
2019-08-08 19:39:13 +02:00
|
|
|
"github.com/lightningnetwork/lnd/chanfitness"
|
2016-03-23 02:43:10 +01:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2018-10-22 05:36:56 +02:00
|
|
|
"github.com/lightningnetwork/lnd/channelnotifier"
|
2021-02-08 21:54:38 +01:00
|
|
|
"github.com/lightningnetwork/lnd/cluster"
|
2018-01-17 05:25:34 +01:00
|
|
|
"github.com/lightningnetwork/lnd/contractcourt"
|
2017-03-19 19:40:25 +01:00
|
|
|
"github.com/lightningnetwork/lnd/discovery"
|
2020-11-24 15:38:30 +01:00
|
|
|
"github.com/lightningnetwork/lnd/funding"
|
2020-08-24 08:54:38 +02:00
|
|
|
"github.com/lightningnetwork/lnd/healthcheck"
|
2017-05-02 22:04:58 +02:00
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch"
|
2019-01-05 02:34:59 +01:00
|
|
|
"github.com/lightningnetwork/lnd/invoices"
|
2018-12-13 12:26:30 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/autopilotrpc"
|
2018-12-11 03:34:36 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/chainrpc"
|
2018-12-20 11:42:28 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
|
2019-03-27 11:44:30 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
|
2018-10-23 02:42:21 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/signrpc"
|
2020-04-10 02:05:46 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/verrpc"
|
2018-10-25 04:32:37 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
|
2016-03-23 02:43:10 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2020-06-17 02:33:06 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chancloser"
|
2019-11-01 05:32:04 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chanfunding"
|
2019-05-31 09:59:27 +02:00
|
|
|
"github.com/lightningnetwork/lnd/monitoring"
|
2019-01-05 02:34:59 +01:00
|
|
|
"github.com/lightningnetwork/lnd/netann"
|
2020-07-02 23:46:06 +02:00
|
|
|
"github.com/lightningnetwork/lnd/peer"
|
2019-07-29 16:59:48 +02:00
|
|
|
"github.com/lightningnetwork/lnd/peernotifier"
|
2016-12-27 06:42:23 +01:00
|
|
|
"github.com/lightningnetwork/lnd/routing"
|
2019-11-15 11:15:22 +01:00
|
|
|
"github.com/lightningnetwork/lnd/routing/localchans"
|
2018-06-15 05:15:56 +02:00
|
|
|
"github.com/lightningnetwork/lnd/signal"
|
2018-09-26 07:03:16 +02:00
|
|
|
"github.com/lightningnetwork/lnd/sweep"
|
2019-01-12 03:32:40 +01:00
|
|
|
"github.com/lightningnetwork/lnd/watchtower"
|
2019-06-14 02:29:09 +02:00
|
|
|
"github.com/lightningnetwork/lnd/watchtower/wtclient"
|
2016-03-23 02:43:10 +01:00
|
|
|
)
|
|
|
|
|
2020-05-25 18:34:00 +02:00
|
|
|
// replaceableLogger is a thin wrapper around a logger that is used so the
|
|
|
|
// logger can be replaced easily without some black pointer magic.
|
|
|
|
type replaceableLogger struct {
|
|
|
|
btclog.Logger
|
|
|
|
subsystem string
|
|
|
|
}
|
|
|
|
|
2017-06-21 17:07:44 +02:00
|
|
|
// Loggers can not be used before the log rotator has been initialized with a
|
2020-05-25 18:34:00 +02:00
|
|
|
// log file. This must be performed early during application startup by
|
|
|
|
// calling InitLogRotator() on the main log writer instance in the config.
|
2016-03-23 02:43:10 +01:00
|
|
|
var (
|
2020-05-25 18:34:00 +02:00
|
|
|
// lndPkgLoggers is a list of all lnd package level loggers that are
|
|
|
|
// registered. They are tracked here so they can be replaced once the
|
|
|
|
// 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
|
|
|
|
}
|
2018-09-20 12:28:35 +02:00
|
|
|
|
2019-09-23 13:20:14 +02:00
|
|
|
// 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
|
2020-05-25 18:34:00 +02:00
|
|
|
// by using the addSubLogger method. We declare all loggers so we never
|
|
|
|
// run into a nil reference if they are used early. But the SetupLoggers
|
|
|
|
// function should always be called as soon as possible to finish
|
|
|
|
// setting them up properly with a root logger.
|
|
|
|
ltndLog = addLndPkgLogger("LTND")
|
|
|
|
rpcsLog = addLndPkgLogger("RPCS")
|
|
|
|
srvrLog = addLndPkgLogger("SRVR")
|
|
|
|
utxnLog = addLndPkgLogger("UTXN")
|
|
|
|
brarLog = addLndPkgLogger("BRAR")
|
|
|
|
atplLog = addLndPkgLogger("ATPL")
|
2016-03-23 02:43:10 +01:00
|
|
|
)
|
|
|
|
|
2020-08-28 11:18:15 +02:00
|
|
|
// genSubLogger creates a logger for a subsystem. We provide an instance of
|
|
|
|
// a signal.Interceptor to be able to shutdown in the case of a critical error.
|
|
|
|
func genSubLogger(root *build.RotatingLogWriter,
|
|
|
|
interceptor signal.Interceptor) func(string) btclog.Logger {
|
|
|
|
|
|
|
|
// Create a shutdown function which will request shutdown from our
|
|
|
|
// interceptor if it is listening.
|
|
|
|
shutdown := func() {
|
|
|
|
if !interceptor.Listening() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
interceptor.RequestShutdown()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a function which will create a sublogger from our root
|
|
|
|
// logger without shutdown fn.
|
|
|
|
return func(tag string) btclog.Logger {
|
|
|
|
return root.GenSubLogger(tag, shutdown)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 18:34:00 +02:00
|
|
|
// SetupLoggers initializes all package-global logger variables.
|
2020-08-28 11:18:15 +02:00
|
|
|
func SetupLoggers(root *build.RotatingLogWriter, interceptor signal.Interceptor) {
|
|
|
|
genLogger := genSubLogger(root, interceptor)
|
|
|
|
|
2020-05-25 18:34:00 +02:00
|
|
|
// Now that we have the proper root logger, we can replace the
|
|
|
|
// placeholder lnd package loggers.
|
|
|
|
for _, l := range lndPkgLoggers {
|
2020-08-28 11:18:15 +02:00
|
|
|
l.Logger = build.NewSubLogger(l.subsystem, genLogger)
|
2020-05-25 18:34:00 +02:00
|
|
|
SetSubLogger(root, l.subsystem, l.Logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some of the loggers declared in the main lnd package are also used
|
|
|
|
// in sub packages.
|
|
|
|
signal.UseLogger(ltndLog)
|
|
|
|
autopilot.UseLogger(atplLog)
|
|
|
|
|
2020-08-28 11:18:15 +02:00
|
|
|
AddSubLogger(root, "LNWL", interceptor, lnwallet.UseLogger)
|
|
|
|
AddSubLogger(root, "DISC", interceptor, discovery.UseLogger)
|
|
|
|
AddSubLogger(root, "NTFN", interceptor, chainntnfs.UseLogger)
|
|
|
|
AddSubLogger(root, "CHDB", interceptor, channeldb.UseLogger)
|
|
|
|
AddSubLogger(root, "HSWC", interceptor, htlcswitch.UseLogger)
|
|
|
|
AddSubLogger(root, "CMGR", interceptor, connmgr.UseLogger)
|
|
|
|
AddSubLogger(root, "BTCN", interceptor, neutrino.UseLogger)
|
|
|
|
AddSubLogger(root, "CNCT", interceptor, contractcourt.UseLogger)
|
|
|
|
AddSubLogger(root, "SPHX", interceptor, sphinx.UseLogger)
|
|
|
|
AddSubLogger(root, "SWPR", interceptor, sweep.UseLogger)
|
|
|
|
AddSubLogger(root, "SGNR", interceptor, signrpc.UseLogger)
|
|
|
|
AddSubLogger(root, "WLKT", interceptor, walletrpc.UseLogger)
|
|
|
|
AddSubLogger(root, "ARPC", interceptor, autopilotrpc.UseLogger)
|
|
|
|
AddSubLogger(root, "INVC", interceptor, invoices.UseLogger)
|
|
|
|
AddSubLogger(root, "NANN", interceptor, netann.UseLogger)
|
|
|
|
AddSubLogger(root, "WTWR", interceptor, watchtower.UseLogger)
|
|
|
|
AddSubLogger(root, "NTFR", interceptor, chainrpc.UseLogger)
|
|
|
|
AddSubLogger(root, "IRPC", interceptor, invoicesrpc.UseLogger)
|
|
|
|
AddSubLogger(root, "CHNF", interceptor, channelnotifier.UseLogger)
|
|
|
|
AddSubLogger(root, "CHBU", interceptor, chanbackup.UseLogger)
|
|
|
|
AddSubLogger(root, "PROM", interceptor, monitoring.UseLogger)
|
|
|
|
AddSubLogger(root, "WTCL", interceptor, wtclient.UseLogger)
|
|
|
|
AddSubLogger(root, "PRNF", interceptor, peernotifier.UseLogger)
|
|
|
|
AddSubLogger(root, "CHFD", interceptor, chanfunding.UseLogger)
|
|
|
|
AddSubLogger(root, "PEER", interceptor, peer.UseLogger)
|
|
|
|
AddSubLogger(root, "CHCL", interceptor, chancloser.UseLogger)
|
|
|
|
|
|
|
|
AddSubLogger(root, routing.Subsystem, interceptor, routing.UseLogger, localchans.UseLogger)
|
|
|
|
AddSubLogger(root, routerrpc.Subsystem, interceptor, routerrpc.UseLogger)
|
|
|
|
AddSubLogger(root, chanfitness.Subsystem, interceptor, chanfitness.UseLogger)
|
|
|
|
AddSubLogger(root, verrpc.Subsystem, interceptor, verrpc.UseLogger)
|
|
|
|
AddSubLogger(root, healthcheck.Subsystem, interceptor, healthcheck.UseLogger)
|
|
|
|
AddSubLogger(root, chainreg.Subsystem, interceptor, chainreg.UseLogger)
|
|
|
|
AddSubLogger(root, chanacceptor.Subsystem, interceptor, chanacceptor.UseLogger)
|
|
|
|
AddSubLogger(root, funding.Subsystem, interceptor, funding.UseLogger)
|
2021-02-08 21:54:38 +01:00
|
|
|
AddSubLogger(root, cluster.Subsystem, interceptor, cluster.UseLogger)
|
2019-03-27 11:44:30 +01:00
|
|
|
}
|
|
|
|
|
2020-05-25 18:34:00 +02:00
|
|
|
// AddSubLogger is a helper method to conveniently create and register the
|
2019-12-11 08:55:10 +01:00
|
|
|
// logger of one or more sub systems.
|
2020-05-25 18:34:00 +02:00
|
|
|
func AddSubLogger(root *build.RotatingLogWriter, subsystem string,
|
2020-08-28 11:18:15 +02:00
|
|
|
interceptor signal.Interceptor, useLoggers ...func(btclog.Logger)) {
|
|
|
|
|
|
|
|
// genSubLogger will return a callback for creating a logger instance,
|
|
|
|
// which we will give to the root logger.
|
|
|
|
genLogger := genSubLogger(root, interceptor)
|
2020-05-25 18:34:00 +02:00
|
|
|
|
2019-12-11 08:55:10 +01:00
|
|
|
// Create and register just a single logger to prevent them from
|
|
|
|
// overwriting each other internally.
|
2020-08-28 11:18:15 +02:00
|
|
|
logger := build.NewSubLogger(subsystem, genLogger)
|
2020-05-25 18:34:00 +02:00
|
|
|
SetSubLogger(root, subsystem, logger, useLoggers...)
|
2017-06-21 17:07:44 +02:00
|
|
|
}
|
|
|
|
|
2020-05-25 18:34:00 +02:00
|
|
|
// SetSubLogger is a helper method to conveniently register the logger of a sub
|
2019-09-23 13:20:14 +02:00
|
|
|
// system.
|
2020-05-25 18:34:00 +02:00
|
|
|
func SetSubLogger(root *build.RotatingLogWriter, subsystem string,
|
|
|
|
logger btclog.Logger, useLoggers ...func(btclog.Logger)) {
|
2019-09-23 13:20:14 +02:00
|
|
|
|
2020-05-25 18:34:00 +02:00
|
|
|
root.RegisterSubLogger(subsystem, logger)
|
2019-12-11 08:55:10 +01:00
|
|
|
for _, useLogger := range useLoggers {
|
2019-09-23 13:20:14 +02:00
|
|
|
useLogger(logger)
|
|
|
|
}
|
2016-03-23 02:43:10 +01:00
|
|
|
}
|
|
|
|
|
2017-07-31 01:01:44 +02:00
|
|
|
// logClosure is used to provide a closure over expensive logging operations so
|
|
|
|
// don't have to be performed when the logging level doesn't warrant it.
|
2016-06-21 20:32:07 +02:00
|
|
|
type logClosure func() string
|
|
|
|
|
|
|
|
// String invokes the underlying function and returns the result.
|
|
|
|
func (c logClosure) String() string {
|
|
|
|
return c()
|
|
|
|
}
|
|
|
|
|
|
|
|
// newLogClosure returns a new closure over a function that returns a string
|
|
|
|
// which itself provides a Stringer interface so that it can be used with the
|
|
|
|
// logging system.
|
|
|
|
func newLogClosure(c func() string) logClosure {
|
|
|
|
return logClosure(c)
|
|
|
|
}
|