forked from lightninglabs/pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
189 lines (158 loc) · 5.8 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
// As this file is very similar in every package, ignore the linter here.
// nolint:dupl,interfacer
package pool
import (
"context"
"github.com/btcsuite/btclog"
"github.com/lightninglabs/aperture/l402"
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/pool/account"
"github.com/lightninglabs/pool/auctioneer"
"github.com/lightninglabs/pool/clientdb"
"github.com/lightninglabs/pool/funding"
"github.com/lightninglabs/pool/order"
"github.com/lightninglabs/pool/poolscript"
"github.com/lightningnetwork/lnd"
"github.com/lightningnetwork/lnd/build"
"github.com/lightningnetwork/lnd/signal"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const Subsystem = "POOL"
var (
logWriter = build.NewRotatingLogWriter()
log = build.NewSubLogger(Subsystem, nil)
rpcLog = build.NewSubLogger("RPCS", nil)
sdcrLog = build.NewSubLogger("SDCR", nil)
)
// SetupLoggers initializes all package-global logger variables.
func SetupLoggers(root *build.RotatingLogWriter, intercept signal.Interceptor) {
genLogger := genSubLogger(root, intercept)
logWriter = root
log = build.NewSubLogger(Subsystem, genLogger)
rpcLog = build.NewSubLogger("RPCS", genLogger)
sdcrLog = build.NewSubLogger("SDCR", genLogger)
lnd.SetSubLogger(root, Subsystem, log)
lnd.SetSubLogger(root, "RPCS", rpcLog)
lnd.SetSubLogger(root, "SDCR", sdcrLog)
lnd.AddSubLogger(root, funding.Subsystem, intercept, funding.UseLogger)
lnd.AddSubLogger(
root, auctioneer.Subsystem, intercept, auctioneer.UseLogger,
)
lnd.AddSubLogger(root, order.Subsystem, intercept, order.UseLogger)
lnd.AddSubLogger(root, "LNDC", intercept, lndclient.UseLogger)
lnd.AddSubLogger(root, "SGNL", intercept, signal.UseLogger)
lnd.AddSubLogger(root, account.Subsystem, intercept, account.UseLogger)
lnd.AddSubLogger(root, l402.Subsystem, intercept, l402.UseLogger)
lnd.AddSubLogger(
root, clientdb.Subsystem, intercept, clientdb.UseLogger,
)
lnd.AddSubLogger(
root, poolscript.Subsystem, intercept, poolscript.UseLogger,
)
}
// 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)
}
}
// errorLogUnaryServerInterceptor is a simple UnaryServerInterceptor that will
// automatically log any errors that occur when serving a client's unary
// request.
func errorLogUnaryServerInterceptor(logger btclog.Logger) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (interface{}, error) {
resp, err := handler(ctx, req)
if err != nil {
// TODO(roasbeef): also log request details?
logger.Errorf("[%v]: %v", info.FullMethod, err)
}
return resp, err
}
}
// errorLogUnaryClientInterceptor is a simple UnaryClientInterceptor that will
// automatically log any errors that occur when executing a unary request to
// the server.
func errorLogUnaryClientInterceptor(logger btclog.Logger) grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply interface{},
cc *grpc.ClientConn, invoker grpc.UnaryInvoker,
opts ...grpc.CallOption) error {
err := invoker(ctx, method, req, reply, cc, opts...)
if err != nil {
logger.Errorf("[%v]: %v", method, err)
}
return err
}
}
// errorLogStreamServerInterceptor is a simple StreamServerInterceptor that
// will log any errors that occur while processing a client or server streaming
// RPC.
func errorLogStreamServerInterceptor(logger btclog.Logger) grpc.StreamServerInterceptor {
return func(srv interface{}, ss grpc.ServerStream,
info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
err := handler(srv, ss)
if err != nil {
logger.Errorf("[%v]: %v", info.FullMethod, err)
}
return err
}
}
// errorLoggingClientStream wraps around the default client stream to long any errors
// that happen when we attempt to send or receive messages on the main stream.
type errorLoggingClientStream struct {
grpc.ClientStream
methodName string
logger btclog.Logger
}
// RecvMsg attempts to recv a message, but logs an error if it occurs.
func (e *errorLoggingClientStream) RecvMsg(m interface{}) error {
err := e.ClientStream.RecvMsg(m)
s, ok := status.FromError(err)
isCtxCanceledErr := ok && s.Code() == codes.Canceled
if err != nil && !isCtxCanceledErr {
e.logger.Errorf("[%v]: %v", e.methodName, err)
}
return err
}
// SendMsg attempts to send a message, but logs an error if it occurs.
func (e *errorLoggingClientStream) SendMsg(m interface{}) error {
err := e.ClientStream.SendMsg(m)
if err != nil {
e.logger.Errorf("[%v]: %v", e.methodName, err)
}
return err
}
// errorLogStreamClientInterceptor is a simple StreamClientInterceptor that
// will log any errors that occur while processing the messages for a server's
// streaming RPC.
func errorLogStreamClientInterceptor(
logger btclog.Logger) grpc.StreamClientInterceptor {
return func(ctx context.Context, desc *grpc.StreamDesc,
cc *grpc.ClientConn, method string, streamer grpc.Streamer,
opts ...grpc.CallOption) (grpc.ClientStream, error) {
mainStream, err := streamer(ctx, desc, cc, method, opts...)
if err != nil {
logger.Errorf("[%v]: %v", method, err)
return nil, err
}
return &errorLoggingClientStream{
ClientStream: mainStream,
logger: logger,
methodName: method,
}, nil
}
}