tail

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2021 License: MIT Imports: 9 Imported by: 0

README

tail

Tail a file programmatically just like you run shell command tail -f /somefile.

Features

  • support tail a non-exist file until that file exists
  • tail works even if file get removed or truncated
  • tail from specified offset

Basic usage

// closeFunc is used to stop tailing
lineCh, closeFunc, errCh := tail.TailF("/path/to/file", true)
go func() {
    for {
        select {
        case <-errCh:
            return
        case line := <-lineCh:
            fmt.Println(line)
        }
    }
}()

Advance usage

by default, tail poll file delete/truncate-event every 10 seconds, you can config poll interval to satisfy your own needs.

lineCh, closeFunc, errCh := tail.TailWithConfig("/path/to/file", true, &tail.Config{PollInterval: 500 * time.Millisecond})
go func() {
    for {
        select {
        case <-errCh:
            return
        case line := <-lineCh:
            fmt.Println(line)
        }
    }
}()

More configs

config tail offset
// tail.SeekFromStart/tail.SeekFromEnd/tail.SeekOffsetAuto
lineCh, closeFunc, errCh := tail.TailWithConfig("/path/to/file", true, &tail.Config{
		PollInterval: 500 * time.Millisecond, 
		Offset: tail.SeekFromStart,
})

Limitations

Not tested on windows.

Documentation

Index

Constants

View Source
const (
	// file is eof, eof might triggered multiple times.
	EventEof = "eof"
	// if the tailing file get truncated or deleted, tail will restart
	EventTailRestart = "rs"
)

Variables

View Source
var (
	//SeekFromEnd tail file from end
	SeekFromEnd = &SeekOffset{
		Offset: 0,
		Whence: io.SeekEnd,
	}

	// SeekOffsetAuto tail file from offset (end - 1000) if file size is gt than 1000, else tail from start
	SeekOffsetAuto = &SeekOffset{
		Offset: 0,
		Whence: -1,
	}

	// SeekFromStart tail file from offset 0
	SeekFromStart = &SeekOffset{
		Offset: 0,
		Whence: io.SeekStart,
	}
)

Functions

func TailF

func TailF(filePath string, waitFileExist bool) (chan string, func(), chan error)

func TailWithConfig

func TailWithConfig(filePath string, waitFileExist bool, conf *Config) (chan string, func(), chan error)

Types

type Config

type Config struct {
	PollInterval time.Duration
	Offset       *SeekOffset
	Follow       bool
}

func NewDefaultConfig

func NewDefaultConfig() *Config

type SeekOffset

type SeekOffset struct {
	Offset int64
	Whence int // io.Seek*
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL