-
Notifications
You must be signed in to change notification settings - Fork 199
/
main.go
341 lines (296 loc) · 8.53 KB
/
main.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package main
import (
"encoding/hex"
"fmt"
"net/url"
"os"
"os/signal"
"path/filepath"
"runtime"
"syscall"
"time"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/marshal"
"github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/common"
"github.com/gorilla/websocket"
"github.com/urfave/cli"
)
const (
defaultLogPath = "logs"
wsLogPath = "/log"
ws = "ws"
wss = "wss"
)
type config struct {
workingDir string
address string
logLevel string
logSave bool
useWss bool
logWithCorrelation bool
logWithLoggerName bool
}
var (
nodeHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}
{{if len .Authors}}
AUTHOR:
{{range .Authors}}{{ . }}{{end}}
{{end}}{{if .Commands}}
GLOBAL OPTIONS:
{{range .VisibleFlags}}{{.}}
{{end}}
VERSION:
{{.Version}}
{{end}}
`
// address defines a flag for setting the address and port on which the node will listen for connections
address = cli.StringFlag{
Name: "address",
Usage: "Address and port number on which the application will try to connect to the elrond-go node",
Value: "127.0.0.1:8080",
Destination: &argsConfig.address,
}
// logLevel defines the logger level
logLevel = cli.StringFlag{
Name: "log-level",
Usage: "This flag specifies the logger `level(s)`. It can contain multiple comma-separated value. For example" +
", if set to *:INFO the logs for all packages will have the INFO level. However, if set to *:INFO,api:DEBUG" +
" the logs for all packages will have the INFO level, excepting the api package which will receive a DEBUG" +
" log level.",
Value: "*:" + logger.LogInfo.String(),
Destination: &argsConfig.logLevel,
}
//logFile is used when the log output needs to be logged in a file
logSaveFile = cli.BoolFlag{
Name: "log-save",
Usage: "Boolean option for enabling log saving. If set, it will automatically save all the logs into a file.",
Destination: &argsConfig.logSave,
}
//useWss is used when the user require connection through wss
useWss = cli.BoolFlag{
Name: "use-wss",
Usage: "Will use wss instead of ws when creating the web socket",
Destination: &argsConfig.useWss,
}
//logWithCorrelation is used to enable log correlation elements
logWithCorrelation = cli.BoolFlag{
Name: "log-correlation",
Usage: "Boolean option for enabling log correlation elements.",
Destination: &argsConfig.logWithCorrelation,
}
//logWithLoggerName is used to enable log correlation elements
logWithLoggerName = cli.BoolFlag{
Name: "log-logger-name",
Usage: "Boolean option for logger name in the logs.",
Destination: &argsConfig.logWithLoggerName,
}
// workingDirectory defines a flag for the path for the working directory.
workingDirectory = cli.StringFlag{
Name: "working-directory",
Usage: "The application will store here the logs in a subfolder.",
Value: "",
Destination: &argsConfig.workingDir,
}
argsConfig = &config{}
log = logger.GetOrCreate("logviewer")
cliApp *cli.App
webSocket *websocket.Conn
fileForLogs *os.File
marshalizer marshal.Marshalizer
retryDuration = time.Second * 10
)
func main() {
initCliFlags()
marshalizer = &marshal.GogoProtoMarshalizer{}
cliApp.Action = func(c *cli.Context) error {
return startLogViewer(c)
}
err := cliApp.Run(os.Args)
if err != nil {
log.Error(err.Error())
os.Exit(1)
}
}
func initCliFlags() {
cliApp = cli.NewApp()
cli.AppHelpTemplate = nodeHelpTemplate
cliApp.Name = "Elrond Logviewer App"
cliApp.Version = fmt.Sprintf("%s/%s/%s-%s", "1.0.0", runtime.Version(), runtime.GOOS, runtime.GOARCH)
cliApp.Usage = "Logviewer application used to communicate with elrond-go node to log the message lines"
cliApp.Flags = []cli.Flag{
address,
logLevel,
logSaveFile,
workingDirectory,
useWss,
logWithCorrelation,
logWithLoggerName,
}
cliApp.Authors = []cli.Author{
{
Name: "The Elrond Team",
Email: "contact@elrond.com",
},
}
}
func startLogViewer(ctx *cli.Context) error {
log.Info("logviewer application started", "version", cliApp.Version)
var err error
logLevels, _, err := logger.ParseLogLevelAndMatchingString(argsConfig.logLevel)
if err != nil {
return err
}
if !ctx.IsSet(workingDirectory.Name) {
argsConfig.workingDir, err = os.Getwd()
if err != nil {
log.LogIfError(err)
argsConfig.workingDir = ""
}
}
if argsConfig.logSave {
err = prepareLogFile()
if err != nil {
return err
}
defer func() {
_ = fileForLogs.Close()
}()
}
profile := &logger.Profile{
LogLevelPatterns: argsConfig.logLevel,
WithCorrelation: argsConfig.logWithCorrelation,
WithLoggerName: argsConfig.logWithLoggerName,
}
customLogProfile := ctx.IsSet(logLevel.Name) || ctx.IsSet(logWithLoggerName.Name) || ctx.IsSet(logWithCorrelation.Name)
if customLogProfile {
err = profile.Apply()
log.LogIfError(err)
}
go func() {
for {
webSocket, err = openWebSocket(argsConfig.address)
if err != nil {
log.Error(fmt.Sprintf("logviewer websocket error, retrying in %v...", retryDuration), "error", err.Error())
time.Sleep(retryDuration)
continue
}
if customLogProfile {
err = sendProfile(webSocket, profile)
} else {
err = sendDefaultProfileIdentifier(webSocket)
}
log.LogIfError(err)
listeningOnWebSocket()
time.Sleep(retryDuration)
}
}()
//set this log's level to the lowest desired log level that matches received logs from elrond-go
lowestLogLevel := getLowestLogLevel(logLevels)
log.SetLevel(lowestLogLevel)
waitForUserToTerminateApp(webSocket)
return nil
}
func getLowestLogLevel(logLevels []logger.LogLevel) logger.LogLevel {
lowest := logLevels[0]
for i := 1; i < len(logLevels); i++ {
if lowest > logLevels[i] {
lowest = logLevels[i]
}
}
return lowest
}
func prepareLogFile() error {
logDirectory := filepath.Join(argsConfig.workingDir, defaultLogPath)
logsFile, err := core.CreateFile(
core.ArgCreateFileArgument{
Prefix: "logviewer",
Directory: logDirectory,
FileExtension: "log",
},
)
if err != nil {
return err
}
return logger.AddLogObserver(logsFile, &logger.PlainFormatter{})
}
func openWebSocket(address string) (*websocket.Conn, error) {
scheme := ws
if argsConfig.useWss {
scheme = wss
}
u := url.URL{
Scheme: scheme,
Host: address,
Path: wsLogPath,
}
conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
return nil, err
}
return conn, nil
}
func sendProfile(conn *websocket.Conn, profile *logger.Profile) error {
profileMessage, err := profile.Marshal()
if err != nil {
return err
}
return conn.WriteMessage(websocket.TextMessage, profileMessage)
}
func sendDefaultProfileIdentifier(conn *websocket.Conn) error {
return conn.WriteMessage(websocket.TextMessage, []byte(common.DefaultLogProfileIdentifier))
}
func listeningOnWebSocket() {
for {
msgType, message, err := webSocket.ReadMessage()
if msgType == websocket.CloseMessage {
return
}
if err == nil {
outputMessage(message)
continue
}
_, isConnectionClosed := err.(*websocket.CloseError)
if !isConnectionClosed {
log.Error("logviewer websocket error, retrying in %v...", "error", err.Error())
} else {
log.Error(fmt.Sprintf("logviewer websocket terminated by the server side, retrying in %v...", retryDuration), "error", err.Error())
}
return
}
}
func waitForUserToTerminateApp(conn *websocket.Conn) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
<-sigs
log.Info("terminating logviewer app at user's signal...")
if conn != nil {
err := conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
log.LogIfError(err)
time.Sleep(time.Second)
}
log.Info("logviewer application stopped")
}
func outputMessage(message []byte) {
logLine := &logger.LogLineWrapper{}
err := marshalizer.Unmarshal(logLine, message)
if err != nil {
log.Debug("can not unmarshal received data", "data", hex.EncodeToString(message))
return
}
recoveredLogLine := &logger.LogLine{
LoggerName: logLine.LoggerName,
Correlation: logLine.Correlation,
Message: logLine.Message,
LogLevel: logger.LogLevel(logLine.LogLevel),
Args: make([]interface{}, len(logLine.Args)),
Timestamp: time.Unix(0, logLine.Timestamp),
}
for i, str := range logLine.Args {
recoveredLogLine.Args[i] = str
}
log.Log(recoveredLogLine)
}