-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlogger.go
72 lines (57 loc) · 1.2 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
package httpway
import "fmt"
//logger interface
type Logger interface {
Info(v ...interface{})
Warning(v ...interface{})
Error(v ...interface{})
Debug(v ...interface{})
}
const default_depth = 5
type internalLogger struct {
l Logger
id uint64
prefix string
}
func (il *internalLogger) Info(v ...interface{}) {
if il.l == nil {
return
}
il.log(il.l.Info, v...)
}
func (il *internalLogger) Warning(v ...interface{}) {
if il.l == nil {
return
}
il.log(il.l.Warning, v...)
}
func (il *internalLogger) Error(v ...interface{}) {
if il.l == nil {
return
}
il.log(il.l.Error, v...)
}
func (il *internalLogger) Debug(v ...interface{}) {
if il.l == nil {
return
}
il.log(il.l.Debug, v...)
}
func (il *internalLogger) log(f func(v ...interface{}), v ...interface{}) {
if len(v) > 1 {
v[0] = fmt.Sprintf("[%x] %s", il.id, v[0].(string))
} else {
v[0] = fmt.Sprintf("[%x] %v", il.id, v[0])
}
if il.prefix != "" {
v[0] = fmt.Sprintf("[%s] %s", il.prefix, v[0].(string))
}
f(v...)
}
type internalServerLoggerWriter struct {
l Logger
}
func (islw *internalServerLoggerWriter) Write(b []byte) (n int, err error) {
islw.l.Error("Go Server: %s", string(b))
return len(b), nil
}