/
log.go
153 lines (125 loc) · 2.57 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
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
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"sync"
"time"
)
type LogLevel int
const (
UnknownLogLevel LogLevel = iota
Debug
Info
Notice
Warn
Error
)
var logLevels = map[string]LogLevel{
"debug": Debug,
"info": Info,
"notice": Notice,
"warn": Warn,
"error": Error,
}
var logLevelLabels = map[LogLevel]string{
Debug: "debug",
Info: "info",
Notice: "notice",
Warn: "warn",
Error: "error",
}
const LogBuffer = 64
func (l LogLevel) String() string {
return logLevelLabels[l]
}
const (
LogOutputStdout = "stdout"
LogOutputStdErr = "stderr"
LogOutputNone = "none"
)
type nullWriter struct{}
func (w *nullWriter) Write(p []byte) (int, error) {
return 0, nil
}
type Logger struct {
level LogLevel
msg chan string
internal *log.Logger
wg *sync.WaitGroup
}
func NewLogger(file, level string) (*Logger, error) {
lv, ok := logLevels[level]
if !ok {
return nil, fmt.Errorf("invalid log level %s", level)
}
var w io.Writer
switch file {
case LogOutputStdout, "":
w = os.Stdout
case LogOutputStdErr:
w = os.Stderr
case LogOutputNone:
w = &nullWriter{}
default:
var err error
if w, err = os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666); err != nil {
return nil, fmt.Errorf("failed to open file %s %w", file, err)
}
}
l := &Logger{
level: lv,
msg: make(chan string, LogBuffer),
internal: log.New(w, "", log.Ldate|log.Lmicroseconds),
wg: new(sync.WaitGroup),
}
go l.run()
l.Info("start logging (level: %s)", level)
return l, nil
}
func (l *Logger) Shutdown() {
l.Wait()
}
func (l *Logger) Wait() {
_, cancelFunc := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFunc()
l.wg.Wait()
return
}
func (l *Logger) run() {
for {
select {
case msg := <-l.msg:
l.write(msg)
}
}
}
func (l *Logger) write(msg string) {
defer l.wg.Done()
l.internal.Println(msg)
}
func (l *Logger) Print(level LogLevel, message string) {
if l.level > level {
return
}
msg := fmt.Sprintf("[%s] %s", logLevelLabels[level], message)
l.wg.Add(1)
l.msg <- msg
}
func (l *Logger) Debug(format string, a ...interface{}) {
l.Print(Debug, fmt.Sprintf(format, a...))
}
func (l *Logger) Info(format string, a ...interface{}) {
l.Print(Info, fmt.Sprintf(format, a...))
}
func (l *Logger) Notice(format string, a ...interface{}) {
l.Print(Notice, fmt.Sprintf(format, a...))
}
func (l *Logger) Warn(format string, a ...interface{}) {
l.Print(Warn, fmt.Sprintf(format, a...))
}
func (l *Logger) Error(format string, a ...interface{}) {
l.Print(Error, fmt.Sprintf(format, a...))
}