-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.go
82 lines (71 loc) · 1.5 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
package sqsjkr
import (
"fmt"
"log"
"os"
)
// LogLevel type
type LogLevel int
// Log level const
const (
ErrorLevel LogLevel = iota
WarnLevel
InfoLevel
DebugLevel
)
// Logger is sqsjkr logger struct
type Logger struct {
Logger *log.Logger
Level LogLevel
}
// Errorf output error log
func (l Logger) Errorf(format string, args ...interface{}) {
l.output(fmt.Sprint("[error] ", format), args...)
}
// Warnf output warning log
func (l Logger) Warnf(format string, args ...interface{}) {
if l.Level > ErrorLevel {
l.output(fmt.Sprint("[warn] ", format), args...)
}
}
// Infof output information log
func (l Logger) Infof(format string, args ...interface{}) {
if l.Level > WarnLevel {
l.output(fmt.Sprint("[info] ", format), args...)
}
}
// Debugf output for debug
func (l Logger) Debugf(format string, args ...interface{}) {
if l.Level > InfoLevel {
l.output(fmt.Sprint("[debug] ", format), args...)
}
}
func (l Logger) output(str string, args ...interface{}) {
if args != nil {
l.Logger.Output(3, fmt.Sprintf(str, args...))
} else {
l.Logger.Output(3, str)
}
}
// SetLevel set a logger level
func (l *Logger) SetLevel(level string) {
switch level {
case "error":
l.Level = ErrorLevel
case "warn":
l.Level = WarnLevel
case "info":
l.Level = InfoLevel
case "debug":
l.Level = DebugLevel
default:
l.Level = InfoLevel
}
}
// NewLogger returns Logger struct
func NewLogger() Logger {
lgg := log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lshortfile)
return Logger{
Logger: lgg,
}
}