-
Notifications
You must be signed in to change notification settings - Fork 34
/
log.go
164 lines (150 loc) · 3.33 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
package main
import (
"log"
"os"
"runtime"
"sync"
"time"
)
type LogLevel int
var gLog *logger
const (
LvDEBUG LogLevel = iota
LvINFO
LvWARN
LvERROR
)
var (
logFileNames map[LogLevel]string
loglevel map[LogLevel]string
)
func init() {
logFileNames = make(map[LogLevel]string)
loglevel = make(map[LogLevel]string)
logFileNames[0] = ".log"
loglevel[LvDEBUG] = "DEBUG"
loglevel[LvINFO] = "INFO"
loglevel[LvWARN] = "WARN"
loglevel[LvERROR] = "ERROR"
}
const (
LogFile = iota
LogConsole
LogFileAndConsole
)
type logger struct {
loggers map[LogLevel]*log.Logger
files map[LogLevel]*os.File
level LogLevel
logDir string
mtx *sync.Mutex
lineEnding string
pid int
maxLogSize int64
mode int
}
func NewLogger(path string, filePrefix string, level LogLevel, maxLogSize int64, mode int) *logger {
loggers := make(map[LogLevel]*log.Logger)
logfiles := make(map[LogLevel]*os.File)
var (
logdir string
)
if path == "" {
logdir = "log/"
} else {
logdir = path + "/log/"
}
os.MkdirAll(logdir, 0777)
for lv := range logFileNames {
logFilePath := logdir + filePrefix + logFileNames[lv]
f, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
log.Fatal(err)
}
os.Chmod(logFilePath, 0644)
logfiles[lv] = f
loggers[lv] = log.New(f, "", log.LstdFlags)
}
var le string
if runtime.GOOS == "windows" {
le = "\r\n"
} else {
le = "\n"
}
pLog := &logger{loggers, logfiles, level, logdir, &sync.Mutex{}, le, os.Getpid(), maxLogSize, mode}
go pLog.checkFile()
return pLog
}
func (l *logger) setLevel(level LogLevel) {
l.mtx.Lock()
defer l.mtx.Unlock()
l.level = level
}
func (l *logger) setMode(mode int) {
l.mtx.Lock()
defer l.mtx.Unlock()
l.mode = mode
}
func (l *logger) checkFile() {
if l.maxLogSize <= 0 {
return
}
ticker := time.NewTicker(time.Minute)
for {
select {
case <-ticker.C:
l.mtx.Lock()
for lv, logFile := range l.files {
f, e := logFile.Stat()
if e != nil {
continue
}
if f.Size() <= l.maxLogSize {
continue
}
logFile.Close()
fname := f.Name()
backupPath := l.logDir + fname + ".0"
os.Remove(backupPath)
os.Rename(l.logDir+fname, backupPath)
newFile, e := os.OpenFile(l.logDir+fname, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if e == nil {
l.loggers[lv].SetOutput(newFile)
l.files[lv] = newFile
}
}
l.mtx.Unlock()
}
}
}
func (l *logger) Printf(level LogLevel, format string, params ...interface{}) {
l.mtx.Lock()
defer l.mtx.Unlock()
if level < l.level {
return
}
pidAndLevel := []interface{}{l.pid, loglevel[level]}
params = append(pidAndLevel, params...)
if l.mode == LogFile || l.mode == LogFileAndConsole {
l.loggers[0].Printf("%d %s "+format+l.lineEnding, params...)
}
if l.mode == LogConsole || l.mode == LogFileAndConsole {
log.Printf("%d %s "+format+l.lineEnding, params...)
}
}
func (l *logger) Println(level LogLevel, params ...interface{}) {
l.mtx.Lock()
defer l.mtx.Unlock()
if level < l.level {
return
}
pidAndLevel := []interface{}{l.pid, " ", loglevel[level], " "}
params = append(pidAndLevel, params...)
params = append(params, l.lineEnding)
if l.mode == LogFile || l.mode == LogFileAndConsole {
l.loggers[0].Print(params...)
}
if l.mode == LogConsole || l.mode == LogFileAndConsole {
log.Print(params...)
}
}