-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
138 lines (119 loc) · 3.58 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
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
package yagll
import (
"fmt"
"os"
"strings"
"text/template"
"time"
)
type Logger struct {
colors map[Level]string
files map[Level]*os.File
toggle map[Level]bool
template string
timeFormat string
}
func (l *Logger) SetColor(level Level, color string) {
l.colors[level] = color
}
func (l *Logger) SetDefaultColors() {
l.colors[DEBUG] = Yellow
l.colors[INFO] = Blue
l.colors[ERROR] = Red
}
func (l *Logger) SetOutput(level Level, file *os.File) {
l.files[level] = file
}
func (l *Logger) SetDefaultOutput() {
l.files[DEBUG] = os.Stdout
l.files[INFO] = os.Stdout
l.files[ERROR] = os.Stderr
}
// Sets the template used by the logger.
// Uses text/template to format the message.
func (l *Logger) SetTemplate(template string) {
l.template = template
}
// Sets the template to the default template used by the logger.
// Uses text/template to format the message.
//
// Default template: {{ .Time }} [{{ .Color }}{{ .Level }}{{ .Reset }}] {{.Message}}
func (l *Logger) SetDefaultTemplate() {
l.template = `{{ .Time }} [{{ .Color }}{{ .Level }}{{ .Reset }}] {{.Message}}`
}
// Sets the time format used by the logger (time.Now().Format(format)).
func (l *Logger) SetTimeFormat(format string) {
l.timeFormat = format
}
// Sets the time format to the default format used by the logger.
func (l *Logger) SetDefaultTimeFormat() {
l.timeFormat = "Jan _2 15:04:05 2006"
}
// Print the formatted message to the debug level output.
// The message is formatted using fmt.Sprintf and always ends in a new line.
func (l *Logger) Debugf(format string, a ...interface{}) {
l.output(fmt.Sprintf(format, a...), DEBUG)
}
// Print the formatted message to the info level output.
// The message is formatted using fmt.Sprintf and always ends in a new line.
func (l *Logger) Infof(format string, a ...interface{}) {
l.output(fmt.Sprintf(format, a...), INFO)
}
// Print the formatted message to the error level output.
// The message is formatted using fmt.Sprintf and always ends in a new line.
func (l *Logger) Errorf(format string, a ...interface{}) {
l.output(fmt.Sprintf(format, a...), ERROR)
}
// Print the message to the debug level output.
// The message always ends in a new line.
func (l *Logger) Debugln(message string) {
l.output(message, DEBUG)
}
// Print the message to the info level output.
// The message always ends in a new line.
func (l *Logger) Infoln(message string) {
l.output(message, INFO)
}
// Print the message to the error level output.
// The message always ends in a new line.
func (l *Logger) Errorln(message string) {
l.output(message, ERROR)
}
// Toggle weather or not to print a message to the output.
func (l *Logger) Toggle(level Level, toggle bool) {
l.toggle[level] = toggle
}
func NewLogger() *Logger {
l := &Logger{}
l.colors = make(map[Level]string)
l.files = make(map[Level]*os.File)
l.toggle = map[Level]bool{DEBUG: true, INFO: true, ERROR: true}
l.SetDefaultTemplate()
l.SetDefaultColors()
l.SetDefaultOutput()
l.SetDefaultTimeFormat()
return l
}
func (l *Logger) output(message string, level Level) {
if !l.toggle[level] {
return
}
t := time.Now()
tstring := t.Format(l.timeFormat)
msg := strings.TrimRight(message, "\n")
template, err := template.New("yagll").Parse(l.template)
if err != nil {
fmt.Fprintf(os.Stderr, "YAGLL: Error parsing template: %s", err)
}
err = template.Execute(l.files[level], map[string]string{
"Time": tstring,
"Color": l.colors[level],
"Level": level.String(),
"Reset": Reset,
"Message": msg,
})
l.files[level].Write([]byte("\n"))
if err != nil {
fmt.Fprintf(os.Stderr, "YAGLL: Error executing template: %s", err)
}
}