/
logger.go
247 lines (205 loc) · 6.36 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
_____ _____ _____ ____ ______ _____ ------
| | | | | | | | | | | | |
| | | | | | | | | | | | |
| --- | | | | |-----| |---- | | |-----| |----- ------
| | | | | | | | | | | | |
| ____| |_____ | ____| | ____| | |_____| _____| |_____ |_____
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
Copyright © 2020-2024 Microsoft Corporation. All rights reserved.
Author : <blobfusedev@microsoft.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
*/
package log
import (
"errors"
"log"
"strings"
"time"
"github.com/Azure/azure-storage-fuse/v2/common"
)
// Logger : Interface to define a generic Logger. Implement this to create your new logging lib
type Logger interface {
GetLoggerObj() *log.Logger
SetLogFile(name string) error
SetMaxLogSize(size int)
SetLogFileCount(count int)
SetLogLevel(level common.LogLevel)
Destroy() error
GetType() string
GetLogLevel() common.LogLevel
Debug(format string, args ...interface{})
Trace(format string, args ...interface{})
Info(format string, args ...interface{})
Warn(format string, args ...interface{})
Err(format string, args ...interface{})
Crit(format string, args ...interface{})
LogRotate() error
}
// newLogger : Method to create Logger object
func NewLogger(name string, config common.LogConfig) (Logger, error) {
timeTracker = config.TimeTracker
if len(strings.TrimSpace(config.Tag)) == 0 {
config.Tag = common.FileSystemName
}
if name == "base" {
baseLogger, err := newBaseLogger(LogFileConfig{
LogFile: config.FilePath,
LogLevel: config.Level,
LogSize: config.MaxFileSize * 1024 * 1024,
LogFileCount: int(config.FileCount),
LogTag: config.Tag,
})
if err != nil {
return nil, err
}
return baseLogger, nil
} else if name == "silent" {
silentLogger := &SilentLogger{}
return silentLogger, nil
} else if name == "" || name == "default" || name == "syslog" {
sysLogger, err := newSysLogger(config.Level, config.Tag)
if err != nil {
if err == NoSyslogService {
// Syslog service does not exists on this system
// fallback to file based logging.
return NewLogger("base", config)
}
return nil, err
}
return sysLogger, nil
}
return nil, errors.New("invalid logger type")
}
var logObj Logger
var timeTracker bool
// ------------------ Public methods to use logging lib ------------------
func GetLoggerObj() *log.Logger {
return logObj.GetLoggerObj()
}
func GetLogLevel() common.LogLevel {
return logObj.GetLogLevel()
}
func GetType() string { // TODO: Should I make an enum for this instead?
return logObj.GetType()
}
func TimeTracker() bool {
return timeTracker
}
func SetDefaultLogger(name string, config common.LogConfig) error {
var err error
logObj, err = NewLogger(name, config)
if err != nil || logObj == nil {
return err
}
return nil
}
func SetConfig(config common.LogConfig) error {
timeTracker = config.TimeTracker
if logObj != nil {
if config.FilePath != "" {
err := logObj.SetLogFile(config.FilePath)
if err != nil {
return err
}
}
if config.Level != common.ELogLevel.INVALID() {
logObj.SetLogLevel(config.Level)
}
if config.MaxFileSize != 0 {
logObj.SetMaxLogSize(int(config.MaxFileSize))
}
if config.FileCount != 0 {
logObj.SetLogFileCount(int(config.FileCount))
}
}
return nil
}
func SetLogFile(name string) error {
if logObj != nil {
return logObj.SetLogFile(name)
}
return nil
}
func SetMaxLogSize(size int) {
if logObj != nil {
logObj.SetMaxLogSize(size)
}
}
func SetLogFileCount(count int) {
if logObj != nil {
logObj.SetLogFileCount(count)
}
}
// SetLogLevel : Reset the log level
func SetLogLevel(lvl common.LogLevel) {
if logObj != nil {
logObj.SetLogLevel(lvl)
Crit("SetLogLevel : Log level reset to : %s", lvl.String())
}
}
// Destroy : DeInitialize the logging library
func Destroy() error {
return logObj.Destroy()
}
// ------------------ Public methods for logging events ------------------
// Debug : Debug message logging
func Debug(msg string, args ...interface{}) {
logObj.Debug(msg, args...)
}
// Trace : Trace message logging
func Trace(msg string, args ...interface{}) {
logObj.Trace(msg, args...)
}
// Info : Info message logging
func Info(msg string, args ...interface{}) {
logObj.Info(msg, args...)
}
// Warn : Warning message logging
func Warn(msg string, args ...interface{}) {
logObj.Warn(msg, args...)
}
// Err : Error message logging
func Err(msg string, args ...interface{}) {
logObj.Err(msg, args...)
}
// Crit : Critical message logging
func Crit(msg string, args ...interface{}) {
logObj.Crit(msg, args...)
}
// LogRotate : Rotate the log files explicitly
func LogRotate() error {
return logObj.LogRotate()
}
func init() {
logObj, _ = NewLogger("syslog", common.LogConfig{
Level: common.ELogLevel.LOG_DEBUG(),
})
}
// TimeTracker : Dump time taken by a call
func TimeTrack(start time.Time, location string, name string) {
if timeTracker {
elapsed := time.Since(start)
logObj.Crit("TimeTracker :: [%s] %s => %s", location, name, elapsed)
}
}
// TimeTracker : Dump time taken by a call
func TimeTrackDiff(diff time.Duration, location string, name string) {
if timeTracker {
logObj.Crit("TimeTracker :: [%s] %s => %s", location, name, diff)
}
}