-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
313 lines (253 loc) · 8.18 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package rotatorr
import (
"errors"
"fmt"
"io"
"os"
"path"
"path/filepath"
"reflect"
"time"
"golift.io/rotatorr/filer"
)
// These are the default directory and log file POSIX modes.
const (
FileMode os.FileMode = 0o600
DirMode os.FileMode = 0o750
)
// DefaultMaxSize is only used when Every and FileSize Config
// struct members are omitted.
const DefaultMaxSize = 10 * 1024 * 1024
// Custom errors returned by this package.
var (
ErrWriteTooLarge = fmt.Errorf("log msg length exceeds max file size")
ErrNilInterface = fmt.Errorf("nil Rotatorr interface provided")
)
// Config is the data needed to create a new Log Rotatorr.
type Config struct {
Rotatorr Rotatorr // REQUIRED: Custom log Rotatorr. Use your own or one of the provided interfaces.
Filepath string // Full path to log file. Set this, the default is lousy.
FileMode os.FileMode // POSIX mode for new files.
DirMode os.FileMode // POSIX mode for new folders.
Every time.Duration // Maximum log file age. Rotate every hour or day, etc.
FileSize int64 // Maximum log file size in bytes. Default is unlimited (no rotation).
}
// Logger is what you get in return for providing a Config. Use this to set log output.
// You must obtain a Logger by calling one of the New() procedures.
type Logger struct {
config *Config // incoming configurtation.
log chan []byte // incoming log messages passed across go routines.
resp chan *resp // response sent back across go routines.
signal chan struct{} // used for Rotate and Close ops.
size int64 // the size of the active open file.
created time.Time // the date the active open file was created.
File *os.File // The active open file. Useful for direct writing.
Interface Rotatorr // copied from config for brevity.
filer.Filer // overridable file system procedures.
}
// resp is used to send responses back across our go routines.
type resp struct {
size int64
err error
}
// New takes in your configuration and returns a Logger you can use with
// log.SetOutput(). The provided logger handles log rotation and dispatching
// post-actions like compression.
func New(config *Config) (*Logger, error) {
logger := &Logger{config: config, Interface: config.Rotatorr, Filer: filer.Default()}
if err := logger.initialize(false); err != nil {
return nil, err
}
return logger, nil
}
// NewMust takes in your configuration and returns a Logger you can use with
// log.SetOutput(). If an error occurs opening the log file, making log directories,
// or rotating files it is ignored (and retried later). Do not pass a Nil Rotatorr.
func NewMust(config *Config) *Logger {
logger := &Logger{config: config, Interface: config.Rotatorr, Filer: filer.Default()}
if err := logger.initialize(true); errors.Is(err, ErrNilInterface) {
panic(err)
}
return logger
}
// initialize runs all the startup routines.
func (l *Logger) initialize(ignoreErrors bool) error {
var err error
defer func() {
if err == nil || ignoreErrors {
l.log = make(chan []byte)
l.resp = make(chan *resp)
l.signal = make(chan struct{})
go l.processLogChannel()
}
}()
if l.Interface == nil {
err = ErrNilInterface
} else if err = l.setConfigDefaults(); err != nil {
return err
} else {
err = l.checkAndRotate(0)
}
return err
}
// setConfigDefaults does exactly what it says. Sets missing values.
func (l *Logger) setConfigDefaults() error {
if l.config.Filepath == "" {
l.config.Filepath = filepath.Join(os.TempDir(),
filepath.Base(os.Args[0])+"-"+path.Dir(reflect.TypeOf(Logger{}).PkgPath())+".log")
}
if l.config.Every == 0 && l.config.FileSize == 0 {
l.config.FileSize = DefaultMaxSize
}
if l.config.DirMode == 0 {
l.config.DirMode = DirMode
}
if l.config.FileMode == 0 {
l.config.FileMode = FileMode
}
dirs, err := l.Interface.Dirs(l.config.Filepath)
if err != nil {
return fmt.Errorf("validating Rotatorr: %w", err)
}
for _, dir := range dirs {
if err := l.MkdirAll(dir, l.config.DirMode); err != nil {
return fmt.Errorf("making directories for logfiles: %w", err)
}
}
return nil
}
// processLogChannel runs in a go routine and reads the incoming logs channel.
// Received logs are dispatched to the write method. Replies are then sent to the
// response channel. This also handles log rotation and routine shutdown. Everything
// except specific background actions (compression?) happen in this one go routine.
func (l *Logger) processLogChannel() {
for {
select {
case b := <-l.log:
size, err := l.write(b)
l.resp <- &resp{int64(size), err}
case _, ok := <-l.signal:
if !ok {
l.signal = nil
l.resp <- &resp{err: l.stop()}
return
}
size, err := l.rotate()
l.resp <- &resp{size, err}
}
}
}
// openLog opens the log file for writing.
// If the file exists, it is appended to. If it does not exist, it is created.
// Any necessary folders are also created.
func (l *Logger) openLog() error {
err := l.MkdirAll(filepath.Dir(l.config.Filepath), l.config.DirMode)
if err != nil {
return fmt.Errorf("making directories for logfiles: %w", err)
}
perm := os.O_WRONLY | os.O_APPEND
if info, err := l.Stat(l.config.Filepath); err != nil {
// File doesn't exist, or something wrong, truncate it!
perm = os.O_WRONLY | os.O_TRUNC | os.O_CREATE
l.size = 0
l.created = time.Now()
} else {
// File exists, append to it!
l.size = info.Size()
l.created = info.CreateTime
}
l.File, err = l.OpenFile(l.config.Filepath, perm, l.config.FileMode)
if err != nil {
return fmt.Errorf("error with new logfile: %w", err)
}
return nil
}
// Write sends data directly to the file. This satisfies the io.ReadCloser interface.
// You should generally not call this and instead pass *Logger into log.SetOutput().
func (l *Logger) Write(b []byte) (int, error) {
l.log <- b
resp := <-l.resp
return int(resp.size), resp.err
}
// write sends a message into the log file after everyhing checks out - from a channel message.
func (l *Logger) write(b []byte) (int, error) {
if err := l.checkAndRotate(int64(len(b))); err != nil {
return 0, err
}
size, err := l.File.Write(b)
l.size += int64(size)
if err != nil {
return size, fmt.Errorf("error writing log msg: %w", err)
}
return size, nil
}
// checkAndRotate gets the current file's size and creation time.
// Checks if it's too large or too old, and rotates it if so.
// Makes sure the log file is open and ready for writing.
func (l *Logger) checkAndRotate(size int64) error {
if l.File == nil {
if err := l.openLog(); err != nil {
return err
}
}
if l.config.FileSize > 0 && size > l.config.FileSize {
return fmt.Errorf("%w: %d>%d", ErrWriteTooLarge, size, l.config.FileSize)
}
if (l.config.FileSize != 0 && l.size+size > l.config.FileSize) ||
(l.config.Every != 0 && time.Now().After(l.created.Add(l.config.Every))) {
if _, err := l.rotate(); err != nil {
return err
}
}
return nil
}
// Rotate forces the log to rotate immediately. Returns the size of the rotated log.
func (l *Logger) Rotate() (int64, error) {
l.signal <- struct{}{}
resp := <-l.resp
return resp.size, resp.err
}
// rotate renames the log - from a channel message.
func (l *Logger) rotate() (int64, error) {
size := l.size
if err := l.close(); err != nil {
return size, err
}
fpath, err := l.Interface.Rotate(l.config.Filepath)
if fpath != "" {
defer l.Interface.Post(l.config.Filepath, fpath)
}
if err != nil {
return size, fmt.Errorf("error rotatorring: %w", err)
}
return size, l.openLog()
}
// Close stops the go routines, closes the active log file session and all channels.
// If another Write() is sent, a panic will ensue.
func (l *Logger) Close() error {
defer close(l.resp)
close(l.signal)
return (<-l.resp).err
}
// close closes the active log file - from a channel message.
func (l *Logger) close() error {
if l.File == nil {
return nil
}
err := l.File.Close()
l.File = nil
if err != nil {
return fmt.Errorf("closing log file %s: %w", l.config.Filepath, err)
}
return nil
}
// stop closes everything down.
func (l *Logger) stop() error {
if l.log != nil {
close(l.log)
}
l.log = nil
return l.close()
}
// Our interface must satify an io.WriteCloser.
var _ io.WriteCloser = (*Logger)(nil)