-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
165 lines (148 loc) · 3.39 KB
/
log.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
package logger
import (
"fmt"
"io"
"log"
"os"
"strings"
//"github.com/fatih/color"
//"syscall"
)
type colorConsoleWriter struct {
prefix string
postfix string
w *os.File
}
func (writer *colorConsoleWriter) Write(p []byte) (n int, err error) {
if len(writer.prefix) > 0 {
fmt.Fprint(writer.w, writer.prefix)
n, err = writer.w.Write(p)
fmt.Fprint(writer.w, writer.postfix)
return
}
return writer.w.Write(p)
}
type logFileWriter struct {
path string
file *os.File
}
func (writer *logFileWriter) close() {
if nil != writer.file {
writer.file.Close()
}
}
func (writer *logFileWriter) Write(p []byte) (n int, err error) {
if nil != writer.file {
_, err := writer.file.Write(p)
if nil != err {
fmt.Printf("Failed to write logfile for reason:%v\n", err)
}
fi, err := writer.file.Stat()
//5MB
if nil == err && fi.Size() >= 1*1024*1024 {
os.Remove(writer.path + ".1")
writer.file.Close()
os.Rename(writer.path, writer.path+".1")
writer.file, _ = os.OpenFile(writer.path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
}
} else {
fmt.Printf("No log file inited for %s \n", writer.path)
}
return len(p), nil
}
func initLogWriter(path string) *logFileWriter {
writer := new(logFileWriter)
writer.path = path
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
//fmt.Printf("file is %s\n", path)
if err != nil {
fmt.Println(err)
} else {
writer.file = file
}
return writer
}
func IsDebugEnable() bool {
return true
}
func InitLogger(output []string) {
withFile = false
ws := make([]io.Writer, 0)
for _, name := range output {
if strings.EqualFold(name, "stdout") {
ws = append(ws, os.Stdout)
withFile = true
} else if strings.EqualFold(name, "console") {
ws = append(ws, os.Stdout)
withFile = true
} else if strings.EqualFold(name, "color") {
//ws = append(ws, os.Stderr)
withColorConsole = true
} else {
ws = append(ws, initLogWriter(name))
withFile = true
}
}
if len(ws) > 0 {
log.SetOutput(io.MultiWriter(ws...))
}
}
var withColorConsole bool
var withFile bool
var colorConsoleLogger *log.Logger
func Debug(format string, v ...interface{}) {
if withFile {
log.Output(2, fmt.Sprintf(format, v...))
}
if withColorConsole {
colorConsoleLogger.Output(2, fmt.Sprintf(format, v...))
}
}
func Notice(format string, v ...interface{}) {
if withFile {
log.Output(2, fmt.Sprintf(format, v...))
}
if withColorConsole {
setNoticeColor()
colorConsoleLogger.Output(2, fmt.Sprintf(format, v...))
unsetNoticeColor()
}
}
func Info(format string, v ...interface{}) {
if withFile {
log.Output(2, fmt.Sprintf(format, v...))
}
if withColorConsole {
setINFOColor()
colorConsoleLogger.Output(2, fmt.Sprintf(format, v...))
unsetINFOColor()
}
}
func Error(format string, v ...interface{}) {
if withFile {
log.Output(2, fmt.Sprintf(format, v...))
}
if withColorConsole {
setErrorColor()
colorConsoleLogger.Output(2, fmt.Sprintf(format, v...))
unsetErrorColor()
}
}
func Fatal(format string, v ...interface{}) {
if withFile {
log.Output(2, fmt.Sprintf(format, v...))
}
if withColorConsole {
setErrorColor()
colorConsoleLogger.Output(2, fmt.Sprintf(format, v...))
unsetErrorColor()
}
os.Exit(1)
}
func init() {
logFlag := log.LstdFlags | log.Lshortfile
log.SetFlags(logFlag)
log.SetOutput(os.Stdout)
withFile = true
colorConsoleLogger = log.New(&colorConsoleWriter{w: os.Stdout}, "", logFlag)
}