-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.go
67 lines (58 loc) · 1.05 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 spoor
import (
"fmt"
"io"
"strings"
)
const (
DEBUG = Level(1)
INFO = Level(2)
WARN = Level(3)
ERROR = Level(4)
FATAL = Level(5)
)
type AppLogFunc func(lvl Level, f string, args ...interface{})
type Logger interface {
Output(callerSkip int, s string) error
SetOutput(w io.Writer)
}
type Level int
func (l *Level) Get() interface{} { return *l }
func (l *Level) Set(s string) error {
lvl, err := ParseLogLevel(s)
if err != nil {
return err
}
*l = lvl
return nil
}
func (l Level) String() string {
switch l {
case DEBUG:
return "DEBUG"
case INFO:
return "INFO"
case WARN:
return "WARNING"
case ERROR:
return "ERROR"
case FATAL:
return "FATAL"
}
return "invalid"
}
func ParseLogLevel(levelStr string) (Level, error) {
switch strings.ToLower(levelStr) {
case "debug":
return DEBUG, nil
case "info":
return INFO, nil
case "warn":
return WARN, nil
case "error":
return ERROR, nil
case "fatal":
return FATAL, nil
}
return 0, fmt.Errorf("invalid log level '%s' (debug, info, warn, error, fatal)", levelStr)
}