-
Notifications
You must be signed in to change notification settings - Fork 20
/
log.go
180 lines (152 loc) · 3.43 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package log
import (
"bytes"
"flag"
"fmt"
"io"
stdlog "log"
"os"
"path/filepath"
"strings"
"sync"
)
type severity int32
const (
DEBUG severity = iota
INFO
WARNING
ERROR
)
var names = []string{
DEBUG: "DEBUG",
INFO: "INFO",
WARNING: "WARNING",
ERROR: "ERROR",
}
func (s *severity) Get() interface{} {
return *s
}
func (s *severity) Set(value string) error {
threshold := INFO
value = strings.ToUpper(value)
for i, name := range names {
if name == value {
threshold = severity(i)
}
}
*s = threshold
return nil
}
func (s *severity) String() string {
return names[int(*s)]
}
type logger struct {
sync.Mutex
buffer *buffer
// *stdlog.Logger
debug *stdlog.Logger
info *stdlog.Logger
warning *stdlog.Logger
err *stdlog.Logger
severity severity
dir string
}
type buffer struct {
bytes.Buffer
next *buffer
}
func (l *logger) getBuffer() *buffer {
l.Lock()
b := l.buffer
if b != nil {
l.buffer = b.next
}
l.Unlock()
if b == nil {
b = new(buffer)
} else {
b.next = nil
b.Reset()
}
return b
}
func (l *logger) print(args ...interface{}) {
buf := l.getBuffer()
fmt.Fprint(buf, args...)
if buf.Bytes()[buf.Len()-1] != '\n' {
buf.WriteByte('\n')
}
}
func (l *logger) printf(format string, args ...interface{}) {
buf := l.getBuffer()
fmt.Fprintf(buf, format, args...)
}
// the default logger
var log logger
func init() {
flag.StringVar(&log.dir, "log_dir", "", "if empty, write log files in this directory")
flag.Var(&log.severity, "log_level", "logs at and above this level")
format := stdlog.Ldate | stdlog.Ltime | stdlog.Lmicroseconds | stdlog.Lshortfile
log.debug = stdlog.New(os.Stdout, "[DEBUG] ", format)
log.info = stdlog.New(os.Stdout, "[INFO] ", format)
log.warning = stdlog.New(os.Stderr, "[WARNING] ", format)
log.err = stdlog.New(os.Stderr, "[ERROR] ", format)
}
// Setup setup log format and output file
func Setup() {
format := stdlog.Ldate | stdlog.Ltime | stdlog.Lmicroseconds | stdlog.Lshortfile
fname := fmt.Sprintf("%s.%d.log", filepath.Base(os.Args[0]), os.Getpid())
f, err := os.Create(filepath.Join(log.dir, fname))
if err != nil {
stdlog.Fatal(err)
}
log.debug = stdlog.New(f, "[DEBUG] ", format)
log.info = stdlog.New(f, "[INFO] ", format)
multi := io.MultiWriter(f, os.Stderr)
log.warning = stdlog.New(multi, "[WARNING] ", format)
log.err = stdlog.New(multi, "[ERROR] ", format)
}
func Debug(v ...interface{}) {
if log.severity == DEBUG {
log.debug.Output(2, fmt.Sprint(v...))
}
}
func Debugf(format string, v ...interface{}) {
if log.severity == DEBUG {
log.debug.Output(2, fmt.Sprintf(format, v...))
}
}
func Info(v ...interface{}) {
if log.severity <= INFO {
log.info.Output(2, fmt.Sprint(v...))
}
}
func Infof(format string, v ...interface{}) {
if log.severity <= INFO {
log.info.Output(2, fmt.Sprintf(format, v...))
}
}
func Warning(v ...interface{}) {
if log.severity <= WARNING {
log.warning.Output(2, fmt.Sprint(v...))
}
}
func Warningf(format string, v ...interface{}) {
if log.severity <= WARNING {
log.warning.Output(2, fmt.Sprintf(format, v...))
}
}
func Error(v ...interface{}) {
log.err.Output(2, fmt.Sprint(v...))
}
func Errorf(format string, v ...interface{}) {
log.err.Output(2, fmt.Sprintf(format, v...))
}
func Fatal(v ...interface{}) {
log.err.Output(2, fmt.Sprint(v...))
stdlog.Fatal(v...)
}
func Fatalf(format string, v ...interface{}) {
log.err.Output(2, fmt.Sprintf(format, v...))
stdlog.Fatalf(format, v...)
}