extract number of errors and pod directory

This commit is contained in:
2022-09-07 20:09:49 +02:00
parent 95f86d69f4
commit 620cef3310
6 changed files with 174 additions and 5 deletions

View File

@ -7,18 +7,42 @@ import (
"regexp"
)
type Option func(monitor *LogMonitor)
// WithTailFollow Continue looking for new lines (tail -f)
func WithTailFollow(follow bool) Option {
return func(monitor *LogMonitor) {
monitor.follow = follow
}
}
// WithReOpen Reopen recreated files (tail -F)
func WithReOpen(reopen bool) Option {
return func(monitor *LogMonitor) {
monitor.reopen = reopen
}
}
type Monitor interface {
Close() error
Watch(filename string, filter *regexp.Regexp, lineChan chan<- string) error
}
func New() *LogMonitor {
return &LogMonitor{
func New(opts ...Option) *LogMonitor {
l := &LogMonitor{
follow: true,
reopen: true,
cancel: make(chan interface{}),
}
for _, opt := range opts {
opt(l)
}
return l
}
type LogMonitor struct {
follow bool
reopen bool
cancel chan interface{}
}
@ -28,7 +52,7 @@ func (l *LogMonitor) Close() error {
}
func (l *LogMonitor) Watch(filename string, filter *regexp.Regexp, lineChan chan<- string) error {
t, err := tail.TailFile(filename, tail.Config{Follow: true, ReOpen: true, Logger: tail.DiscardingLogger})
t, err := tail.TailFile(filename, tail.Config{Follow: l.follow, ReOpen: l.reopen, Logger: tail.DiscardingLogger})
if err != nil {
return fmt.Errorf("unable to begin to tail file '%s': %w", filename, err)
}
@ -36,11 +60,15 @@ func (l *LogMonitor) Watch(filename string, filter *regexp.Regexp, lineChan chan
for {
select {
case line := <-t.Lines:
if line == nil {
// EOF
t.Cleanup()
return nil
}
if line.Err != nil {
zap.S().Errorf("tail error: %v", err)
continue
}
zap.S().Infof("new line: '%s'", line.Text)
if filter.MatchString(line.Text) {
lineChan <- line.Text
}
@ -50,5 +78,4 @@ func (l *LogMonitor) Watch(filename string, filter *regexp.Regexp, lineChan chan
return nil
}
}
return nil
}