-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
121 lines (103 loc) · 2.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
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
package sentinel
import (
"fmt"
"io"
"log"
"os"
)
type LogLevel int
const (
LOG_DEBUG LogLevel = iota
LOG_INFO
LOG_WARNING
LOG_ERR
// LOG_OFF
// LOG_UNKNOWN
)
// default log options
const (
DEFAULT_LOG_PREFIX = "[redis-sentinel]"
DEFAULT_LOG_FLAG = log.Ldate | log.Ltime
DEFAULT_LOG_LEVEL = LOG_DEBUG
)
type SimpleLogger struct {
DEBUG *log.Logger
ERR *log.Logger
INFO *log.Logger
WARN *log.Logger
level LogLevel
}
var logger *SimpleLogger
func init() {
logger = NewSimpleLogger(
os.Stdout,
DEFAULT_LOG_PREFIX,
DEFAULT_LOG_FLAG,
DEFAULT_LOG_LEVEL,
)
logger.Info("sentinal init ")
}
func NewSimpleLogger(out io.Writer, prefix string, flag int, l LogLevel) *SimpleLogger {
return &SimpleLogger{
DEBUG: log.New(out, fmt.Sprintf("%s [debug] ", prefix), flag),
ERR: log.New(out, fmt.Sprintf("%s [error] ", prefix), flag),
INFO: log.New(out, fmt.Sprintf("%s [info] ", prefix), flag),
WARN: log.New(out, fmt.Sprintf("%s [warn] ", prefix), flag),
level: l,
}
}
func (s *SimpleLogger) Error(v ...interface{}) {
if s.level <= LOG_ERR {
s.ERR.Output(2, fmt.Sprint(v...))
}
return
}
func (s *SimpleLogger) Errorf(format string, v ...interface{}) {
if s.level <= LOG_ERR {
s.ERR.Output(2, fmt.Sprintf(format, v...))
}
return
}
func (s *SimpleLogger) Debug(v ...interface{}) {
if s.level <= LOG_DEBUG {
s.DEBUG.Output(2, fmt.Sprint(v...))
}
return
}
func (s *SimpleLogger) Debugf(format string, v ...interface{}) {
if s.level <= LOG_DEBUG {
s.DEBUG.Output(2, fmt.Sprintf(format, v...))
}
return
}
func (s *SimpleLogger) Info(v ...interface{}) {
if s.level <= LOG_INFO {
s.INFO.Output(2, fmt.Sprint(v...))
}
return
}
func (s *SimpleLogger) Infof(format string, v ...interface{}) {
if s.level <= LOG_INFO {
s.INFO.Output(2, fmt.Sprintf(format, v...))
}
return
}
func (s *SimpleLogger) Warn(v ...interface{}) {
if s.level <= LOG_WARNING {
s.WARN.Output(2, fmt.Sprint(v...))
}
return
}
func (s *SimpleLogger) Warnf(format string, v ...interface{}) {
if s.level <= LOG_WARNING {
s.WARN.Output(2, fmt.Sprintf(format, v...))
}
return
}
func (s *SimpleLogger) Level() LogLevel {
return s.level
}
func (s *SimpleLogger) SetLevel(l LogLevel) {
s.level = l
return
}