-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogger.go
125 lines (101 loc) · 2.54 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
package zanredisdb
import (
"fmt"
"log"
"os"
"sync/atomic"
)
type Logger interface {
Output(depth int, s string)
OutputErr(depth int, s string)
Flush()
}
type LevelLogger struct {
Logger Logger
level int32
}
type SimpleLogger struct {
logger *log.Logger
}
func NewSimpleLogger() *SimpleLogger {
return &SimpleLogger{
logger: log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds),
}
}
func header(lvl, msg string) string {
return fmt.Sprintf("%s: %s", lvl, msg)
}
func (self *SimpleLogger) Output(depth int, s string) {
self.logger.Output(depth+1, s)
}
func (self *SimpleLogger) OutputErr(depth int, s string) {
self.logger.Output(depth+1, header("ERR", s))
}
func (self *SimpleLogger) Flush() {
}
func NewLevelLogger(l int32, logger Logger) *LevelLogger {
return &LevelLogger{Logger: logger, level: l}
}
func (self *LevelLogger) Flush() {
if self.Logger != nil {
self.Logger.Flush()
}
}
func (self *LevelLogger) SetLevel(l int32) {
atomic.StoreInt32(&self.level, l)
}
func (self *LevelLogger) Level() int32 {
return self.level
}
func (self *LevelLogger) Infof(f string, args ...interface{}) {
if self.Logger != nil && self.level > 0 {
self.Logger.Output(2, fmt.Sprintf(f, args...))
}
}
// used only for wrap call (for other logger interface)
func (self *LevelLogger) Printf(f string, args ...interface{}) {
if self.Logger != nil {
self.Logger.Output(3, fmt.Sprintf(f, args...))
}
}
func (self *LevelLogger) Infoln(f string) {
if self.Logger != nil && self.level > 0 {
self.Logger.Output(2, f)
}
}
func (self *LevelLogger) Debugf(f string, args ...interface{}) {
if self.Logger != nil && self.level > 1 {
self.Logger.Output(2, fmt.Sprintf(f, args...))
}
}
func (self *LevelLogger) Detailf(f string, args ...interface{}) {
if self.Logger != nil && self.level > 2 {
self.Logger.Output(2, fmt.Sprintf(f, args...))
}
}
func (self *LevelLogger) Warningf(f string, args ...interface{}) {
if self.Logger != nil {
self.Logger.OutputErr(2, fmt.Sprintf(f, args...))
}
}
func (self *LevelLogger) Warningln(f string) {
if self.Logger != nil {
self.Logger.OutputErr(2, f)
}
}
func (self *LevelLogger) Errorf(f string, args ...interface{}) {
if self.Logger != nil {
self.Logger.OutputErr(2, fmt.Sprintf(f, args...))
}
}
func (self *LevelLogger) Errorln(f string) {
if self.Logger != nil {
self.Logger.OutputErr(2, f)
}
}
var levelLog = NewLevelLogger(2, NewSimpleLogger())
// should call only once before any proxy started.
func SetLogger(level int32, l Logger) {
levelLog.Logger = l
levelLog.SetLevel(level)
}