forked from decred/dcrwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
93 lines (83 loc) · 2.74 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
// Copyright (c) 2013-2017 The btcsuite developers
// Copyright (c) 2015-2018 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"os"
"decred.org/dcrwallet/v4/chain"
"decred.org/dcrwallet/v4/internal/loader"
"decred.org/dcrwallet/v4/internal/loggers"
"decred.org/dcrwallet/v4/internal/rpc/jsonrpc"
"decred.org/dcrwallet/v4/internal/rpc/rpcserver"
"decred.org/dcrwallet/v4/p2p"
"decred.org/dcrwallet/v4/spv"
"decred.org/dcrwallet/v4/ticketbuyer"
"decred.org/dcrwallet/v4/wallet"
"decred.org/dcrwallet/v4/wallet/udb"
"github.com/decred/dcrd/connmgr/v3"
"github.com/decred/dcrd/mixing/mixpool"
"github.com/decred/slog"
)
var log = loggers.MainLog
// Initialize package-global logger variables.
func init() {
loader.UseLogger(loggers.LoaderLog)
wallet.UseLogger(loggers.WalletLog)
udb.UseLogger(loggers.WalletLog)
ticketbuyer.UseLogger(loggers.TkbyLog)
chain.UseLogger(loggers.SyncLog)
spv.UseLogger(loggers.SyncLog)
p2p.UseLogger(loggers.PeerLog)
rpcserver.UseLogger(loggers.GrpcLog)
jsonrpc.UseLogger(loggers.JsonrpcLog)
connmgr.UseLogger(loggers.CmgrLog)
// XXX mixclient.UseLogger(loggers.MixcLog)
mixpool.UseLogger(loggers.MixpLog)
}
// subsystemLoggers maps each subsystem identifier to its associated logger.
var subsystemLoggers = map[string]slog.Logger{
"DCRW": loggers.MainLog,
"LODR": loggers.LoaderLog,
"WLLT": loggers.WalletLog,
"TKBY": loggers.TkbyLog,
"SYNC": loggers.SyncLog,
"PEER": loggers.PeerLog,
"GRPC": loggers.GrpcLog,
"RPCS": loggers.JsonrpcLog,
"CMGR": loggers.CmgrLog,
"MIXC": loggers.MixcLog,
"MIXP": loggers.MixpLog,
"VSPC": loggers.VspcLog,
}
// 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
}
// Defaults to info if the log level is invalid.
level, _ := slog.LevelFromString(logLevel)
logger.SetLevel(level)
}
// setLogLevels sets the log level for all subsystem loggers to the passed
// level. It also dynamically creates the subsystem loggers as needed, so it
// can be used to initialize the logging system.
func setLogLevels(logLevel string) {
// Configure all sub-systems with the new logging level. Dynamically
// create loggers as needed.
for subsystemID := range subsystemLoggers {
setLogLevel(subsystemID, logLevel)
}
}
// fatalf logs a message, flushes the logger, and finally exit the process with
// a non-zero return code.
func fatalf(format string, args ...any) {
log.Errorf(format, args...)
os.Stdout.Sync()
loggers.CloseLogRotator()
os.Exit(1)
}