forked from OffchainLabs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
67 lines (57 loc) · 2.17 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
package util
import (
"strings"
"time"
"github.com/ethereum/go-ethereum/log"
)
// EphemeralErrorHandler handles errors that are ephemeral in nature i.h these are errors
// that we would like to log as a warning unless they repeat for more than a certain duration of time.
type EphemeralErrorHandler struct {
Duration time.Duration
ErrorString string
FirstOccurrence *time.Time
IgnoreDuration time.Duration
IgnoredErrLogLevel func(string, ...interface{}) // Default IgnoredErrLogLevel is log.Debug
}
func NewEphemeralErrorHandler(duration time.Duration, errorString string, ignoreDuration time.Duration) *EphemeralErrorHandler {
return &EphemeralErrorHandler{
Duration: duration,
ErrorString: errorString,
FirstOccurrence: &time.Time{},
IgnoreDuration: ignoreDuration,
IgnoredErrLogLevel: log.Debug,
}
}
// LogLevel method defaults to returning the input currentLogLevel if the given error doesnt contain the errorSubstring,
// but if it does, then returns one of the corresponding loglevels as follows
// - IgnoredErrLogLevel - if the error has been repeating for less than the IgnoreDuration of time. Defaults to log.Debug
// - log.Warn - if the error has been repeating for less than the given duration of time
// - log.Error - Otherwise
//
// # Usage Examples
//
// ephemeralErrorHandler.Loglevel(err, log.Error)("msg")
// ephemeralErrorHandler.Loglevel(err, log.Error)("msg", "key1", val1, "key2", val2)
// ephemeralErrorHandler.Loglevel(err, log.Error)("msg", "key1", val1)
func (h *EphemeralErrorHandler) LogLevel(err error, currentLogLevel func(msg string, ctx ...interface{})) func(string, ...interface{}) {
if h.ErrorString != "" && !strings.Contains(err.Error(), h.ErrorString) {
h.Reset()
return currentLogLevel
}
if *h.FirstOccurrence == (time.Time{}) {
*h.FirstOccurrence = time.Now()
}
if h.IgnoreDuration != 0 && time.Since(*h.FirstOccurrence) < h.IgnoreDuration {
if h.IgnoredErrLogLevel != nil {
return h.IgnoredErrLogLevel
}
return log.Debug
}
if time.Since(*h.FirstOccurrence) < h.Duration {
return log.Warn
}
return log.Error
}
func (h *EphemeralErrorHandler) Reset() {
*h.FirstOccurrence = time.Time{}
}