-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
129 lines (118 loc) · 3.46 KB
/
errors.go
File metadata and controls
129 lines (118 loc) · 3.46 KB
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
// Copyright (C) 2023, Lux Partners Limited All rights reserved.
// See the file LICENSE for licensing terms.
package utils
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"
)
const (
noLimitNumberOfMatches = -1
timestampStart = "["
timestampEnd = "]"
)
var (
filters = []string{
"failing health check",
"DEBUG",
"INFO",
}
// It was initially suggested to also look for all warn messages,
// however this results in too much stuff printed to the screen.
// For documentation reasons though leaving the according regex here
// so it can easily be re-enabled if wished.
// errorRegEx := regexp.MustCompile(`(?im)(^.*error.*|.*warn.*)`)
errorRegEx = regexp.MustCompile(`(?im)(^.*error.*)`)
)
// FindErrorLogs is a utility function, we will NOT do error handling,
// as this is supposed to be called during error handling itself
// we don't want to make it even more complex
func FindErrorLogs(rootDirs ...string) {
alreadyNotified := false
foundErrors := []string{}
for _, rootDir := range rootDirs {
if _, err := os.Stat(rootDir); err != nil {
return
}
_ = filepath.WalkDir(rootDir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
if !strings.HasSuffix(d.Name(), "log") {
return nil
}
content, err := os.ReadFile(path)
if err != nil {
return err
}
occurrences := errorRegEx.FindAllString(string(content), noLimitNumberOfMatches)
thisFileNotified := false
for _, o := range occurrences {
// first apply all filters
skip := false
for _, f := range filters {
if strings.Contains(o, f) {
skip = true
}
}
if skip {
continue
}
// also check if this log has already been found in another log file
if alreadyFound(o, foundErrors) {
continue
}
if !alreadyNotified {
fmt.Println()
fmt.Printf("================================= !!! ================================\n")
fmt.Printf("Found some error strings in the logs, check these for possible causes:\n")
alreadyNotified = true
foundErrors = append(foundErrors, removeTimestamp(o))
}
if !thisFileNotified {
fmt.Printf("----------------------------------------------------------------------\n")
fmt.Printf("-- Found error logs in file at path %s:\n", path)
thisFileNotified = true
fmt.Println()
}
fmt.Printf("%s\n", o)
fmt.Printf("----------------------------------------------------------------------\n")
fmt.Println()
}
}
return nil
})
}
if len(foundErrors) > 0 {
fmt.Printf("================!!! end of errors in logs !!! ========================\n")
}
}
func removeTimestamp(s string) string {
// first let's make sure this string follows the usual node timestamp structure
// the same log in a different file most likely will have a different timestamp
// log has format `[timestamp] log text`
if s[:1] == timestampStart {
split := strings.SplitAfter(s, timestampEnd)
if len(split) >= 2 {
return split[1]
}
}
// otherwise just return the string as-is, don't make more assumptions
return s
}
// if an error has already been found, we should not print it again
func alreadyFound(s string, found []string) bool {
log := removeTimestamp(s)
for _, f := range found {
// this is a pretty strict requirement, but probably justified
if f == log {
return true
}
}
return false
}
// Removed duplicate - defined in common.go