-
Notifications
You must be signed in to change notification settings - Fork 0
/
logga.go
157 lines (133 loc) · 2.89 KB
/
logga.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package logga
import (
"encoding/json"
"fmt"
"io"
"os"
"sync"
"text/template"
"time"
)
type Level int
type Logger interface {
Debugf(string, ...interface{})
Infof(string, ...interface{})
Warningf(string, ...interface{})
Errorf(string, ...interface{})
Fatalf(string, ...interface{})
SetOption(Option)
}
type Option func(Logger) error
type LogRecord struct {
Message string `json:"message"`
Time string `json:"time"`
Level string `json:"level"`
}
type logger struct {
level Level
formatter Formatter
timeFormat string
out io.Writer
mutex sync.Mutex
}
type Formatter interface {
Format(message *LogRecord, out io.Writer)
}
type textFormatter struct {
template *template.Template
}
func newTextFormatter(tmplText string) *textFormatter {
tmpl, _ := template.New("").Parse(tmplText)
return &textFormatter{
template: tmpl,
}
}
func (f textFormatter) Format(message *LogRecord, out io.Writer) {
f.template.Execute(out, message)
}
type JSONFormatter struct {
}
func (j JSONFormatter) Format(message *LogRecord, out io.Writer) {
encoder := json.NewEncoder(out)
encoder.Encode(message)
}
const (
All Level = iota
Debug
Info
Warning
Error
Fatal
Off
)
var levelDescription = map[Level]string{
Debug: "DEBUG",
Warning: "WARNING",
Error: "ERROR",
Fatal: "FATAL",
}
func WithLevel(level Level) Option {
return func(l Logger) error {
l.(*logger).level = level
return nil
}
}
func WithMessageTemplate(tmplText string) Option {
return func(l Logger) error {
l.(*logger).formatter = newTextFormatter(tmplText)
return nil
}
}
func WithFormatter(fmt Formatter) Option {
return func(l Logger) error {
l.(*logger).formatter = fmt
return nil
}
}
func WithOutput(out io.Writer) Option {
return func(l Logger) error {
l.(*logger).out = out
return nil
}
}
func NewLogger(opts ...Option) Logger {
l := &logger{}
l.out = os.Stderr
l.timeFormat = time.RFC3339
l.formatter = newTextFormatter("{{.Level}} - {{.Time}} - {{.Message}}\n")
for _, opt := range opts {
opt(l)
}
return l
}
func (l *logger) SetOption(opt Option) {
l.mutex.Lock()
defer l.mutex.Unlock()
opt(l)
}
func (l logger) Debugf(format string, args ...interface{}) {
l.printf(Debug, format, args...)
}
func (l logger) Infof(format string, args ...interface{}) {
l.printf(Info, format, args...)
}
func (l logger) Warningf(format string, args ...interface{}) {
l.printf(Warning, format, args...)
}
func (l logger) Errorf(format string, args ...interface{}) {
l.printf(Error, format, args...)
}
func (l logger) Fatalf(format string, args ...interface{}) {
l.printf(Fatal, format, args...)
os.Exit(1)
}
func (l logger) printf(level Level, format string, args ...interface{}) {
if level >= l.level {
message := fmt.Sprintf(format, args...)
l.formatter.Format(&LogRecord{
Message: message,
Time: time.Now().Format(l.timeFormat),
Level: levelDescription[level],
}, l.out)
}
}