2020-07-21 22:06:15 +02:00
|
|
|
package terminal
|
2020-05-26 16:30:26 +02:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/btcsuite/btclog"
|
|
|
|
|
"github.com/lightninglabs/faraday"
|
2021-05-17 12:47:10 +02:00
|
|
|
"github.com/lightninglabs/loop/loopd"
|
|
|
|
|
"github.com/lightninglabs/pool"
|
2020-05-26 16:30:26 +02:00
|
|
|
"github.com/lightningnetwork/lnd"
|
|
|
|
|
"github.com/lightningnetwork/lnd/build"
|
2020-07-21 10:47:53 +02:00
|
|
|
"github.com/lightningnetwork/lnd/signal"
|
2020-05-26 16:30:26 +02:00
|
|
|
"google.golang.org/grpc/grpclog"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2020-07-23 08:53:31 +02:00
|
|
|
// log is a logger that is initialized with no output filters. This
|
|
|
|
|
// means the package will not perform any logging by default until the
|
|
|
|
|
// caller requests it.
|
2020-05-26 16:30:26 +02:00
|
|
|
log btclog.Logger
|
2021-05-17 12:47:10 +02:00
|
|
|
|
|
|
|
|
// interceptor is the OS signal interceptor that we need to keep a
|
|
|
|
|
// reference to for listening for shutdown signals.
|
|
|
|
|
interceptor signal.Interceptor
|
2020-05-26 16:30:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
// Subsystem defines the logging code for this subsystem.
|
2020-07-21 22:06:15 +02:00
|
|
|
Subsystem = "LITD"
|
2020-05-26 16:30:26 +02:00
|
|
|
|
|
|
|
|
// GrpcLogSubsystem defines the logging code for the gRPC subsystem.
|
|
|
|
|
GrpcLogSubsystem = "GRPC"
|
|
|
|
|
|
|
|
|
|
// levelDiffToGRPCLogger is the difference in numerical log level
|
|
|
|
|
// definitions between the grpclog package and the btclog package.
|
|
|
|
|
levelDiffToGRPCLogger = 2
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// The default amount of logging is none.
|
|
|
|
|
func init() {
|
|
|
|
|
UseLogger(build.NewSubLogger(Subsystem, nil))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UseLogger uses a specified Logger to output package logging info. This
|
|
|
|
|
// should be used in preference to SetLogWriter if the caller is also using
|
|
|
|
|
// btclog.
|
|
|
|
|
func UseLogger(logger btclog.Logger) {
|
|
|
|
|
log = logger
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetupLoggers initializes all package-global logger variables.
|
2021-05-17 12:47:10 +02:00
|
|
|
func SetupLoggers(root *build.RotatingLogWriter, intercept signal.Interceptor) {
|
|
|
|
|
genLogger := genSubLogger(root, intercept)
|
|
|
|
|
|
|
|
|
|
log = build.NewSubLogger(Subsystem, genLogger)
|
|
|
|
|
interceptor = intercept
|
|
|
|
|
|
2020-07-23 08:53:31 +02:00
|
|
|
// Add the lightning-terminal root logger.
|
2021-05-17 12:47:10 +02:00
|
|
|
lnd.AddSubLogger(root, Subsystem, intercept, UseLogger)
|
|
|
|
|
|
|
|
|
|
// Add daemon loggers to lnd's root logger.
|
|
|
|
|
faraday.SetupLoggers(root, intercept)
|
|
|
|
|
loopd.SetupLoggers(root, intercept)
|
|
|
|
|
pool.SetupLoggers(root, intercept)
|
2020-10-08 09:34:41 +02:00
|
|
|
|
|
|
|
|
// Setup the gRPC loggers too.
|
2021-05-17 12:47:10 +02:00
|
|
|
grpclog.SetLoggerV2(NewGrpcLogLogger(root, intercept, GrpcLogSubsystem))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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-26 16:30:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewGrpcLogLogger creates a new grpclog compatible logger and attaches it as
|
|
|
|
|
// a sub logger to the passed root logger.
|
|
|
|
|
func NewGrpcLogLogger(root *build.RotatingLogWriter,
|
2021-05-17 12:47:10 +02:00
|
|
|
intercept signal.Interceptor, subsystem string) *GrpcLogLogger {
|
2020-05-26 16:30:26 +02:00
|
|
|
|
2021-05-17 12:47:10 +02:00
|
|
|
logger := build.NewSubLogger(subsystem, genSubLogger(root, intercept))
|
2020-05-26 16:30:26 +02:00
|
|
|
lnd.SetSubLogger(root, subsystem, logger)
|
|
|
|
|
return &GrpcLogLogger{
|
|
|
|
|
Logger: logger,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GrpcLogLogger is a wrapper around a btclog logger to make it compatible with
|
2020-10-08 09:34:48 +02:00
|
|
|
// the grpclog logger package. By default we downgrade the info level to debug
|
|
|
|
|
// to reduce the verbosity of the logger.
|
2020-05-26 16:30:26 +02:00
|
|
|
type GrpcLogLogger struct {
|
|
|
|
|
btclog.Logger
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-08 09:34:48 +02:00
|
|
|
func (l GrpcLogLogger) Info(args ...interface{}) {
|
|
|
|
|
l.Logger.Debug(args...)
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-26 16:30:26 +02:00
|
|
|
func (l GrpcLogLogger) Infoln(args ...interface{}) {
|
2020-10-08 09:34:48 +02:00
|
|
|
l.Logger.Debug(args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l GrpcLogLogger) Infof(format string, args ...interface{}) {
|
|
|
|
|
l.Logger.Debugf(format, args...)
|
2020-05-26 16:30:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l GrpcLogLogger) Warning(args ...interface{}) {
|
|
|
|
|
l.Logger.Warn(args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l GrpcLogLogger) Warningln(args ...interface{}) {
|
|
|
|
|
l.Logger.Warn(args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l GrpcLogLogger) Warningf(format string, args ...interface{}) {
|
|
|
|
|
l.Logger.Warnf(format, args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l GrpcLogLogger) Errorln(args ...interface{}) {
|
|
|
|
|
l.Logger.Error(args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l GrpcLogLogger) Fatal(args ...interface{}) {
|
|
|
|
|
l.Logger.Critical(args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l GrpcLogLogger) Fatalln(args ...interface{}) {
|
|
|
|
|
l.Logger.Critical(args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l GrpcLogLogger) Fatalf(format string, args ...interface{}) {
|
|
|
|
|
l.Logger.Criticalf(format, args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l GrpcLogLogger) V(level int) bool {
|
|
|
|
|
return level+levelDiffToGRPCLogger >= int(l.Logger.Level())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A compile-time check to make sure our GrpcLogLogger satisfies the
|
|
|
|
|
// grpclog.LoggerV2 interface.
|
|
|
|
|
var _ grpclog.LoggerV2 = (*GrpcLogLogger)(nil)
|