-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
logger.go
86 lines (71 loc) · 1.55 KB
/
logger.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package irt
import (
"fmt"
"time"
)
type LogLevel int
const (
LogTrace LogLevel = iota
LogDebug
LogInfo
LogWarning
LogError
)
const (
timestampLayout = "2006-01-02T15:04:05.000000-07:00"
)
func levelToString(level LogLevel) string {
switch level {
case LogTrace:
return "TRACE"
case LogDebug:
return "DEBUG"
case LogInfo:
return "INFO"
case LogWarning:
return "WARNING"
case LogError:
return "ERROR"
default:
return "UNKNOWN_LEVEL"
}
}
type Logger interface {
Logf(level LogLevel, format string, params ...interface{})
}
type consoleLogger struct {
level LogLevel
}
func NewConsoleLogger(level LogLevel) Logger {
res := &consoleLogger{}
res.SetLevel(level)
return res
}
func (l *consoleLogger) SetLevel(level LogLevel) {
l.level = level
}
func (l *consoleLogger) GetLevel() LogLevel {
return l.level
}
func (l *consoleLogger) Logf(level LogLevel, format string, params ...interface{}) {
if l.level > level {
return
}
now := time.Now()
if level == LogError {
fmt.Println("******************** ERROR ********************")
fmt.Println("")
fmt.Printf("[%s] %s: %s\n", now.Format(timestampLayout), levelToString(level), fmt.Sprintf(format, params...))
fmt.Println("\n***********************************************")
} else {
fmt.Printf("[%s] %s: %s\n", now.Format(timestampLayout), levelToString(level), fmt.Sprintf(format, params...))
}
}
type dummyLogger struct {
}
func NewDummyLogger() Logger {
res := &dummyLogger{}
return res
}
func (l *dummyLogger) Logf(level LogLevel, format string, params ...interface{}) {
}