Skip to content

Commit

Permalink
Fix error handling in tail methods
Browse files Browse the repository at this point in the history
  • Loading branch information
RemcodM committed Sep 15, 2021
1 parent 600346e commit ee27d84
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/hpcloud/tail"
"gopkg.in/tomb.v1"
)

func tailFile(ctx context.Context, file string, poll bool, dest *os.File) {
Expand Down Expand Up @@ -49,7 +50,7 @@ func tailFile(ctx context.Context, file string, poll bool, dest *os.File) {
return
// get the next log line and echo it out
case line := <-t.Lines:
if t.Err() != nil {
if t.Err() != nil && t.Err() != tomb.ErrStillAlive {
log.Printf("Warning: unable to tail %s: %s", file, t.Err())
errCount++
if errCount > 30 {
Expand All @@ -58,6 +59,13 @@ func tailFile(ctx context.Context, file string, poll bool, dest *os.File) {
time.Sleep(2 * time.Second) // Sleep for 2 seconds before retrying
} else if line == nil {
return
} else if line.Err != nil {
log.Printf("Warning: unable to tail %s: %s", file, t.Err())
errCount++
if errCount > 30 {
log.Fatalf("Logged %d consecutive errors while tailing. Exiting", errCount)
}
time.Sleep(2 * time.Second) // Sleep for 2 seconds before retrying
} else {
fmt.Fprintln(dest, line.Text)
errCount = 0 // Zero the error count
Expand Down

0 comments on commit ee27d84

Please sign in to comment.