This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
forked from lightninglabs/lightning-terminal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
183 lines (150 loc) · 5.48 KB
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package terminal
import (
"github.com/btcsuite/btclog"
"github.com/lightninglabs/faraday"
"github.com/lightninglabs/lightning-node-connect/mailbox"
"github.com/lightninglabs/lightning-terminal/accounts"
"github.com/lightninglabs/lightning-terminal/autopilotserver"
"github.com/lightninglabs/lightning-terminal/firewall"
mid "github.com/lightninglabs/lightning-terminal/rpcmiddleware"
"github.com/lightninglabs/lightning-terminal/rules"
"github.com/lightninglabs/lightning-terminal/session"
"github.com/lightninglabs/lightning-terminal/subservers"
"github.com/lightninglabs/loop/loopd"
"github.com/lightninglabs/pool"
tap "github.com/lightninglabs/taproot-assets"
"github.com/lightningnetwork/lnd"
"github.com/lightningnetwork/lnd/build"
"github.com/lightningnetwork/lnd/signal"
"google.golang.org/grpc/grpclog"
)
var (
// 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.
log btclog.Logger
// interceptor is the OS signal interceptor that we need to keep a
// reference to for listening for shutdown signals.
interceptor signal.Interceptor
)
const (
// Subsystem defines the logging code for this subsystem.
Subsystem = "LITD"
// 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.
func SetupLoggers(root *build.RotatingLogWriter, intercept signal.Interceptor) {
genLogger := genSubLogger(root, intercept)
log = build.NewSubLogger(Subsystem, genLogger)
interceptor = intercept
// Add the lightning-terminal root logger.
lnd.AddSubLogger(root, Subsystem, intercept, UseLogger)
lnd.AddSubLogger(root, session.Subsystem, intercept, session.UseLogger)
lnd.AddSubLogger(root, mailbox.Subsystem, intercept, mailbox.UseLogger)
lnd.AddSubLogger(root, mid.Subsystem, intercept, mid.UseLogger)
lnd.AddSubLogger(
root, accounts.Subsystem, intercept, accounts.UseLogger,
)
lnd.AddSubLogger(
root, firewall.Subsystem, intercept, firewall.UseLogger,
)
lnd.AddSubLogger(root, rules.Subsystem, intercept, rules.UseLogger)
lnd.AddSubLogger(
root, autopilotserver.Subsystem, intercept,
autopilotserver.UseLogger,
)
lnd.AddSubLogger(
root, subservers.Subsystem, intercept, subservers.UseLogger,
)
// Add daemon loggers to lnd's root logger.
faraday.SetupLoggers(root, intercept)
loopd.SetupLoggers(root, intercept)
pool.SetupLoggers(root, intercept)
tap.SetupLoggers(root, intercept)
// Setup the gRPC loggers too.
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)
}
}
// NewGrpcLogLogger creates a new grpclog compatible logger and attaches it as
// a sub logger to the passed root logger.
func NewGrpcLogLogger(root *build.RotatingLogWriter,
intercept signal.Interceptor, subsystem string) *GrpcLogLogger {
logger := build.NewSubLogger(subsystem, genSubLogger(root, intercept))
lnd.SetSubLogger(root, subsystem, logger)
return &GrpcLogLogger{
Logger: logger,
}
}
// GrpcLogLogger is a wrapper around a btclog logger to make it compatible with
// the grpclog logger package. By default we downgrade the info level to debug
// to reduce the verbosity of the logger.
type GrpcLogLogger struct {
btclog.Logger
}
func (l GrpcLogLogger) Info(args ...interface{}) {
l.Logger.Debug(args...)
}
func (l GrpcLogLogger) Infoln(args ...interface{}) {
l.Logger.Debug(args...)
}
func (l GrpcLogLogger) Infof(format string, args ...interface{}) {
l.Logger.Debugf(format, args...)
}
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)