forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 23
/
log.go
226 lines (203 loc) · 9.38 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package dcrlnd
import (
"github.com/decred/dcrd/connmgr"
"github.com/decred/dcrlnd/automation"
"github.com/decred/dcrlnd/autopilot"
"github.com/decred/dcrlnd/build"
"github.com/decred/dcrlnd/chainntnfs"
"github.com/decred/dcrlnd/chainreg"
"github.com/decred/dcrlnd/chainscan"
"github.com/decred/dcrlnd/chainscan/csdrivers"
"github.com/decred/dcrlnd/chanacceptor"
"github.com/decred/dcrlnd/chanbackup"
"github.com/decred/dcrlnd/chanfitness"
"github.com/decred/dcrlnd/channeldb"
"github.com/decred/dcrlnd/channelnotifier"
"github.com/decred/dcrlnd/cluster"
"github.com/decred/dcrlnd/contractcourt"
"github.com/decred/dcrlnd/discovery"
"github.com/decred/dcrlnd/funding"
"github.com/decred/dcrlnd/healthcheck"
"github.com/decred/dcrlnd/htlcswitch"
"github.com/decred/dcrlnd/invoices"
"github.com/decred/dcrlnd/keychain"
"github.com/decred/dcrlnd/kvdb/postgres"
"github.com/decred/dcrlnd/lnrpc/autopilotrpc"
"github.com/decred/dcrlnd/lnrpc/chainrpc"
"github.com/decred/dcrlnd/lnrpc/invoicesrpc"
"github.com/decred/dcrlnd/lnrpc/routerrpc"
"github.com/decred/dcrlnd/lnrpc/signrpc"
"github.com/decred/dcrlnd/lnrpc/verrpc"
"github.com/decred/dcrlnd/lnrpc/walletrpc"
"github.com/decred/dcrlnd/lnwallet"
"github.com/decred/dcrlnd/lnwallet/chancloser"
"github.com/decred/dcrlnd/lnwallet/chanfunding"
"github.com/decred/dcrlnd/lnwallet/dcrwallet"
"github.com/decred/dcrlnd/lnwallet/remotedcrwallet"
"github.com/decred/dcrlnd/monitoring"
"github.com/decred/dcrlnd/netann"
"github.com/decred/dcrlnd/peer"
"github.com/decred/dcrlnd/peernotifier"
"github.com/decred/dcrlnd/routing"
"github.com/decred/dcrlnd/routing/localchans"
"github.com/decred/dcrlnd/rpcperms"
"github.com/decred/dcrlnd/signal"
"github.com/decred/dcrlnd/sweep"
"github.com/decred/dcrlnd/tor"
"github.com/decred/dcrlnd/walletunlocker"
"github.com/decred/dcrlnd/watchtower"
"github.com/decred/dcrlnd/watchtower/wtclient"
sphinx "github.com/decred/lightning-onion/v4"
"github.com/decred/slog"
)
// 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 {
slog.Logger
subsystem string
}
// Loggers can not be used before the log rotator has been initialized with a
// log file. This must be performed early during application startup by
// calling InitLogRotator() on the main log writer instance in the config.
var (
// 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
}
// 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
// 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")
atplLog = addLndPkgLogger("ATPL")
)
// 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) slog.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) slog.Logger {
return root.GenSubLogger(tag, shutdown)
}
}
// SetupLoggers initializes all package-global logger variables.
func SetupLoggers(root *build.RotatingLogWriter, interceptor signal.Interceptor) {
genLogger := genSubLogger(root, interceptor)
// 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, genLogger)
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)
// Decred-specific logs.
AddSubLogger(root, "DCRW", interceptor, dcrwallet.UseLogger)
AddSubLogger(root, "RDCW", interceptor, remotedcrwallet.UseLogger)
AddSubLogger(root, "KCHN", interceptor, keychain.UseLogger)
AddSubLogger(root, "CSCN", interceptor, chainscan.UseLogger)
AddSubLogger(root, "CSDR", interceptor, csdrivers.UseLogger)
AddSubLogger(root, "AUTO", interceptor, automation.UseLogger)
AddSubLogger(root, "PGDB", interceptor, postgres.UseLogger)
AddSubLogger(root, "LCCH", interceptor, localchans.UseLogger)
AddSubLogger(root, "PING", interceptor, peer.UsePingLogger)
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, "CNCT", interceptor, contractcourt.UseLogger)
AddSubLogger(root, "UTXN", interceptor, contractcourt.UseNurseryLogger)
AddSubLogger(root, "BRAR", interceptor, contractcourt.UseBreachLogger)
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, "WUNL", interceptor, walletunlocker.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)
AddSubLogger(root, cluster.Subsystem, interceptor, cluster.UseLogger)
AddSubLogger(root, rpcperms.Subsystem, interceptor, rpcperms.UseLogger)
AddSubLogger(root, tor.Subsystem, interceptor, tor.UseLogger)
}
// AddSubLogger is a helper method to conveniently create and register the
// logger of one or more sub systems.
func AddSubLogger(root *build.RotatingLogWriter, subsystem string,
interceptor signal.Interceptor, useLoggers ...func(slog.Logger)) {
// genSubLogger will return a callback for creating a logger instance,
// which we will give to the root logger.
genLogger := genSubLogger(root, interceptor)
// Create and register just a single logger to prevent them from
// overwriting each other internally.
logger := build.NewSubLogger(subsystem, genLogger)
SetSubLogger(root, subsystem, logger, useLoggers...)
}
// SetSubLogger is a helper method to conveniently register the logger of a sub
// system.
func SetSubLogger(root *build.RotatingLogWriter, subsystem string,
logger slog.Logger, useLoggers ...func(slog.Logger)) {
root.RegisterSubLogger(subsystem, logger)
for _, useLogger := range useLoggers {
useLogger(logger)
}
}
// 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.
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)
}