forked from zllangct/ecs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
120 lines (104 loc) · 2.19 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
package ecs
import (
"fmt"
"log"
"os"
"runtime"
)
type Logger interface {
Debug(v ...interface{})
Info(v ...interface{})
Error(v ...interface{})
Fatal(v ...interface{})
Debugf(fmt string, v ...interface{})
Infof(fmt string, v ...interface{})
Errorf(fmt string, v ...interface{})
Fatalf(fmt string, v ...interface{})
}
var Log Logger = NewStdLog()
type StdLogLevel uint8
const (
StdLogLevelDebug StdLogLevel = iota
StdLogLevelInfo
StdLogLevelError
StdLogLevelFatal
StdLogLevelNoPrint
)
type StdLog struct {
logger *log.Logger
level StdLogLevel
}
func NewStdLog(level ...StdLogLevel) *StdLog {
l := StdLogLevelDebug
if len(level) > 0 {
l = level[0]
}
return &StdLog{
level: l,
logger: log.New(os.Stdout, "", log.Lshortfile),
}
}
func (p StdLog) Debug(v ...interface{}) {
p.logger.Output(2, fmt.Sprintf("[DEBUG][%d] %s", goroutineID(), fmt.Sprint(v...)))
}
func (p StdLog) Debugf(format string, v ...interface{}) {
p.logger.Output(2, fmt.Sprintf("[DEBUG][%d] %s", goroutineID(), fmt.Sprintf(format, v...)))
}
func (p StdLog) Info(v ...interface{}) {
if p.level > StdLogLevelInfo {
return
}
p.logger.Output(2, fmt.Sprint(v...))
}
func (p StdLog) Infof(format string, v ...interface{}) {
if p.level > StdLogLevelInfo {
return
}
p.logger.Output(2, fmt.Sprintf(format, v...))
}
func (p StdLog) Error(v ...interface{}) {
if p.level > StdLogLevelError {
return
}
buf := make([]byte, 1024)
for {
n := runtime.Stack(buf, false)
if n < len(buf) {
buf = buf[:n]
break
}
buf = make([]byte, 2*len(buf))
}
s := fmt.Sprint(append(v, "\n", string(buf))...)
p.logger.Output(2, s)
}
func (p StdLog) Errorf(format string, v ...interface{}) {
if p.level > StdLogLevelError {
return
}
buf := make([]byte, 1024)
for {
n := runtime.Stack(buf, false)
if n < len(buf) {
buf = buf[:n]
break
}
buf = make([]byte, 2*len(buf))
}
s := fmt.Sprint(fmt.Sprintf(format, v...), "\n", string(buf))
p.logger.Output(2, s)
}
func (p StdLog) Fatal(v ...interface{}) {
if p.level > StdLogLevelFatal {
return
}
p.Error(v...)
os.Exit(1)
}
func (p StdLog) Fatalf(format string, v ...interface{}) {
if p.level > StdLogLevelFatal {
return
}
p.Errorf(format, v...)
os.Exit(1)
}