2019-01-24 16:28:25 +03:00
|
|
|
package lnd
|
2016-03-23 04:43:10 +03:00
|
|
|
|
|
|
|
import (
|
2017-06-21 18:07:44 +03:00
|
|
|
"fmt"
|
2018-09-26 08:03:16 +03:00
|
|
|
"io"
|
|
|
|
"os"
|
2017-06-21 18:07:44 +03:00
|
|
|
"path/filepath"
|
|
|
|
|
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-06-21 18:07:44 +03:00
|
|
|
"github.com/jrick/logrotate/rotator"
|
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"
|
2019-02-09 06:39:38 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chanbackup"
|
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"
|
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"
|
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"
|
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"
|
2016-12-27 08:42:23 +03:00
|
|
|
"github.com/lightningnetwork/lnd/routing"
|
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"
|
2016-03-23 04:43:10 +03:00
|
|
|
)
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// Loggers per subsystem. A single backend logger is created and all subsystem
|
2017-06-21 18:07:44 +03:00
|
|
|
// loggers created from it will write to the backend. When adding new
|
|
|
|
// subsystems, add the subsystem logger variable here and to the
|
|
|
|
// subsystemLoggers map.
|
|
|
|
//
|
|
|
|
// Loggers can not be used before the log rotator has been initialized with a
|
2017-07-31 02:01:44 +03:00
|
|
|
// log file. This must be performed early during application startup by
|
|
|
|
// calling initLogRotator.
|
2016-03-23 04:43:10 +03:00
|
|
|
var (
|
2018-09-20 13:28:35 +03:00
|
|
|
logWriter = &build.LogWriter{}
|
|
|
|
|
2017-07-31 02:01:44 +03:00
|
|
|
// backendLog is the logging backend used to create all subsystem
|
|
|
|
// loggers. The backend must not be used before the log rotator has
|
|
|
|
// been initialized, or data races and/or nil pointer dereferences will
|
|
|
|
// occur.
|
2018-09-20 13:28:35 +03:00
|
|
|
backendLog = btclog.NewBackend(logWriter)
|
2017-06-21 18:07:44 +03:00
|
|
|
|
|
|
|
// logRotator is one of the logging outputs. It should be closed on
|
|
|
|
// application shutdown.
|
|
|
|
logRotator *rotator.Rotator
|
|
|
|
|
2018-09-20 13:28:35 +03:00
|
|
|
ltndLog = build.NewSubLogger("LTND", backendLog.Logger)
|
|
|
|
lnwlLog = build.NewSubLogger("LNWL", backendLog.Logger)
|
|
|
|
peerLog = build.NewSubLogger("PEER", backendLog.Logger)
|
|
|
|
discLog = build.NewSubLogger("DISC", backendLog.Logger)
|
|
|
|
rpcsLog = build.NewSubLogger("RPCS", backendLog.Logger)
|
|
|
|
srvrLog = build.NewSubLogger("SRVR", backendLog.Logger)
|
|
|
|
ntfnLog = build.NewSubLogger("NTFN", backendLog.Logger)
|
|
|
|
chdbLog = build.NewSubLogger("CHDB", backendLog.Logger)
|
|
|
|
fndgLog = build.NewSubLogger("FNDG", backendLog.Logger)
|
|
|
|
hswcLog = build.NewSubLogger("HSWC", backendLog.Logger)
|
|
|
|
utxnLog = build.NewSubLogger("UTXN", backendLog.Logger)
|
|
|
|
brarLog = build.NewSubLogger("BRAR", backendLog.Logger)
|
|
|
|
cmgrLog = build.NewSubLogger("CMGR", backendLog.Logger)
|
|
|
|
crtrLog = build.NewSubLogger("CRTR", backendLog.Logger)
|
|
|
|
btcnLog = build.NewSubLogger("BTCN", backendLog.Logger)
|
|
|
|
atplLog = build.NewSubLogger("ATPL", backendLog.Logger)
|
|
|
|
cnctLog = build.NewSubLogger("CNCT", backendLog.Logger)
|
|
|
|
sphxLog = build.NewSubLogger("SPHX", backendLog.Logger)
|
2018-09-26 08:03:16 +03:00
|
|
|
swprLog = build.NewSubLogger("SWPR", backendLog.Logger)
|
2018-10-23 03:42:21 +03:00
|
|
|
sgnrLog = build.NewSubLogger("SGNR", backendLog.Logger)
|
2018-10-25 05:32:37 +03:00
|
|
|
wlktLog = build.NewSubLogger("WLKT", backendLog.Logger)
|
2018-12-13 14:26:30 +03:00
|
|
|
arpcLog = build.NewSubLogger("ARPC", backendLog.Logger)
|
2018-12-20 13:57:44 +03:00
|
|
|
invcLog = build.NewSubLogger("INVC", backendLog.Logger)
|
2019-01-05 04:34:59 +03:00
|
|
|
nannLog = build.NewSubLogger("NANN", backendLog.Logger)
|
2019-01-12 05:32:40 +03:00
|
|
|
wtwrLog = build.NewSubLogger("WTWR", backendLog.Logger)
|
2018-12-11 05:34:36 +03:00
|
|
|
ntfrLog = build.NewSubLogger("NTFR", backendLog.Logger)
|
2018-12-20 13:42:28 +03:00
|
|
|
irpcLog = build.NewSubLogger("IRPC", backendLog.Logger)
|
2018-10-22 06:36:56 +03:00
|
|
|
chnfLog = build.NewSubLogger("CHNF", backendLog.Logger)
|
2019-02-09 06:39:38 +03:00
|
|
|
chbuLog = build.NewSubLogger("CHBU", backendLog.Logger)
|
2019-05-31 10:59:27 +03:00
|
|
|
promLog = build.NewSubLogger("PROM", backendLog.Logger)
|
2016-03-23 04:43:10 +03:00
|
|
|
)
|
|
|
|
|
2017-06-21 18:07:44 +03:00
|
|
|
// Initialize package-global logger variables.
|
|
|
|
func init() {
|
|
|
|
lnwallet.UseLogger(lnwlLog)
|
|
|
|
discovery.UseLogger(discLog)
|
|
|
|
chainntnfs.UseLogger(ntfnLog)
|
|
|
|
channeldb.UseLogger(chdbLog)
|
|
|
|
htlcswitch.UseLogger(hswcLog)
|
|
|
|
connmgr.UseLogger(cmgrLog)
|
|
|
|
routing.UseLogger(crtrLog)
|
|
|
|
neutrino.UseLogger(btcnLog)
|
2017-08-11 07:39:45 +03:00
|
|
|
autopilot.UseLogger(atplLog)
|
2018-01-17 07:25:34 +03:00
|
|
|
contractcourt.UseLogger(cnctLog)
|
2018-03-13 22:59:02 +03:00
|
|
|
sphinx.UseLogger(sphxLog)
|
2018-06-15 06:15:56 +03:00
|
|
|
signal.UseLogger(ltndLog)
|
2018-09-26 08:03:16 +03:00
|
|
|
sweep.UseLogger(swprLog)
|
2018-10-23 03:42:21 +03:00
|
|
|
signrpc.UseLogger(sgnrLog)
|
2018-10-25 05:32:37 +03:00
|
|
|
walletrpc.UseLogger(wlktLog)
|
2018-12-13 14:26:30 +03:00
|
|
|
autopilotrpc.UseLogger(arpcLog)
|
2018-12-20 13:57:44 +03:00
|
|
|
invoices.UseLogger(invcLog)
|
2019-01-05 04:34:59 +03:00
|
|
|
netann.UseLogger(nannLog)
|
2019-01-12 05:32:40 +03:00
|
|
|
watchtower.UseLogger(wtwrLog)
|
2018-12-11 05:34:36 +03:00
|
|
|
chainrpc.UseLogger(ntfrLog)
|
2018-12-20 13:42:28 +03:00
|
|
|
invoicesrpc.UseLogger(irpcLog)
|
2018-10-22 06:36:56 +03:00
|
|
|
channelnotifier.UseLogger(chnfLog)
|
2019-02-09 06:39:38 +03:00
|
|
|
chanbackup.UseLogger(chbuLog)
|
2019-05-31 10:59:27 +03:00
|
|
|
monitoring.UseLogger(promLog)
|
2019-03-27 13:44:30 +03:00
|
|
|
|
|
|
|
addSubLogger(routerrpc.Subsystem, routerrpc.UseLogger)
|
|
|
|
}
|
|
|
|
|
|
|
|
// addSubLogger is a helper method to conveniently register the logger of a sub
|
|
|
|
// system.
|
|
|
|
func addSubLogger(subsystem string, useLogger func(btclog.Logger)) {
|
|
|
|
logger := build.NewSubLogger(subsystem, backendLog.Logger)
|
|
|
|
useLogger(logger)
|
|
|
|
subsystemLoggers[subsystem] = logger
|
2017-06-21 18:07:44 +03:00
|
|
|
}
|
|
|
|
|
2016-03-23 04:43:10 +03:00
|
|
|
// subsystemLoggers maps each subsystem identifier to its associated logger.
|
|
|
|
var subsystemLoggers = map[string]btclog.Logger{
|
|
|
|
"LTND": ltndLog,
|
|
|
|
"LNWL": lnwlLog,
|
|
|
|
"PEER": peerLog,
|
2017-03-19 21:40:25 +03:00
|
|
|
"DISC": discLog,
|
2016-03-23 04:43:10 +03:00
|
|
|
"RPCS": rpcsLog,
|
|
|
|
"SRVR": srvrLog,
|
|
|
|
"NTFN": ntfnLog,
|
|
|
|
"CHDB": chdbLog,
|
2016-06-21 21:32:07 +03:00
|
|
|
"FNDG": fndgLog,
|
2016-07-10 02:41:06 +03:00
|
|
|
"HSWC": hswcLog,
|
2016-09-12 22:37:51 +03:00
|
|
|
"UTXN": utxnLog,
|
2016-11-29 06:43:57 +03:00
|
|
|
"BRAR": brarLog,
|
2016-12-15 05:11:31 +03:00
|
|
|
"CMGR": cmgrLog,
|
2016-12-27 08:42:23 +03:00
|
|
|
"CRTR": crtrLog,
|
2017-05-18 21:48:10 +03:00
|
|
|
"BTCN": btcnLog,
|
2017-08-11 07:39:45 +03:00
|
|
|
"ATPL": atplLog,
|
2018-01-17 07:25:34 +03:00
|
|
|
"CNCT": cnctLog,
|
2018-03-13 22:59:02 +03:00
|
|
|
"SPHX": sphxLog,
|
2018-09-26 08:03:16 +03:00
|
|
|
"SWPR": swprLog,
|
2018-10-23 03:42:21 +03:00
|
|
|
"SGNR": sgnrLog,
|
2018-10-25 05:32:37 +03:00
|
|
|
"WLKT": wlktLog,
|
2018-12-13 14:26:30 +03:00
|
|
|
"ARPC": arpcLog,
|
2018-12-20 13:57:44 +03:00
|
|
|
"INVC": invcLog,
|
2019-01-05 04:34:59 +03:00
|
|
|
"NANN": nannLog,
|
2019-01-12 05:32:40 +03:00
|
|
|
"WTWR": wtwrLog,
|
2018-12-11 05:34:36 +03:00
|
|
|
"NTFR": ntfnLog,
|
2018-12-20 13:42:28 +03:00
|
|
|
"IRPC": irpcLog,
|
2018-10-22 06:36:56 +03:00
|
|
|
"CHNF": chnfLog,
|
2019-02-09 06:39:38 +03:00
|
|
|
"CHBU": chbuLog,
|
2019-05-31 10:59:27 +03:00
|
|
|
"PROM": promLog,
|
2016-03-23 04:43:10 +03:00
|
|
|
}
|
|
|
|
|
2017-07-31 02:01:44 +03:00
|
|
|
// initLogRotator initializes the logging rotator to write logs to logFile and
|
2017-06-21 18:07:44 +03:00
|
|
|
// create roll files in the same directory. It must be called before the
|
2017-07-31 02:01:44 +03:00
|
|
|
// package-global log rotator variables are used.
|
2018-04-07 01:11:42 +03:00
|
|
|
func initLogRotator(logFile string, MaxLogFileSize int, MaxLogFiles int) {
|
2017-06-21 18:07:44 +03:00
|
|
|
logDir, _ := filepath.Split(logFile)
|
|
|
|
err := os.MkdirAll(logDir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "failed to create log directory: %v\n", err)
|
|
|
|
os.Exit(1)
|
2016-03-23 04:43:10 +03:00
|
|
|
}
|
2018-04-07 01:11:42 +03:00
|
|
|
r, err := rotator.New(logFile, int64(MaxLogFileSize*1024), false, MaxLogFiles)
|
2016-03-23 04:43:10 +03:00
|
|
|
if err != nil {
|
2017-06-21 18:07:44 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "failed to create file rotator: %v\n", err)
|
2016-03-23 04:43:10 +03:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2017-07-07 00:36:55 +03:00
|
|
|
pr, pw := io.Pipe()
|
|
|
|
go r.Run(pr)
|
2017-06-21 18:07:44 +03:00
|
|
|
|
2018-09-20 13:28:35 +03:00
|
|
|
logWriter.RotatorPipe = pw
|
2017-06-21 18:07:44 +03:00
|
|
|
logRotator = r
|
2016-03-23 04:43:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// setLogLevel sets the logging level for provided subsystem. Invalid
|
|
|
|
// subsystems are ignored. Uninitialized subsystems are dynamically created as
|
|
|
|
// needed.
|
|
|
|
func setLogLevel(subsystemID string, logLevel string) {
|
|
|
|
// Ignore invalid subsystems.
|
|
|
|
logger, ok := subsystemLoggers[subsystemID]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-21 18:07:44 +03:00
|
|
|
// Defaults to info if the log level is invalid.
|
|
|
|
level, _ := btclog.LevelFromString(logLevel)
|
2016-03-23 04:43:10 +03:00
|
|
|
logger.SetLevel(level)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setLogLevels sets the log level for all subsystem loggers to the passed
|
2016-06-21 21:32:07 +03:00
|
|
|
// level. It also dynamically creates the subsystem loggers as needed, so it
|
2016-03-23 04:43:10 +03:00
|
|
|
// can be used to initialize the logging system.
|
|
|
|
func setLogLevels(logLevel string) {
|
2017-06-21 18:07:44 +03:00
|
|
|
// Configure all sub-systems with the new logging level. Dynamically
|
2016-03-23 04:43:10 +03:00
|
|
|
// create loggers as needed.
|
|
|
|
for subsystemID := range subsystemLoggers {
|
|
|
|
setLogLevel(subsystemID, logLevel)
|
|
|
|
}
|
|
|
|
}
|
2016-06-21 21:32:07 +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)
|
|
|
|
}
|