Documentation ¶
Overview ¶
nxadm/tail provides a Go library that emulates the features of the BSD `tail` program. The library comes with full support for truncation/move detection as it is designed to work with log rotation tools. The library works on all operating systems supported by Go, including POSIX systems like Linux and *BSD, and MS Windows. Go 1.9 is the oldest compiler release supported.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultLogger logs to os.Stderr and it 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) )
var ( // ErrStop is returned when the tail of a file has been marked to be stopped. ErrStop = errors.New("tail should now stop") )
Functions ¶
func OpenFile
deprecated
Types ¶
type Config ¶
type Config struct { // File-specifc Location *SeekInfo // Tail from this location. If nil, start at the beginning of the file 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 the default inotify Pipe bool // The file is a named pipe (mkfifo) // Generic IO Follow bool // Continue looking for new lines (tail -f) MaxLineSize int // If non-zero, split longer lines into multiple lines CompleteLines bool // Only return complete lines (that end with "\n" or EOF when Follow is false) // Optionally, use a ratelimiter (e.g. created by the ratelimiter/NewLeakyBucket function) RateLimiter *ratelimiter.LeakyBucket // Optionally use a Logger. When nil, the Logger is set to tail.DefaultLogger. // To disable logging, set it to tail.DiscardingLogger Logger logger }
Config is used to specify how a file must be tailed.
type Line ¶
type SeekInfo ¶
SeekInfo represents arguments to io.Seek. See: https://golang.org/pkg/io/#SectionReader.Seek
type Tail ¶
type Tail struct { Filename string // The filename Lines chan *Line // A consumable channel of *Line Config // Tail.Configuration tomb.Tomb // provides: Done, Kill, Dying // contains filtered or unexported fields }
func TailFile ¶
TailFile begins tailing the file. And returns a pointer to a Tail struct and an error. An output stream is made available via the Tail.Lines channel (e.g. to be looped and printed). To handle errors during tailing, after finishing reading from the Lines channel, invoke the `Wait` or `Err` method on the returned *Tail.
Example ¶
// Keep tracking a file even when recreated. // /var/log/messages is typically continuously written and rotated daily. testFileName := "/var/log/messages" // ReOpen when truncated, Follow to wait for new input when EOL is reached tailedFile, err := TailFile(testFileName, Config{ReOpen: true, Follow: true}) if err != nil { panic(err) } for line := range tailedFile.Lines { fmt.Println(line.Text) } // Prints all the lines in the logfile and keeps printing new input
Output:
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. If you plan to re-read a file, don't call Cleanup in between.
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
examples
|
|
01-tailAndPrint
Tail a file and print its contents.
|
Tail a file and print its contents. |
02-closeAndReopen
Tail a file, print its contents, close it and reopen it.
|
Tail a file, print its contents, close it and reopen it. |
03-consumeJSON
Consume and unmarshall JSON.
|
Consume and unmarshall JSON. |
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. |