forked from zajann/easylog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
easylog.go
167 lines (140 loc) · 2.77 KB
/
easylog.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
package easylog
import (
"fmt"
"io"
"log"
"os"
"sync"
"time"
)
const (
defaultLevel = DEBUG
defaultMaxSizeBytes = int64(100 << (10 * 2)) // 100 MiB
)
var (
level LogLevel = defaultLevel
logger *log.Logger
fp *os.File
maxSize int64 = defaultMaxSizeBytes
filePath string
mutex sync.Mutex
stdout *os.File = os.Stdout
)
// logFilePath = "" means only log to stdout
func Init(logFilePath string) error {
closeFile()
filePath = logFilePath
return checkFile(false)
}
func Logger() *log.Logger {
return logger
}
func check(err error) {
if err != nil {
panic(err)
}
}
func logAt(l LogLevel, a ...any) string {
if l < level {
return ""
}
check(checkFile(false))
sprintArgs := []interface{}{levelFmt[l], ": "}
sprintArgs = append(sprintArgs, a...)
msg := fmt.Sprint(sprintArgs...)
logger.Output(3, msg)
return msg
}
func logAtf(l LogLevel, format string, a ...any) string {
if l < level {
return ""
}
check(checkFile(false))
msg := fmt.Sprintf("%s: %s", levelFmt[l], fmt.Sprintf(format, a...))
logger.Output(3, msg)
return msg
}
func Debug(a ...any) {
logAt(DEBUG, a...)
}
func Info(a ...any) {
logAt(INFO, a...)
}
func Warn(a ...any) {
logAt(WARN, a...)
}
func Error(a ...any) {
logAt(ERROR, a...)
}
func Fatal(a ...any) {
panic(logAt(FATAL, a...))
}
func Debugf(format string, a ...any) {
logAtf(DEBUG, format, a...)
}
func Infof(format string, a ...any) {
logAtf(INFO, format, a...)
}
func Warnf(format string, a ...any) {
logAtf(WARN, format, a...)
}
func Errorf(format string, a ...any) {
logAtf(ERROR, format, a...)
}
func Fatalf(format string, a ...any) {
panic(logAtf(FATAL, format, a...))
}
func checkFile(locked bool) error {
if !locked {
mutex.Lock()
defer mutex.Unlock()
}
var err error
if fp == nil {
var writer io.Writer
if filePath == "" {
fp = stdout
writer = stdout
} else {
if fp, err = os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666); err != nil {
return err
}
writer = io.MultiWriter(stdout, fp)
}
logger = log.New(writer, "", log.Ldate|log.Ltime|log.Lshortfile|log.LUTC)
}
info, err := fp.Stat()
if err != nil {
return err
}
if info.Size() >= maxSize {
closeFile()
new := fmt.Sprintf("%s.bak.%s", filePath, time.Now().Format("20060102150405"))
if err = os.Rename(filePath, new); err != nil {
return err
}
return checkFile(true)
}
return nil
}
func closeFile() {
if fp == nil {
return
}
fp.Close()
fp = nil
}
func GetLevel() LogLevel {
return level
}
func SetLevel(lv LogLevel) {
level = lv
}
func SetMaxSize(sizeMiB int) {
if sizeMiB <= 0 {
maxSize = defaultMaxSizeBytes
Error("SetMaxSize: sizeMiB must be greater than 0, using default value: ", defaultMaxSizeBytes)
} else {
maxSize = int64(sizeMiB) << (10 * 2) // MiB to Byte
}
}