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
|
@ -11,10 +11,9 @@ import (
|
|||
"github.com/rakyll/statik/fs"
|
||||
|
||||
"github.com/tweag/gerrit-queue/gerrit"
|
||||
"github.com/tweag/gerrit-queue/misc"
|
||||
_ "github.com/tweag/gerrit-queue/statik" // register static assets
|
||||
"github.com/tweag/gerrit-queue/submitqueue"
|
||||
|
||||
"github.com/apex/log/handlers/memory"
|
||||
)
|
||||
|
||||
//loadTemplate loads a list of templates, relative to the statikFS root, and a FuncMap, and returns a template object
|
||||
|
@ -48,7 +47,7 @@ func loadTemplate(templateNames []string, funcMap template.FuncMap) (*template.T
|
|||
}
|
||||
|
||||
// MakeFrontend returns a http.Handler
|
||||
func MakeFrontend(memoryHandler *memory.Handler, gerritClient *gerrit.Client, runner *submitqueue.Runner) http.Handler {
|
||||
func MakeFrontend(rotatingLogHandler *misc.RotatingLogHandler, gerritClient *gerrit.Client, runner *submitqueue.Runner) http.Handler {
|
||||
router := gin.Default()
|
||||
|
||||
projectName := gerritClient.GetProjectName()
|
||||
|
@ -88,7 +87,7 @@ func MakeFrontend(memoryHandler *memory.Handler, gerritClient *gerrit.Client, ru
|
|||
"HEAD": HEAD,
|
||||
|
||||
// History
|
||||
"memory": memoryHandler,
|
||||
"memory": rotatingLogHandler,
|
||||
})
|
||||
})
|
||||
return router
|
||||
|
|
3
go.sum
3
go.sum
|
@ -9,6 +9,7 @@ github.com/aws/aws-sdk-go v1.20.6/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN
|
|||
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U2H7sSsE2t6Kca0lfwTK8JdoNGS/yzM/4iH5I=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||
|
@ -41,6 +42,7 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
|
|||
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
|
||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rakyll/statik v0.1.6 h1:uICcfUXpgqtw2VopbIncslhAmE5hwc4g20TEyEENBNs=
|
||||
github.com/rakyll/statik v0.1.6/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6GX8Zs=
|
||||
|
@ -54,6 +56,7 @@ github.com/smartystreets/assertions v1.0.0/go.mod h1:kHHU4qYBaI3q23Pp3VPrmWhuIUr
|
|||
github.com/smartystreets/go-aws-auth v0.0.0-20180515143844-0c1422d1fdb9/go.mod h1:SnhjPscd9TpLiy1LpzGSKh3bXCfxxXuqd9xmQJy3slM=
|
||||
github.com/smartystreets/gunit v1.0.0/go.mod h1:qwPWnhz6pn0NnRBP++URONOVyNkPyr4SauJk4cUOwJs=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLDRpvE+3b7gP/C2YyLFYxNmcLnPTMe0=
|
||||
github.com/tj/go-elastic v0.0.0-20171221160941-36157cbbebc2/go.mod h1:WjeM0Oo1eNAjXGDx2yma7uG2XoyRZTq1uv3M/o7imD0=
|
||||
|
|
8
main.go
8
main.go
|
@ -10,12 +10,12 @@ import (
|
|||
|
||||
"github.com/tweag/gerrit-queue/frontend"
|
||||
"github.com/tweag/gerrit-queue/gerrit"
|
||||
"github.com/tweag/gerrit-queue/misc"
|
||||
"github.com/tweag/gerrit-queue/submitqueue"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/apex/log/handlers/memory"
|
||||
"github.com/apex/log/handlers/multi"
|
||||
"github.com/apex/log/handlers/text"
|
||||
)
|
||||
|
@ -78,11 +78,11 @@ func main() {
|
|||
},
|
||||
}
|
||||
|
||||
memoryLogHandler := memory.New()
|
||||
rotatingLogHandler := misc.NewRotatingLogHandler(10000)
|
||||
l := &log.Logger{
|
||||
Handler: multi.New(
|
||||
text.New(os.Stderr),
|
||||
memoryLogHandler,
|
||||
rotatingLogHandler,
|
||||
),
|
||||
Level: log.DebugLevel,
|
||||
}
|
||||
|
@ -96,7 +96,7 @@ func main() {
|
|||
|
||||
runner := submitqueue.NewRunner(l, gerrit, submitQueueTag)
|
||||
|
||||
handler := frontend.MakeFrontend(memoryLogHandler, gerrit, runner)
|
||||
handler := frontend.MakeFrontend(rotatingLogHandler, gerrit, runner)
|
||||
|
||||
// fetch only on first run
|
||||
runner.Trigger(fetchOnly)
|
||||
|
|
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…
Reference in a new issue