2019-01-24 16:28:25 +03:00
|
|
|
package lnd
|
2016-03-23 04:43:10 +03:00
|
|
|
|
|
|
|
import (
|
2019-08-07 03:57:02 +03:00
|
|
|
"context"
|
2017-06-21 18:07:44 +03:00
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/connmgr"
|
2016-03-23 04:43:10 +03:00
|
|
|
"github.com/btcsuite/btclog"
|
2017-05-18 21:48:10 +03:00
|
|
|
"github.com/lightninglabs/neutrino"
|
2019-03-27 13:44:30 +03:00
|
|
|
sphinx "github.com/lightningnetwork/lightning-onion"
|
2017-08-11 07:39:45 +03:00
|
|
|
"github.com/lightningnetwork/lnd/autopilot"
|
2018-09-20 13:28:35 +03:00
|
|
|
"github.com/lightningnetwork/lnd/build"
|
2016-08-04 22:37:50 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2020-11-09 15:07:45 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainreg"
|
2020-11-09 10:34:49 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chanacceptor"
|
2019-02-09 06:39:38 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chanbackup"
|
2019-08-08 20:39:13 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chanfitness"
|
2016-03-23 04:43:10 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2018-10-22 06:36:56 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channelnotifier"
|
2018-01-17 07:25:34 +03:00
|
|
|
"github.com/lightningnetwork/lnd/contractcourt"
|
2017-03-19 21:40:25 +03:00
|
|
|
"github.com/lightningnetwork/lnd/discovery"
|
2020-11-24 17:38:30 +03:00
|
|
|
"github.com/lightningnetwork/lnd/funding"
|
2020-08-24 09:54:38 +03:00
|
|
|
"github.com/lightningnetwork/lnd/healthcheck"
|
2017-05-02 23:04:58 +03:00
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch"
|
2019-01-05 04:34:59 +03:00
|
|
|
"github.com/lightningnetwork/lnd/invoices"
|
2018-12-13 14:26:30 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/autopilotrpc"
|
2018-12-11 05:34:36 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/chainrpc"
|
2018-12-20 13:42:28 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
|
2019-03-27 13:44:30 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
|
2018-10-23 03:42:21 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/signrpc"
|
2020-04-10 03:05:46 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/verrpc"
|
2018-10-25 05:32:37 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
|
2016-03-23 04:43:10 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2020-06-17 03:33:06 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chancloser"
|
2019-11-01 07:32:04 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chanfunding"
|
2019-05-31 10:59:27 +03:00
|
|
|
"github.com/lightningnetwork/lnd/monitoring"
|
2019-01-05 04:34:59 +03:00
|
|
|
"github.com/lightningnetwork/lnd/netann"
|
2020-07-03 00:46:06 +03:00
|
|
|
"github.com/lightningnetwork/lnd/peer"
|
2019-07-29 17:59:48 +03:00
|
|
|
"github.com/lightningnetwork/lnd/peernotifier"
|
2016-12-27 08:42:23 +03:00
|
|
|
"github.com/lightningnetwork/lnd/routing"
|
2019-11-15 13:15:22 +03:00
|
|
|
"github.com/lightningnetwork/lnd/routing/localchans"
|
2018-06-15 06:15:56 +03:00
|
|
|
"github.com/lightningnetwork/lnd/signal"
|
2018-09-26 08:03:16 +03:00
|
|
|
"github.com/lightningnetwork/lnd/sweep"
|
2019-01-12 05:32:40 +03:00
|
|
|
"github.com/lightningnetwork/lnd/watchtower"
|
2019-06-14 03:29:09 +03:00
|
|
|
"github.com/lightningnetwork/lnd/watchtower/wtclient"
|
2019-08-07 03:57:02 +03:00
|
|
|
"google.golang.org/grpc"
|
2016-03-23 04:43:10 +03:00
|
|
|
)
|
|
|
|
|
2020-05-25 19:34:00 +03: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 18:07:44 +03:00
|
|
|
// Loggers can not be used before the log rotator has been initialized with a
|
2020-05-25 19:34:00 +03: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 04:43:10 +03:00
|
|
|
var (
|
2020-05-25 19:34:00 +03: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 13:28:35 +03:00
|
|
|
|
2019-09-23 14:20:14 +03: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 19:34:00 +03: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 04:43:10 +03:00
|
|
|
)
|
|
|
|
|
2020-05-25 19:34:00 +03:00
|
|
|
// SetupLoggers initializes all package-global logger variables.
|
|
|
|
func SetupLoggers(root *build.RotatingLogWriter) {
|
|
|
|
// Now that we have the proper root logger, we can replace the
|
|
|
|
// placeholder lnd package loggers.
|
|
|
|
for _, l := range lndPkgLoggers {
|
|
|
|
l.Logger = build.NewSubLogger(l.subsystem, root.GenSubLogger)
|
|
|
|
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)
|
|
|
|
|
|
|
|
AddSubLogger(root, "LNWL", lnwallet.UseLogger)
|
|
|
|
AddSubLogger(root, "DISC", discovery.UseLogger)
|
|
|
|
AddSubLogger(root, "NTFN", chainntnfs.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)
|
2020-07-03 00:46:06 +03:00
|
|
|
AddSubLogger(root, "PEER", peer.UseLogger)
|
2020-06-17 03:33:06 +03:00
|
|
|
AddSubLogger(root, "CHCL", chancloser.UseLogger)
|
2020-05-25 19:34:00 +03:00
|
|
|
|
|
|
|
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)
|
2020-08-24 09:54:38 +03:00
|
|
|
AddSubLogger(root, healthcheck.Subsystem, healthcheck.UseLogger)
|
2020-11-09 15:07:45 +03:00
|
|
|
AddSubLogger(root, chainreg.Subsystem, chainreg.UseLogger)
|
2020-11-09 10:34:49 +03:00
|
|
|
AddSubLogger(root, chanacceptor.Subsystem, chanacceptor.UseLogger)
|
2020-11-24 17:38:30 +03:00
|
|
|
AddSubLogger(root, funding.Subsystem, funding.UseLogger)
|
2019-03-27 13:44:30 +03:00
|
|
|
}
|
|
|
|
|
2020-05-25 19:34:00 +03:00
|
|
|
// AddSubLogger is a helper method to conveniently create and register the
|
2019-12-11 10:55:10 +03:00
|
|
|
// logger of one or more sub systems.
|
2020-05-25 19:34:00 +03:00
|
|
|
func AddSubLogger(root *build.RotatingLogWriter, subsystem string,
|
|
|
|
useLoggers ...func(btclog.Logger)) {
|
|
|
|
|
2019-12-11 10:55:10 +03:00
|
|
|
// Create and register just a single logger to prevent them from
|
|
|
|
// overwriting each other internally.
|
2020-05-25 19:34:00 +03:00
|
|
|
logger := build.NewSubLogger(subsystem, root.GenSubLogger)
|
|
|
|
SetSubLogger(root, subsystem, logger, useLoggers...)
|
2017-06-21 18:07:44 +03:00
|
|
|
}
|
|
|
|
|
2020-05-25 19:34:00 +03:00
|
|
|
// SetSubLogger is a helper method to conveniently register the logger of a sub
|
2019-09-23 14:20:14 +03:00
|
|
|
// system.
|
2020-05-25 19:34:00 +03:00
|
|
|
func SetSubLogger(root *build.RotatingLogWriter, subsystem string,
|
|
|
|
logger btclog.Logger, useLoggers ...func(btclog.Logger)) {
|
2019-09-23 14:20:14 +03:00
|
|
|
|
2020-05-25 19:34:00 +03:00
|
|
|
root.RegisterSubLogger(subsystem, logger)
|
2019-12-11 10:55:10 +03:00
|
|
|
for _, useLogger := range useLoggers {
|
2019-09-23 14:20:14 +03:00
|
|
|
useLogger(logger)
|
|
|
|
}
|
2016-03-23 04:43:10 +03:00
|
|
|
}
|
|
|
|
|
2017-07-31 02:01:44 +03: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 21:32:07 +03: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)
|
|
|
|
}
|
2019-08-07 03:57:02 +03:00
|
|
|
|
|
|
|
// errorLogUnaryServerInterceptor is a simple UnaryServerInterceptor that will
|
|
|
|
// automatically log any errors that occur when serving a client's unary
|
|
|
|
// request.
|
|
|
|
func errorLogUnaryServerInterceptor(logger btclog.Logger) grpc.UnaryServerInterceptor {
|
|
|
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
|
|
|
|
handler grpc.UnaryHandler) (interface{}, error) {
|
|
|
|
|
|
|
|
resp, err := handler(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(roasbeef): also log request details?
|
|
|
|
logger.Errorf("[%v]: %v", info.FullMethod, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// errorLogStreamServerInterceptor is a simple StreamServerInterceptor that
|
|
|
|
// will log any errors that occur while processing a client or server streaming
|
|
|
|
// RPC.
|
|
|
|
func errorLogStreamServerInterceptor(logger btclog.Logger) grpc.StreamServerInterceptor {
|
|
|
|
return func(srv interface{}, ss grpc.ServerStream,
|
|
|
|
info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
|
|
|
|
|
|
|
|
err := handler(srv, ss)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("[%v]: %v", info.FullMethod, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|