tail

package
v1.12.0-rc.3 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

README

NOTE: This is a fork of https://github.com/grafana/tail, which is a fork of https://github.com/hpcloud/tail. The grafana/tail repo is no longer mainained because the Loki team has deprecated the Promtail project. It is easier for the Alloy team to maintain this tail package inside the Alloy repo than to have a separate repository for it.

Use outside of that context is not tested or supported.

Go package for tail-ing files

A Go package striving to emulate the features of the BSD tail program.

t, err := tail.TailFile("/var/log/nginx.log", tail.Config{Follow: true})
for line := range t.Lines {
    fmt.Println(line.Text)
}

See API documentation.

Log rotation

Tail comes with full support for truncation/move detection as it is designed to work with log rotation tools.

Installing

go get github.com/hpcloud/tail/...

Windows support

This package needs assistance for full Windows support.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrStop = errors.New("tail should now stop")
)

Functions

func OpenFile

func OpenFile(name string) (file *os.File, err error)

Types

type Config

type Config struct {
	Logger log.Logger
	// Seek to this location before tailing
	Location    *SeekInfo
	PollOptions watch.PollingFileWatcherOptions

	// Change the decoder if the file is not UTF-8.
	// If the tailer doesn't use the right decoding, the output text may be gibberish.
	// For example, if the file is "UTF-16 LE" encoded, the tailer would not separate
	// the new lines properly and the output could come out as chinese characters.
	Decoder *encoding.Decoder
}

Config is used to specify how a file must be tailed.

type Line

type Line struct {
	Text string
	Time time.Time
}

type SeekInfo

type SeekInfo struct {
	Offset int64
	Whence int // os.SEEK_*
}

SeekInfo represents arguments to `os.Seek`

type Tail

type Tail struct {
	Filename string
	Lines    chan *Line
	Config

	tomb.Tomb // provides: Done, Kill, Dying
	// contains filtered or unexported fields
}

func TailFile

func TailFile(filename string, config Config) (*Tail, error)

TailFile begins tailing the file. Output stream is made available via the `Tail.Lines` channel. To handle errors during tailing, invoke the `Wait` or `Err` method after finishing reading from the `Lines` channel.

func (*Tail) Size

func (tail *Tail) Size() (int64, error)

Size returns the length in bytes of the file being tailed, or 0 with an error if there was an error Stat'ing the file.

func (*Tail) Stop

func (tail *Tail) Stop() error

Stop stops the tailing activity.

func (*Tail) Tell

func (tail *Tail) Tell() (int64, error)

Return the file's current position, like stdio's ftell(). But this value is not very accurate. it may readed one line in the chan(tail.Lines), so it may lost one line.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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