-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.go
204 lines (178 loc) · 3.92 KB
/
init.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
202
203
204
package gologger
import (
"fmt"
"log"
"os"
"path/filepath"
"sync"
"time"
)
// const DATE_FORMAT = "20060102"
//var fileLog *FileLogger
var multLog = make(map[string]*FileLogger)
var multLogMut = new(sync.RWMutex)
type LoggerConf struct {
FileDir string `yaml:"fileDir"`
FileName string `yaml:"fileName"`
Prefix string `yaml:"prefix"`
Flag int `yaml:"flag"`
DateFormat string `yaml:"dateFormat"`
Hook *Hook `yaml:"-"`
}
type FileLogger struct {
fileDir string
fileName string
prefix string
logFlag int
date *time.Time
logFile *os.File
logFilepath string
lg *log.Logger
mu *sync.RWMutex
logChan chan string
dateFormat string
hook *Hook
}
func (f *FileLogger) getLogFilePath() string {
f.logFilepath = filepath.Join(f.fileDir, f.fileName) + "." + f.date.Format(f.dateFormat) + ".log"
return f.logFilepath
}
// 日志文件是否必须分割
func (f *FileLogger) isMustSplit() bool {
t, _ := time.Parse(f.dateFormat, time.Now().Format(f.dateFormat))
return t.After(*f.date)
}
// 日志文件是否存在,不存在则创建
func (f *FileLogger) isExistOrCreate() {
_, err := os.Stat(f.fileDir)
if err != nil && !os.IsExist(err) {
os.Mkdir(f.fileDir, 0755)
}
}
func (f *FileLogger) split() (err error) {
f.mu.Lock()
defer f.mu.Unlock()
if f.logFile != nil {
f.logFile.Close()
if f.hook != nil {
f.hook.AfterSplit(f.logFilepath)
}
}
t, _ := time.Parse(f.dateFormat, time.Now().Format(f.dateFormat))
f.date = &t
logFile := f.getLogFilePath()
f.logFile, err = os.OpenFile(logFile, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
return
}
f.lg = log.New(f.logFile, f.prefix, f.logFlag)
return
}
// 日志写入
func (f *FileLogger) logWriter() {
defer func() { recover() }()
for {
str := <-f.logChan
f.mu.RLock()
f.lg.Output(2, str)
f.mu.RUnlock()
}
}
func (f *FileLogger) Println(info string) {
f.logChan <- info
}
func New(conf LoggerConf, names ...string) (err error) {
name := ""
if len(names) > 0 {
name = names[0]
}
multLogMut.Lock()
defer multLogMut.Unlock()
if _, ok := multLog[name]; ok {
err = fmt.Errorf("日志名称:%s已经被使用", name)
return
}
dateFormat := "20060102"
if conf.DateFormat != "" {
dateFormat = conf.DateFormat
}
f := &FileLogger{
fileDir: conf.FileDir,
fileName: conf.FileName,
prefix: conf.Prefix,
logFlag: conf.Flag,
mu: new(sync.RWMutex),
logChan: make(chan string, 1024),
dateFormat: dateFormat,
hook: conf.Hook,
}
t, _ := time.Parse(dateFormat, time.Now().Format(dateFormat))
f.date = &t
f.isExistOrCreate()
logFile := f.getLogFilePath()
f.logFile, err = os.OpenFile(logFile, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
return
}
f.lg = log.New(f.logFile, f.prefix, f.logFlag)
go f.logWriter()
multLog[name] = f
return
}
func StartMonitor() {
defer func() { recover() }()
timer := time.NewTicker(30 * time.Second)
for {
<-timer.C
multLogMut.RLock()
for _, f := range multLog {
if f.lg != nil && f.isMustSplit() {
if err := f.split(); err != nil {
}
}
}
multLogMut.RUnlock()
}
}
func Get(names ...string) *FileLogger {
name := ""
if len(names) > 0 {
name = names[0]
}
multLogMut.RLock()
defer multLogMut.RUnlock()
if f, ok := multLog[name]; ok {
return f
}
return nil
}
// 关闭日志
func Close(names ...string) {
var beCloseNames []string
if len(names) == 0 {
beCloseNames = append(beCloseNames, "")
}
multLogMut.Lock()
defer multLogMut.Unlock()
for _, name := range names {
if f, ok := multLog[name]; ok {
close(f.logChan)
f.lg = nil
f.logFile.Close()
delete(multLog, name)
}
}
}
func Println(info string, names ...string) {
name := ""
if len(names) > 0 {
name = names[0]
}
multLogMut.RLock()
if f, ok := multLog[name]; ok {
f.logChan <- info
} else {
log.Printf("日志名称:%s不存在", name)
}
multLogMut.RUnlock()
}