-
Notifications
You must be signed in to change notification settings - Fork 2
/
LogHandle.go
144 lines (126 loc) · 4.02 KB
/
LogHandle.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
package Logger
import (
"fmt"
"log"
"os"
"path"
"sync"
"time"
"github.com/buguang01/util"
"github.com/gookit/color"
)
//LogHandleModel 写日志用的类,里面会自行维护要写到哪个文件里去;
//第一次使用会按时间开一个当前时间的文件
//如果到了第二天,在第二天的第一次写入时,会关闭之前的文件
//重新打开一个新的文件来写
//文件会在这个基础上,再分为不同等级的日志,写在不同的文件中
//一天的日志,都会写在同一日期的文件夹下面
//如果设置了特殊监听的keyid,那会在之前的基础上,再加一个文件
//会把那个文件的名字中日志等级的部分改成这个keyid
type LogHandleModel struct {
LogName string //这个日志频道的名字
LogChan chan *LogMsgModel //写日志的信道
Logger *log.Logger //写日志的系统对象
Logfile *os.File //对应的日志文件
CurrDay time.Time //写入目录的那个日期部分,用来确定是不是要新开个对象
wg sync.WaitGroup //用来确认是不是关了
}
//NewLogHandle 开新的日志
func NewLogHandle(dt time.Time, lv LogLevel, pathstr string) (result *LogHandleModel) {
result = new(LogHandleModel)
result.CurrDay = util.GetDate(dt)
result.LogChan = make(chan *LogMsgModel, 10)
if lv == LogLevelmainlevel {
result.LogName = "main"
} else {
result.LogName = GetLogNameByLogLevel(lv)
}
filename := fmt.Sprintf("%s_%02d.%02d.%02d.log",
GetFileNameByLogLevel(lv),
dt.Hour(),
dt.Minute(),
dt.Second())
result.Logfile, _ = os.OpenFile(path.Join(pathstr, filename), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
// result.Logfile, _ = os.Create(path.Join(pathstr, filename))
result.Logger = log.New(result.Logfile, "", 0)
go result.handle()
return result
}
//NewLogHandleByKeyID 开新的日志 用keyid来开
func NewLogHandleByKeyID(dt time.Time, keyid int, pathstr string) (result *LogHandleModel) {
result = new(LogHandleModel)
result.CurrDay = util.GetDate(dt)
result.LogChan = make(chan *LogMsgModel, 10)
filename := fmt.Sprintf("%d_%02d.%02d.%02d.log",
keyid,
dt.Hour(),
dt.Minute(),
dt.Second())
result.Logfile, _ = os.OpenFile(path.Join(pathstr, filename), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
//os.Create(path.Join(pathstr, filename))
result.Logger = log.New(result.Logfile, "", 0)
go result.handle()
return result
}
//handle 写日志的协程
func (lghd *LogHandleModel) handle() {
lghd.wg.Add(1)
defer lghd.wg.Done()
defer lghd.Logfile.Close()
msgstr := ""
for msg := range lghd.LogChan {
if msg.Stack == "" {
msgstr = fmt.Sprintf("%d/%02d/%02d %02d:%02d:%02d %s %d %s",
msg.CreateTime.Year(),
msg.CreateTime.Month(),
msg.CreateTime.Day(),
msg.CreateTime.Hour(),
msg.CreateTime.Minute(),
msg.CreateTime.Second(),
GetLogNameByLogLevel(msg.LogLv),
msg.KeyID,
msg.Msg)
} else {
msgstr = fmt.Sprintf("%d/%02d/%02d %02d:%02d:%02d %s %d %s\r\n%v",
msg.CreateTime.Year(),
msg.CreateTime.Month(),
msg.CreateTime.Day(),
msg.CreateTime.Hour(),
msg.CreateTime.Minute(),
msg.CreateTime.Second(),
GetLogNameByLogLevel(msg.LogLv),
msg.KeyID,
msg.Msg,
msg.Stack)
}
lghd.Logger.Output(2, msgstr)
if lghd.LogName == "main" {
if msg.LogLv == LogLevelmainlevel {
color.White.Println(msgstr)
} else if msg.LogLv <= LogLeveldebuglevel {
color.Yellow.Println(msgstr)
} else if msg.LogLv <= LogLevelinfolevel {
color.Green.Println(msgstr)
} else if msg.LogLv <= LogLevelstatuslevel {
color.Cyan.Println(msgstr)
} else if msg.LogLv <= LogLevelerrorlevel {
color.Magenta.Println(msgstr)
} else if msg.LogLv <= LogLevelfatallevel {
color.Red.Println(msgstr)
} else {
color.Normal.Println(msgstr)
// fmt.Println(msgstr)
}
}
}
// fmt.Println("close handle")
}
//Close 关闭本日志
func (lghd *LogHandleModel) Close() {
close(lghd.LogChan)
}
//WaitClose 关闭并等待
func (lghd *LogHandleModel) WaitClose() {
close(lghd.LogChan)
lghd.wg.Wait()
}