Code Monkey home page Code Monkey logo

go-tailer's Introduction

image go-tailer

go-tailer is a Go library designed to help you tail files in a similar fashion to tail -f

License PkgGoDev Security Rating Go Report Card Build/Test

Built off the wonderful work by @fstab in the grok_exporter/tailer module.

See the FOSDEM17: Implementing 'tail -f' presentation for how go-tailer works.

Usage

Basic Usage: File Tailer

Here's a basic example of how to use this library to tail log files.

path := "/var/log/messages" // a single file
path := "/usr/local/myapp/logs/" // a directory
path = "C:\\Program Files\\MyApp\\logs\\*.log" // or a file wildcard

// parse the path glob
parsedGlob, err := glob.Parse(path)
if err != nil {
    panic(fmt.Sprintf("%q: failed to parse glob: %q", parsedGlob, err))
}

// startup a logrus logger
logger := logrus.New()

// startup the file tailer. RunFileTailer can take many path globs
tailer, err := fswatcher.RunFileTailer([]glob.Glob{parsedGlob}, false, true, logger)

// listen to the go channel for captured lines and do something with them
for line := range tailer.Lines() {
    // line.Line contains the line contents
    // line.File contains the name of the file that the line was grabbed from
    DoSomethingWithLine(line.File, line.Line)
}

Polling Tailer

We recommend using the RunFileTailer, which listens for file system events to trigger tailing actions. But if that isn't working for you, you can fall back to a polling listener to periodically read the file for any new log lines.

// specify how often you want the tailer to check for updates
pollInterval := time.Duration(500 * time.Millisecond)

// startup the polling file tailer
tailer, err := fswatcher.RunPollingFileTailer([]glob.Glob{parsedGlob}, false, true, pollInterval, logger)

// listen to the go channel for captured lines and do something with them
for line := range tailer.Lines() {
    DoSomethingWithLine(line.File, line.Line)
}

Other Tailers

Along with reading from files, go-tailer can read from other sources as well.

Frequently Asked Questions (FAQ)

Why was the tailer module forked from fstab/grok_exporter and moved here?

grok_exporter had not been updated for 3-4 years and seems to be abandoned by @fstab. It also was suffering from quite a few security issues (CVEs) , which I've fixed. Additionally, the tailer module was not well known, had poor documentation, and I thought a separate repo would shed some well deserved light on it.

I'd welcome making upstream patches to fstab/grok_exporter if/when it becomes active again!

go-tailer's People

Contributors

asomers avatar dcwangmit01 avatar dependabot[bot] avatar flachesis avatar fstab avatar geekdave avatar gucce avatar hartfordfive avatar janisz avatar jdrews avatar jenserat avatar maxkochubey avatar rhuss avatar roidelapluie avatar skeen avatar virtuald avatar whoawhoawhoa avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

go-tailer's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.