-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathzlog.go
201 lines (173 loc) · 4.42 KB
/
zlog.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package gimg
import (
"bytes"
"fmt"
"io"
"log"
"os"
"runtime"
"sync/atomic"
"time"
)
var (
colors map[string]string
logNo uint64
)
const (
Black = (iota + 30)
Red
Green
Yellow
Blue
Magenta
Cyan
White
)
type Worker struct {
Minion *log.Logger
Color int
LogFile *os.File
}
type Info struct {
Id uint64
Time string
Module string
Level string
Message string
format string
}
type ZLogger struct {
Module string
Worker *Worker
}
func (i *Info) Output() string {
msg := fmt.Sprintf(i.format, i.Id, i.Time, i.Level, i.Message)
return msg
}
func NewWorker(prefix string, flag int, color int, out io.Writer) *Worker {
return &Worker{Minion: log.New(out, prefix, flag), Color: color, LogFile: nil}
}
func NewConsoleWorker(prefix string, flag int, color int) *Worker {
return NewWorker(prefix, flag, color, os.Stdout)
}
func NewFileWorker(prefix string, flag int, color int, logFile *os.File) *Worker {
return &Worker{Minion: log.New(logFile, prefix, flag), Color: color, LogFile: logFile}
}
func (w *Worker) Log(level string, calldepth int, info *Info) error {
if w.Color != 0 {
buf := &bytes.Buffer{}
buf.Write([]byte(colors[level]))
buf.Write([]byte(info.Output()))
buf.Write([]byte("\033[0m"))
return w.Minion.Output(calldepth+1, buf.String())
} else {
return w.Minion.Output(calldepth+1, info.Output())
}
}
func colorString(color int) string {
return fmt.Sprintf("\033[%dm", int(color))
}
func initColors() {
colors = map[string]string{
"CRITICAL": colorString(Magenta),
"ERROR": colorString(Red),
"WARNING": colorString(Yellow),
"NOTICE": colorString(Green),
"DEBUG": colorString(Cyan),
"INFO": colorString(White),
}
}
func NewLogger(module string, color int) (*ZLogger, error) {
initColors()
newWorker := NewConsoleWorker("", 0, color)
return &ZLogger{Module: module, Worker: newWorker}, nil
}
func NewFileLogger(module string, color int, logFile string) (*ZLogger, error) {
fileHandler, err := os.OpenFile(logFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0777)
if err != nil {
return nil, err
} else {
initColors()
newWorker := NewFileWorker("", 0, color, fileHandler)
return &ZLogger{Module: module, Worker: newWorker}, nil
}
}
func NewDailyLogger(module string, color int, logPath string) (*ZLogger, error) {
var logFile string
const layout = "2006-01-02"
now := time.Now()
fileName := now.Format(layout)
if len(logPath) == 0 {
logFile = "./" + fileName + ".log"
} else {
logFile = logPath + "/" + fileName + ".log"
}
fileHandler, err := os.OpenFile(logFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0777)
if err != nil {
return nil, err
} else {
initColors()
newWorker := NewFileWorker("", 0, color, fileHandler)
return &ZLogger{Module: module, Worker: newWorker}, nil
}
}
func (l *ZLogger) Log(lvl string, message string) {
var formatString string = "#%d %s ▶ %.3s %s"
info := &Info{
Id: atomic.AddUint64(&logNo, 1),
Time: time.Now().Format("2006-01-02 15:04:05"),
Module: l.Module,
Level: lvl,
Message: message,
format: formatString,
}
l.Worker.Log(lvl, 2, info)
}
func (l *ZLogger) Fatal(format string, v ...interface{}) {
message := fmt.Sprintf(format, v...)
l.Log("CRITICAL", message)
os.Exit(1)
}
func (l *ZLogger) Panic(format string, v ...interface{}) {
message := fmt.Sprintf(format, v...)
l.Log("CRITICAL", message)
panic(message)
}
func (l *ZLogger) Critical(format string, v ...interface{}) {
message := fmt.Sprintf(format, v...)
l.Log("CRITICAL", message)
}
func (l *ZLogger) Error(format string, v ...interface{}) {
message := fmt.Sprintf(format, v...)
l.Log("ERROR", message)
}
func (l *ZLogger) Warning(format string, v ...interface{}) {
message := fmt.Sprintf(format, v...)
l.Log("WARNING", message)
}
func (l *ZLogger) Notice(format string, v ...interface{}) {
message := fmt.Sprintf(format, v...)
l.Log("NOTICE", message)
}
func (l *ZLogger) Info(format string, v ...interface{}) {
message := fmt.Sprintf(format, v...)
l.Log("INFO", message)
}
func (l *ZLogger) Debug(format string, v ...interface{}) {
message := fmt.Sprintf(format, v...)
l.Log("DEBUG", message)
}
func (l *ZLogger) Strack(format string, v ...interface{}) {
message := fmt.Sprintf(format, v...)
message += "\n"
buf := make([]byte, 1024*1024)
n := runtime.Stack(buf, true)
message += string(buf[:n])
message += "\n"
l.Log("STRACK", message)
}
func (l *ZLogger) Close() {
if l.Worker.LogFile != nil {
l.Worker.LogFile.Close()
}
}