-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxlog.go
174 lines (146 loc) · 3.24 KB
/
xlog.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package log
import (
"bytes"
"fmt"
"github.com/fengleng/go-common/core/hack"
log2 "log"
"sync"
"io"
"os"
"path"
"time"
)
const (
defaultSkip = 4
)
var (
defaultSep string
)
type XLog struct {
level Level
skip int
hostname string
service string
logTextChan chan string
outputType OutputType
wc io.WriteCloser
dir string
sep string
fileName string
curHour int
curHourTime string
openLock sync.Mutex
}
func newXLog(cfg *logCfg) *XLog {
if cfg == nil {
cfg = defaultLogCfg
}
hostname, _ := os.Hostname()
x := &XLog{
level: cfg.logLevel,
skip: defaultSkip,
service: cfg.service,
hostname: hostname,
logTextChan: make(chan string, cfg.logChannelSize),
outputType: cfg.logOutputType,
dir: cfg.dir,
fileName: cfg.filePrefix,
sep: defaultSep,
wc: cfg.wc,
}
go x.Flush()
return x
}
func (x *XLog) Flush() {
for {
select {
case logText := <-x.logTextChan:
x.write(logText)
}
}
}
func (x *XLog) write(logText string) {
if x.needReopen() {
x.reopen()
}
_, err := x.wc.Write(hack.Slice(logText))
if err != nil {
log2.Println(err)
return
}
}
func (x *XLog) needReopen() bool {
return x.outputType == OutputTypeFile && x.curHour != time.Now().Hour()
}
func (x *XLog) reopen() {
x.openLock.Lock()
defer x.openLock.Unlock()
if !x.needReopen() {
return
}
x.curHour = time.Now().Hour()
x.curHourTime = time.Now().Format("200601020315")
fName := fmt.Sprintf("%s%s%s%s", x.dir, x.sep, x.fileName, x.curHourTime)
file, err := os.OpenFile(fName, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err != nil {
log2.Println(err)
file, err = os.OpenFile(fName, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err != nil {
log2.Println(err)
panic(err.Error())
}
}
x.wc = file
}
func (x *XLog) Debug(txt string, a ...interface{}) {
if x.level > DEBUG {
return
}
x.formatLogAndWriteChan(formatValue(txt, a...), DEBUG)
}
func (x *XLog) Info(txt string, a ...interface{}) {
if x.level > INFO {
return
}
x.formatLogAndWriteChan(formatValue(txt, a...), INFO)
}
func (x *XLog) Warn(txt string, a ...interface{}) {
if x.level > WARN {
return
}
x.formatLogAndWriteChan(formatValue(txt, a...), WARN)
}
func (x *XLog) Error(txt string, a ...interface{}) {
if x.level > ERROR {
return
}
x.formatLogAndWriteChan(formatValue(txt, a...), ERROR)
}
func (x *XLog) Fatal(txt string, a ...interface{}) {
if x.level > FATAL {
return
}
x.formatLogAndWriteChan(formatValue(txt, a...), FATAL)
}
func (x *XLog) formatLogAndWriteChan(body *string, level Level) {
var buffer bytes.Buffer
buffer.WriteString("[")
buffer.WriteString(time.Now().Format("2006-01-02 15-04-05.0000"))
buffer.WriteString("] ")
buffer.WriteString("[")
buffer.WriteString(level.String())
buffer.WriteString("] ")
buffer.WriteString("[")
buffer.WriteString(x.hostname)
buffer.WriteString("] ")
buffer.WriteString("[")
buffer.WriteString(x.service)
buffer.WriteString("] ")
f, filename, lineno := getRuntimeInfo(x.skip)
buffer.WriteString("[")
buffer.WriteString(fmt.Sprintf("%s:%d%s", path.Base(filename), lineno, f))
buffer.WriteString("] ")
buffer.WriteString(*body)
buffer.WriteString("\n")
x.logTextChan <- colors[level](buffer.String())
}