main.go
4.46 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
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
package gologger
import (
"fmt"
"os"
"path/filepath"
"runtime"
"sync"
"time"
)
type Option uint8
const (
Events Option = 0x01
Errors Option = 0x02
)
const (
MaxLogSize5MB int64 = 5 * 1024 * 1024
MaxLogSize1MB int64 = 1 * 1024 * 1024
MaxLogSize500KB int64 = 500 * 1024
MaxLogSize100KB int64 = 100 * 1024
MaxLogSize512B int64 = 512
)
const ErrDirName = "error-logs"
const EvtDirName = "event-logs"
type goLogger struct {
muEv *sync.Mutex
eventf *os.File
eventFileName string
muEr *sync.Mutex
errorf *os.File
errorFileName string
splitSize int64
}
var logger goLogger
func Init(flags Option, splitSize int64) error {
timestamp := "_" + time.Now().Format(time.RFC3339)
var err error
logger.splitSize = splitSize
// event file/dir
if flags&Events > 0 {
err = os.Mkdir(EvtDirName, os.ModePerm)
if err != nil {
if !os.IsExist(err) {
fmt.Printf("logger: mkdir: couldn't create event log directory\n")
return err
}
}
logger.eventFileName = "events"
path := filepath.Join(EvtDirName, logger.eventFileName+timestamp+".txt")
logger.eventf, err = os.Create(path)
if err != nil {
fmt.Printf("logger: new: couldn't create event log file\n")
return err
}
}
// error file/dir
if flags&Errors > 0 {
err = os.Mkdir(ErrDirName, os.ModePerm)
if err != nil {
if !os.IsExist(err) {
fmt.Printf("logger: new: couldn't create error log directory\n")
return err
}
}
logger.errorFileName = "errors"
path := filepath.Join(ErrDirName, logger.errorFileName+timestamp+".txt")
logger.errorf, err = os.Create(path)
if err != nil {
fmt.Printf("logger: new: couldn't create error log file\n")
return err
}
}
return nil
}
func Print(s string) {
fmt.Printf(time.Now().Format(time.RFC3339) + ": " + s + "\n")
}
func LogEvent(event string) {
if logger.eventf != nil {
logger.muEv.Lock()
defer logger.muEv.Unlock()
logger.eventf.WriteString(time.Now().Format(time.RFC3339) + ": " + event + "\n")
logger.splitEventLog()
}
}
func LogError(comment string, err error) {
if logger.errorf != nil {
logger.muEr.Lock()
defer logger.muEr.Unlock()
logger.errorf.WriteString(time.Now().Format(time.RFC3339) + ": " + comment + ": " + err.Error() + "\n")
logger.splitErrorLog()
}
}
func TraceEvent(event string) {
if logger.eventf != nil {
logger.muEv.Lock()
defer logger.muEv.Unlock()
_, file, line, ok := runtime.Caller(1)
var s string
if ok {
s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, event)
} else {
s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + event + "\n")
}
logger.eventf.WriteString(s)
logger.splitEventLog()
}
}
func TraceError(err error) {
if logger.errorf != nil {
logger.muEr.Lock()
defer logger.muEr.Unlock()
_, file, line, ok := runtime.Caller(1)
var s string
if ok {
s = fmt.Sprintf("%s %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, err.Error())
} else {
s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + err.Error() + "\n")
}
logger.errorf.WriteString(s)
logger.splitErrorLog()
}
}
func Close() {
if logger.eventf != nil {
err := logger.eventf.Close()
if err != nil {
fmt.Printf("logger: on exit: couldn't close event log file\n")
}
}
if logger.errorf != nil {
err := logger.errorf.Close()
if err != nil {
fmt.Printf("logger: on exit: couldn't close error log file\n")
}
}
}
func (l *goLogger) splitEventLog() {
timestamp := "_" + time.Now().Format(time.RFC3339)
evfstats, _ := l.eventf.Stat()
if evfstats.Size() >= l.splitSize {
// close old file
err := l.eventf.Close()
if err != nil {
fmt.Printf("logger: split: couldn't close event file\n")
return
}
// open new file
var errnew error
path := filepath.Join(EvtDirName, l.eventFileName+timestamp)
l.eventf, errnew = os.Create(path)
if errnew != nil {
fmt.Printf("logger: split: couldn't create event log file\n")
}
}
}
func (l *goLogger) splitErrorLog() {
timestamp := "_" + time.Now().Format(time.RFC3339)
erfstats, _ := l.errorf.Stat()
if erfstats.Size() >= l.splitSize {
// close old file
err := l.errorf.Close()
if err != nil {
fmt.Printf("logger: split: couldn't close error file\n")
return
}
// open new file
var errnew error
path := filepath.Join(ErrDirName, l.errorFileName+timestamp)
l.errorf, errnew = os.Create(path)
if errnew != nil {
fmt.Printf("logger: split: couldn't create error log file\n")
}
}
}