This repository has been archived by the owner on Apr 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
195 lines (160 loc) · 3.7 KB
/
logger.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
package log
import (
"fmt"
"os"
"strconv"
"sync"
"time"
"github.com/sirupsen/logrus"
)
const (
debugLog = iota
infoLog
warningLog
errorLog
fatalLog
logSeverity = 5
)
type Logger struct {
logDir string
logFilePrefix string
logFilePath string
logDebug bool
logFileHandle *os.File
logFileMaxCap int64
logMutex sync.Mutex
}
func newLogger(conf map[string]string) (*Logger, error) {
var logDir = "/var/log/huawei"
var maxSize int64 = 1024 * 1024 * 20
var logDebug bool
if "" != conf["logDir"] {
logDir = conf["logDir"]
}
if "" != conf["logFileMaxCap"] {
fileMaxCap, err := getNumInByte(conf["logFileMaxCap"])
if err != nil {
logrus.Errorf("Calc max log file size error: %v.", err)
return nil, err
}
if fileMaxCap > 0 {
maxSize = fileMaxCap
}
}
if "" != conf["logDebug"] {
logDebug, _ = strconv.ParseBool(conf["logDebug"])
}
logger := Logger{
logDir: logDir,
logFileHandle: nil,
logFilePrefix: conf["logFilePrefix"],
logFileMaxCap: maxSize,
logDebug: logDebug,
}
err := logger.initLogFile()
if err != nil {
logrus.Errorf("Obtain log file error: %v.", err)
return nil, err
}
return &logger, nil
}
func (l *Logger) initLogFile() error {
destFilePath := l.logDir
exist, err := isExist(destFilePath)
if err != nil {
return err
}
if !exist {
err := os.MkdirAll(destFilePath, 0755)
if err != nil {
return err
}
}
logFilePath := fmt.Sprintf("%s/%s", destFilePath, l.logFilePrefix)
fileHandle, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
if err != nil {
return err
}
l.logFilePath = logFilePath
l.logFileHandle = fileHandle
return nil
}
func (l *Logger) checkLogFile() {
exist, err := isExist(l.logFilePath)
if err != nil || exist {
return
}
fileHandle, err := os.OpenFile(l.logFilePath, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
if err != nil {
return
}
if l.logFileHandle != nil {
l.logFileHandle.Close()
}
l.logFileHandle = fileHandle
}
func (l *Logger) dumpLog() {
l.checkLogFile()
fileSize, err := getFileByteSize(l.logFileHandle)
if err != nil || fileSize < l.logFileMaxCap {
return
}
timeStr := time.Now().Format("20060102-150405")
newFileName := fmt.Sprintf("%s/%s.%s", l.logDir, l.logFilePrefix, timeStr)
err = copyFile(newFileName, l.logFilePath)
// Only truncate log file in the case of dumping original log file success.
if err == nil {
l.logFileHandle.Truncate(0)
l.logFileHandle.Seek(0, 0)
}
}
func (l *Logger) formatWriteLog(level int, format string, args ...interface{}) {
if level == debugLog && !l.logDebug {
return
}
formatMsg := fmt.Sprintf(format, args...)
l.writeLogMsg(level, formatMsg)
}
func (l *Logger) nonformatWriteLog(level int, args ...interface{}) {
if level == debugLog && !l.logDebug {
return
}
formatMsg := fmt.Sprint(args...)
l.writeLogMsg(level, formatMsg)
}
func (l *Logger) writeLogMsg(level int, logMsg string) {
l.logMutex.Lock()
defer l.logMutex.Unlock()
l.dumpLog()
logLevel := getLogLevel(level)
timeNow := time.Now().Format("2006-01-02 15:04:05.000000")
logContent := fmt.Sprintf("%s %d %s%s\n", timeNow, os.Getpid(), logLevel, logMsg)
err := write(l.logFileHandle, logContent)
if err != nil {
logrus.Errorf("Write log message %s to %s error.", logContent, l.logFilePath)
}
}
func (l *Logger) lockAndFlushAll() {
l.logMutex.Lock()
defer l.logMutex.Unlock()
l.logFileHandle.Sync()
}
func (l *Logger) close() {
l.logFileHandle.Close()
}
func getLogLevel(level int) string {
switch level {
case debugLog:
return "[DEBUG]: "
case infoLog:
return "[INFO]: "
case warningLog:
return "[WARNING]: "
case errorLog:
return "[ERROR]: "
case fatalLog:
return "[FATAL]: "
default:
return "[UNKNOWN]: "
}
}