Git Product home page Git Product logo

gotailer's Introduction

gotailer

Go synchronous file tailing API

Preamble

For our task we didn't tail thousands or hundreds or even just tens of files. These were just 6-8 files that were regularly written (hundreds lines per second), so the most effective way of dealing this was just wait for a second (or whatever) and read all new data written. Hence the API developed which is aimed for dealing with small amount of frequently updated files.

Get it

go get github.com/sirkon/gotailer

and use as shown in

Usage Example

package main

import (
	"log"
	"time"

	"github.com/sirkon/gotailer"
	seeker "github.com/sirkon/gotailer/seekers"
)

const fileName = "/tmp/file_to_watch"

func main() {
	// Setting up watcher object
	watcher, err := gotailer.NewPollingMonitor(fileName, seeker.SeekToEnd, seeker.SeekToEnd)
	if err != nil {
		log.Fatalf("Unrecoverable error: %s", err)
	}
	// Watcher object clearance
	defer func() {
		err := watcher.GetFile().Close()
		if err != nil {
			log.Fatal(err)
		}
	}()

	// Setting up scanner object. No clearance needed as it runs over
	// watcher's provided objects
	scanner := gotailer.NewScanner(watcher.GetFile(), 1024, 512*1024)
	if err != nil {
		log.Fatalf("Unrecoverable error: %s", err)
	}

	for {
		// Check if new file is ready
		finished, err := watcher.Test()
		if err != nil {
			log.Fatalf("Unrecoverable error: %s", err)
		}

		// Read out the current file. Finished set to true force reader to treat the rest of file which
		// is not finished with LF as a line. Otherwise, it will wait for the rest of line.
		for scanner.Scan(finished) {
			log.Println(string(scanner.Bytes()))
			log.Printf("Now at %d position", scanner.Tell())
		}

		// Switch sources if a new file is ready for reading since the current one has just been read out
		if finished {
			if err := scanner.Switch(watcher.GetFile()); err != nil {
				log.Fatal(err)
			}
			continue
		}

		time.Sleep(time.Second)
	}
}

gotailer's People

Contributors

izgib avatar sirkon avatar

Stargazers

 avatar

Watchers

 avatar

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.