add rotatingloghandler which removes older log entries
This commit is contained in:
parent
04a24a0c60
commit
118a88dace
4 changed files with 44 additions and 8 deletions
34
misc/rotatingloghandler.go
Normal file
34
misc/rotatingloghandler.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package misc
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/apex/log"
|
||||
)
|
||||
|
||||
// RotatingLogHandler implementation.
|
||||
type RotatingLogHandler struct {
|
||||
mu sync.Mutex
|
||||
Entries []*log.Entry
|
||||
maxEntries int
|
||||
}
|
||||
|
||||
// NewRotatingLogHandler creates a new rotating log handler
|
||||
func NewRotatingLogHandler(maxEntries int) *RotatingLogHandler {
|
||||
return &RotatingLogHandler{
|
||||
maxEntries: maxEntries,
|
||||
}
|
||||
}
|
||||
|
||||
// HandleLog implements log.Handler.
|
||||
func (h *RotatingLogHandler) HandleLog(e *log.Entry) error {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
// drop tail if we have more entries than maxEntries
|
||||
if len(h.Entries) > h.maxEntries {
|
||||
h.Entries = append([]*log.Entry{e}, h.Entries[:(h.maxEntries-2)]...)
|
||||
} else {
|
||||
h.Entries = append([]*log.Entry{e}, h.Entries...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue