Tail
Package tail implements file tailing with fsnotify.
Fork of nxadm/tail, simplified, reworked and optimized. Currently, supports only Linux.
go get github.com/go-faster/tail
package main
import (
"context"
"fmt"
"io"
"os"
"time"
"github.com/go-faster/tail"
)
func main() {
t := tail.File("/var/log/application.txt", tail.Config{
Follow: true, // tail -f
BufferSize: 1024 * 128, // 128 kb for internal reader buffer
// Force polling if zero events are observed for longer than a minute.
// Optional, just a safeguard to be sure that we are not stuck forever
// if we miss inotify event.
NotifyTimeout: time.Minute,
// You can specify position to start tailing, same as Seek arguments.
// For example, you can use the latest processed Line.Location() value.
Location: &tail.Location{Whence: io.SeekStart, Offset: 0},
})
ctx := context.Background()
// Enjoy zero allocation fast tailing with context support.
if err := t.Tail(ctx, func(ctx context.Context, l *tail.Line) error {
_, _ = fmt.Fprintln(os.Stdout, string(l.Data))
return nil
}); err != nil {
panic(err)
}
}
TODO
- Decide on ergonomics of ErrStop
- Tests for removing, tailing and creating events
- Benchmarks
- Decide how to deal with partial lines (i.e. EOF, but without
\n
) - Decide on Windows support