tail

package module
Version: v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2021 License: MIT Imports: 14 Imported by: 0

README

Build Status Build status

tail-ing文件的Go包

Forked From https://github.com/hpcloud/tail

底层实现

  • inotify

内核2.6以上版本可使用

  • poll

通过轮询实现

filename := "/Users/jack/tail.log"

tails, err := tail.TailFile(filename, tail.Config{
ReOpen: true,
Follow: true,
Location:  &tail.SeekInfo{
Offset: 0,
Whence: io.SeekEnd, // 文件尾部
},
MustExist: false,
Poll:      false,
})

See API documentation.

日志分割

支持文件分割/移动检测 可以很好地配合日志分割程序使用

安装

go get github.com/jackcipher/tail

Fork修改内容

检测到iNode节点变更后,重新打开文件,并将游标设置为文件尾部

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultLogger is used when Config.Logger == nil
	DefaultLogger = log.New(os.Stderr, "", log.LstdFlags)
	// DiscardingLogger can be used to disable logging output
	DiscardingLogger = log.New(ioutil.Discard, "", 0)
)
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 {
	// File-specifc
	Location    *SeekInfo // Seek to this location before tailing
	ReOpen      bool      // Reopen recreated files (tail -F)
	MustExist   bool      // Fail early if the file does not exist
	Poll        bool      // Poll for file changes instead of using inotify
	Pipe        bool      // Is a named pipe (mkfifo)
	RateLimiter *ratelimiter.LeakyBucket

	// Generic IO
	Follow      bool // Continue looking for new lines (tail -f)
	MaxLineSize int  // If non-zero, split longer lines into multiple lines

	// Logger, when nil, is set to tail.DefaultLogger
	// To disable logging: set field to tail.DiscardingLogger
	Logger logger
}

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

type Line

type Line struct {
	Text string
	Time time.Time
	Err  error // Error from tail
}

func NewLine

func NewLine(text string) *Line

NewLine returns a Line with present 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) Cleanup

func (tail *Tail) Cleanup()

Cleanup removes inotify watches added by the tail package. This function is meant to be invoked from a process's exit handler. Linux kernel may not automatically remove inotify watches after the process exits.

func (*Tail) Stop

func (tail *Tail) Stop() error

Stop stops the tailing activity.

func (*Tail) StopAtEOF

func (tail *Tail) StopAtEOF() error

StopAtEOF stops tailing as soon as the end of the file is reached.

func (*Tail) Tell

func (tail *Tail) Tell() (offset int64, err 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
cmd
Package ratelimiter implements the Leaky Bucket ratelimiting algorithm with memcached and in-memory backends.
Package ratelimiter implements the Leaky Bucket ratelimiting algorithm with memcached and in-memory backends.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL