-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHijack.go
56 lines (49 loc) · 1 KB
/
Hijack.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
package logger
import (
"log"
"strings"
)
type SeverityFn func(s string, fallback Severity) Severity
type Hijack struct {
logger Logger
fallbackSeverity Severity
severityFn SeverityFn
}
func DefaultSeverityFn(s string, fallback Severity) Severity {
if strings.Contains(s, "WARN") {
return WARNING
}
if strings.Contains(s, "ERR") {
return ERROR
}
if strings.Contains(s, "NOTICE") {
return NOTICE
}
if strings.Contains(s, "DEBUG") {
return DEBUG
}
return fallback
}
func NewHijack(l Logger, s Severity) *Hijack {
return &Hijack{
logger: l,
fallbackSeverity: s,
severityFn: DefaultSeverityFn,
}
}
func (h *Hijack) Write(data []byte) (n int, err error) {
parts := strings.Split(string(data), "\n")
for _, s := range parts {
if len(s) <= 20 {
continue
}
s = strings.TrimSpace(s[20:])
sev := h.severityFn(s, h.fallbackSeverity)
h.logger.Log(sev, s)
}
return len(data), nil
}
func (h *Hijack) Do() {
log.SetOutput(h)
log.SetFlags(log.LstdFlags)
}