-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
135 lines (115 loc) · 2.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
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
package bytego
import (
"bytes"
"fmt"
"io"
"log"
"strings"
"sync"
)
const (
LEVEL_DEBUG LogLevel = iota - 1
LEVEL_INFO
LEVEL_WARN
LEVEL_ERROR
)
type LogLevel int8
func (l LogLevel) String() string {
switch l {
case LEVEL_DEBUG:
return "DEBUG"
case LEVEL_INFO:
return "INFO"
case LEVEL_WARN:
return "WARN"
case LEVEL_ERROR:
return "ERROR"
default:
return "INFO"
}
}
func ParseLogLevel(s string) LogLevel {
switch strings.ToUpper(s) {
case "DEBUG":
return LEVEL_DEBUG
case "INFO":
return LEVEL_INFO
case "WARN":
return LEVEL_WARN
case "ERROR":
return LEVEL_ERROR
}
return LEVEL_INFO
}
type Logger interface {
SetLevel(level string)
Debug(v ...interface{})
Info(v ...interface{})
Warn(v ...interface{})
Error(v ...interface{})
Debugf(format string, v ...interface{})
Infof(format string, v ...interface{})
Warnf(format string, v ...interface{})
Errorf(format string, v ...interface{})
}
func NewLogger(w io.Writer, level ...string) Logger {
var logLevel LogLevel = LEVEL_INFO
if len(level) > 0 {
logLevel = ParseLogLevel(level[0])
}
var l Logger = &defaultLogger{
level: logLevel,
log: log.New(w, "", log.LstdFlags),
pool: &sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
},
}
return l
}
type defaultLogger struct {
log *log.Logger
level LogLevel
pool *sync.Pool
}
func (l *defaultLogger) SetLevel(level string) {
l.level = ParseLogLevel(level)
}
func (l *defaultLogger) Debug(v ...interface{}) {
_ = l.write(LEVEL_DEBUG, fmt.Sprint(v...))
}
func (l *defaultLogger) Info(v ...interface{}) {
_ = l.write(LEVEL_INFO, fmt.Sprint(v...))
}
func (l *defaultLogger) Warn(v ...interface{}) {
_ = l.write(LEVEL_WARN, fmt.Sprint(v...))
}
func (l *defaultLogger) Error(v ...interface{}) {
_ = l.write(LEVEL_ERROR, fmt.Sprint(v...))
}
func (l *defaultLogger) Debugf(format string, v ...interface{}) {
_ = l.write(LEVEL_DEBUG, format, v...)
}
func (l *defaultLogger) Infof(format string, v ...interface{}) {
_ = l.write(LEVEL_INFO, format, v...)
}
func (l *defaultLogger) Warnf(format string, v ...interface{}) {
_ = l.write(LEVEL_WARN, format, v...)
}
func (l *defaultLogger) Errorf(format string, v ...interface{}) {
_ = l.write(LEVEL_ERROR, format, v...)
}
func (l *defaultLogger) write(level LogLevel, format string, v ...interface{}) error {
if level < l.level {
return nil
}
buf := l.pool.Get().(*bytes.Buffer)
buf.WriteString(l.level.String())
buf.WriteString(" ")
_, _ = fmt.Fprintf(buf, format, v...)
_ = l.log.Output(4, buf.String())
buf.Reset()
l.pool.Put(buf)
return nil
}