Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

go-faster/tail

Repository files navigation

tail Go Reference codecov experimental

Package tail implements file tailing with fsnotify.

Fork of nxadm/tail, simplified, reworked and optimized. Currently, supports only Linux and Darwin.

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

  • Tests for removing, tailing and creating events
  • Decide on Windows support