-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlogger.go
91 lines (77 loc) · 1.64 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
package gnet
import (
"fmt"
"log"
"os"
"runtime"
)
var (
// 默认使用系统库的log接口
// default logger
logger = NewStdLogger(2)
// default InfoLevel
logLevel = InfoLevel
)
// 日志级别,参考zap
//
// log level
const (
DebugLevel int8 = iota - 1
InfoLevel
WarnLevel
ErrorLevel
)
type Logger interface {
Debug(format string, args ...interface{})
Info(format string, args ...interface{})
Warn(format string, args ...interface{})
Error(format string, args ...interface{})
}
type StdLogger struct {
std *log.Logger
callDepth int
}
func (s *StdLogger) Debug(format string, args ...interface{}) {
if logLevel > DebugLevel {
return
}
s.std.Output(s.callDepth, "[D] "+fmt.Sprintf(format, args...))
}
func (s *StdLogger) Info(format string, args ...interface{}) {
if logLevel > InfoLevel {
return
}
s.std.Output(s.callDepth, "[I] "+fmt.Sprintf(format, args...))
}
func (s *StdLogger) Warn(format string, args ...interface{}) {
if logLevel > WarnLevel {
return
}
s.std.Output(s.callDepth, "[W] "+fmt.Sprintf(format, args...))
}
func (s *StdLogger) Error(format string, args ...interface{}) {
if logLevel > ErrorLevel {
return
}
s.std.Output(s.callDepth, "[E] "+fmt.Sprintf(format, args...))
}
func NewStdLogger(callDepth int) Logger {
return &StdLogger{
std: log.New(os.Stderr, "", log.LstdFlags|log.Llongfile),
callDepth: callDepth,
}
}
func GetLogger() Logger {
return logger
}
func SetLogger(w Logger, level int8) {
logger = w
logLevel = level
}
func SetLogLevel(level int8) {
logLevel = level
}
func LogStack() {
buf := make([]byte, 1<<12)
logger.Error(string(buf[:runtime.Stack(buf, false)]))
}