tvl-depot/third_party/gerrit-queue/misc/rotatingloghandler.go
Vincent Ambo 59f97332b3 subtree(3p/gerrit-queue): Vendor at commit '24f5a642'
Imported from github/tvlfyi/gerrit-queue, originally from
github/tweag/gerrit-queue but that upstream is unmaintained.

git-subtree-dir: third_party/gerrit-queue
git-subtree-mainline: ff10b7ab83
git-subtree-split: 24f5a642af
Change-Id: I307cc38185ab9e25eb102c95096298a150ae13a2
2021-12-09 16:13:56 +03:00

34 lines
754 B
Go

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
}