-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.go
92 lines (72 loc) · 1.77 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
87
88
89
90
91
92
package featurevisor
import (
"fmt"
)
type LogLevel string
const (
Debug LogLevel = "debug"
Info LogLevel = "info"
Warn LogLevel = "warn"
Error LogLevel = "error"
)
type LogMessage string
type LogDetails map[string]interface{}
type LogHandler func(level LogLevel, message LogMessage, details LogDetails)
type CreateLoggerOptions struct {
Levels []LogLevel
Handler LogHandler
}
const loggerPrefix = "[Featurevisor]"
var defaultLogLevels = []LogLevel{
Warn,
Error,
}
var defaultLogHandler = func(level LogLevel, message LogMessage, details LogDetails) {
switch level {
case Debug:
fmt.Println(loggerPrefix, message, details)
case Info:
fmt.Println(loggerPrefix, message, details)
case Warn:
fmt.Println(loggerPrefix, message, details)
case Error:
fmt.Println(loggerPrefix, message, details)
}
}
type Logger struct {
Levels []LogLevel
Handler LogHandler
}
func (l *Logger) SetLevels(levels []LogLevel) {
l.Levels = levels
}
func (l *Logger) Log(level LogLevel, message LogMessage, details LogDetails) {
for _, logLevel := range l.Levels {
if logLevel == level {
l.Handler(level, message, details)
}
}
}
func (l *Logger) Debug(message LogMessage, details LogDetails) {
l.Log(Debug, message, details)
}
func (l *Logger) Info(message LogMessage, details LogDetails) {
l.Log(Info, message, details)
}
func (l *Logger) Warn(message LogMessage, details LogDetails) {
l.Log(Warn, message, details)
}
func (l *Logger) Error(message LogMessage, details LogDetails) {
l.Log(Error, message, details)
}
func CreateLogger(options CreateLoggerOptions) Logger {
levels := options.Levels
if levels == nil {
levels = defaultLogLevels
}
handler := options.Handler
if handler == nil {
handler = defaultLogHandler
}
return Logger{Levels: levels, Handler: handler}
}