-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
157 lines (122 loc) · 2.9 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
package gtool
import (
"fmt"
"os"
)
var logDirName = "gtool" //log dir name
var logFileTimeOutDay = 5 //log file will keep 5 days
// set log dir name
func SetLogDirName(name string) {
logDirName = name
}
// get log dir name
func LogDirName() string {
return logDirName
}
// set log file time out day
func SetLogTimeOutDay(day int) {
logFileTimeOutDay = day
}
// get log file time out day
func LogTimeOutDay() int {
return logFileTimeOutDay
}
//log level assert
func LogA(title string, value interface{}) {
fileDir := logPathSon("assert")
logWriteFile("Assert", fileDir, title, value)
}
//log level info
func LogI(title string, value interface{}) {
fileDir := logPathSon("info")
logWriteFile("Info", fileDir, title, value)
}
//log level debug
func LogD(title string, value interface{}) {
fileDir := logPathSon("debug")
logWriteFile("Debug", fileDir, title, value)
}
//log level warn
func LogW(title string, value interface{}) {
fileDir := logPathSon("warn")
logWriteFile("Warn", fileDir, title, value)
}
//log level error
func LogE(title string, value interface{}) {
fileDir := logPathSon("error")
logWriteFile("Error", fileDir, title, value)
}
//log
func Log(format LogFormat) {
fmt.Println(logFormatContent(format))
}
//log sql
func LogSql() {
}
func logBasePath() {
}
//log content format
type LogFormat struct {
Type string //type key
TypeVal interface{} //type val
Title string //log title
}
//log format content
func logFormatContent(format LogFormat) (res string) {
if format.TypeVal != nil {
res = fmt.Sprintf("[%s-%s] %s | %s \n %v", LogDirName(), format.Type, TimeNow(), format.Title, format.TypeVal)
} else {
res = fmt.Sprintf("[%s-%s] %s | %s", LogDirName(), format.Type, TimeNow(), format.Title)
}
return
}
//write log to file
func logWriteFile(fileName, fileDir, title string, value interface{}) {
//create dir
FileDirAutoCreate(fileDir)
//delete time out file
logDelTimeOutFile(fileDir)
path := fileDir + logFileName("")
//format content struct
formatContent := LogFormat{
Type: fileName,
TypeVal: value,
Title: title,
}
content := logFormatContent(formatContent)
status := FileWrite(path, content)
if !status {
Log(formatContent)
}
}
//time out date
var timeOutDate string
//delete time out log file
func logDelTimeOutFile(fileDir string) {
toDate := DateYmdDay(-LogTimeOutDay())
if timeOutDate == toDate {
return
}
fileName := logFileName("")
pathName := fileDir + fileName
if FileDirIsExist(pathName) { //if file exist and remove it
err := os.Remove(pathName)
if err == nil {
timeOutDate = toDate //set time out date
}
}
}
//log file name
func logFileName(name string) string {
if name == "" {
name = DateToday()
}
return fmt.Sprintf("%s.log", name)
}
//log base path
func logPath() string {
return fmt.Sprintf("%s/%s/", PathCurrent(), LogDirName())
}
func logPathSon(name string) string {
return logPath() + name + "/"
}