-
Notifications
You must be signed in to change notification settings - Fork 8
/
writer.go
51 lines (41 loc) · 907 Bytes
/
writer.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
package ui
import (
"regexp"
)
// Writer implements io.Writer
type Writer struct{}
const (
// logPattern to match "[LEVEL] message"
logPattern = "\\[(\\w+)\\] (.*)"
)
var (
// logRegex to check against
logRegex = regexp.MustCompile(logPattern)
)
// Write tries to parse the incoming string. It will try to read the log level
// at start of string as [LEVEL]. If not found it will just use InfoLevel
func (u Writer) Write(p []byte) (n int, err error) {
str := string(p)
matches := logRegex.FindAllStringSubmatch(str, -1)
if len(matches) < 1 {
return 0, nil
}
message := matches[0][2]
strLevel := matches[0][1]
level := ParseLevel(strLevel)
switch level {
case DebugLevel:
Debug(message)
case InfoLevel:
Info(message)
case WarnLevel:
Warn(message)
case ErrorLevel:
Error(message)
case FatalLevel:
Fatal(message)
default:
Info(message)
}
return len(message), nil
}