-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfile.go
More file actions
150 lines (136 loc) · 3.42 KB
/
file.go
File metadata and controls
150 lines (136 loc) · 3.42 KB
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
// Copyright (c) 2020 HigKer
// Open Source: MIT License
// Author: SDing <[email protected]>
// Date: 2020/4/16 9:38 下午
package logker
// file v1.0.6
// File is the implementation of logger interface.
import (
"fmt"
"os"
"path"
)
const (
// Error File Prefix
errPrefix = "error_"
// File Perfix
suffix = ".log"
// Backup File Suffix
bakSuffix = "_bak.log"
// Backup File Perfix
bakPerfix = "log_"
)
// customize logging type
type logFileType int
const (
// Common plain Logging file type
plain logFileType = iota
// Major error logging file type
major
)
type fileLog struct {
logLevel level
// Whether enable error log file.
wheError bool
// Log save directory
directory string
// Log file name
fileName string
// Log file pointer
file *os.File
// Error file log pointer
errFile *os.File
// Customize of time zone type
tz *timeZone
// Set running Time Zone
timeZone logTimeZone
// File system Power
power os.FileMode
// File Max size
fileMaxSize int64
// MessageMatchingCard
formatting string
asyncTask *AsyncTask
}
// Initialization error file pointer
func (f *fileLog) initErrPtr() *os.File {
savePath := path.Join(f.directory, errPrefix+f.fileName+suffix)
file, e := os.OpenFile(savePath, os.O_CREATE|os.O_APPEND|os.O_RDWR, f.power)
if e != nil {
panic("open file fail :" + savePath)
}
return file
}
// Initialization file pointer
func (f *fileLog) initFilePtr() *os.File {
savePath := path.Join(f.directory, f.fileName+".log")
// fmt.Println(savePath)
file, e := os.OpenFile(savePath, os.O_CREATE|os.O_APPEND|os.O_RDWR, f.power)
// fmt.Printf("3 %T %p \n", file, file)
if e != nil {
panic("open file fail :" + savePath)
}
return file
}
func (f *fileLog) isEnableErr() bool {
return f.wheError
}
// TODO: Whether enable current level
func (f *fileLog) isEnableLevel(lev level) bool {
// debug<info<warn<Error
return f.logLevel <= lev
}
// TODO: Check log size & whether division log file
func (f *fileLog) checkSize() bool {
info, e := f.file.Stat()
if e != nil {
return false
}
return info.Size() >= f.fileMaxSize
}
func (f *fileLog) checkErrSize() bool {
info, e := f.errFile.Stat()
if e != nil {
return false
}
return info.Size() >= f.fileMaxSize
}
func (f *fileLog) Info(value string, args ...interface{}) {
if f.isEnableLevel(INFO) {
f.sendMsg(f.pack(INFO, fmt.Sprintf(value, args...)))
}
}
func (f *fileLog) Debug(value string, args ...interface{}) {
if f.isEnableLevel(DEBUG) {
f.sendMsg(f.pack(DEBUG, fmt.Sprintf(value, args...)))
}
}
func (f *fileLog) Error(value string, args ...interface{}) {
if f.isEnableLevel(ERROR) {
f.sendMsg(f.pack(ERROR, fmt.Sprintf(value, args...)))
}
}
func (f *fileLog) Warning(value string, args ...interface{}) {
if f.isEnableLevel(WARNING) {
f.sendMsg(f.pack(WARNING, fmt.Sprintf(value, args...)))
}
}
// Division logging file.
func (f *fileLog) divisionLogFile(fileType logFileType) {
switch fileType {
case plain:
_ = f.file.Close()
srcPath := path.Join(f.directory, f.fileName+suffix)
newPath := path.Join(f.directory, bakPerfix+f.tz.NowTimeStrLogName()+bakSuffix)
_ = os.Rename(srcPath, newPath)
f.file = f.initFilePtr()
case major:
_ = f.errFile.Close()
srcPath := path.Join(f.directory, errPrefix+f.fileName+suffix)
newPath := path.Join(f.directory, errPrefix+f.tz.NowTimeStrLogName()+bakSuffix)
_ = os.Rename(srcPath, newPath)
f.errFile = f.initErrPtr()
default:
f.Error("division log file fail. Type: %v", fileType)
}
}