windex

package module
v0.0.0-...-700d42d Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2012 License: MIT Imports: 5 Imported by: 0

README

Windex: Watch and Index

Windex is a library for concurrently watching log files and indexing their data.

A Watcher provides cross-platform file system notifications via github.com/howeyc/fsnotify. These events help keep track of changes to the size of a file over time. This information is stored, and data from the tail of the file is sent via a channel to an external source, known as anIndexer.

The default Indexer flushes to stdout, effectively making windex a programatically accessible tail -f in its default state. However it can be much more powerful than that.

Indexer is an interface that contains two methods:

type Indexer interface {
	Parse(chan []byte) ([]byte, error)
	Flush([]byte) error
}

The stdout indexer does nothing with Parse(chan []byte), and simply prints with Flush([]byte). Since Parse has access to the data, it will be a powerful step toward implementing Indexers which can parse and flush data to any number of a variety of external sources: Redis, Postgres, Statsd, etc.

Here's an example naive implementation of a Redis Indexer:

type RedisIndexer struct {
	address string
}

func (i *RedisIndexer) Parse(log_data chan []byte) (parsed_log_data []byte, err error) {
	parsed_log_data = <-log_data
	return parsed_log_data, nil
}

func (i *RedisIndexer) Flush(parsed_log_data []byte) (err error) {
	redis, err := redis.Dial("tcp", i.address)

	if err != nil {
		log.Fatal(err)
	}

	redis.Do("lpush", "errorz", parsed_log_data)

	log.Print("Data Pushed: ", len(parsed_log_data), " bytes")

	return
}

func NewRedisIndexer(address string) (stdout *RedisIndexer) {
	return &RedisIndexer{
		address: address,
	}
}

Usage

// Use the windex generator to get a windex instance
windex, err = windex.New("logfile01.log")

// Launch the goroutine to watch our file
go windex.Watch()

// Launch the goroutine to index our data. The default indexer
// flushes data to stdout.
go windex.Index()

// Call select on our exit channel. This will let us keep
// everything moving and in the future may be of more use.
select {
case exit := <-windex.Exit:
	if exit {
		return
	}
}

Examples

The above is paraphrased from the example program in example/stdoutindexer/example.go. There is an example that uses the above Redis Indexer in example/redisindexer/example.go

TODO

  • Bring back the tests
  • Add wcld like Indexer

Credits

windex is (c) Michael R. Bernstein, 2012

License

windex is distributed under the MIT License, see LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrIndexZero       = errors.New("index error")
	ErrNoFileName      = errors.New("missing file name")
	ErrNoFile          = errors.New("missing file")
	ErrInvalidFileSize = errors.New("file size invalid")
)

Functions

This section is empty.

Types

type Indexer

type Indexer interface {
	Parse(chan []byte) ([]byte, error)
	Flush([]byte) error
}

type LogFile

type LogFile struct {
	Filename string
	File     *os.File
	// contains filtered or unexported fields
}

func NewLogFile

func NewLogFile(filename string) (log_file *LogFile, err error)

func (*LogFile) Flush

func (log_file *LogFile) Flush(log_data chan []byte)

type LogFileCursor

type LogFileCursor struct {
	// contains filtered or unexported fields
}

type StdoutIndexer

type StdoutIndexer struct {
}

func NewStdoutIndexer

func NewStdoutIndexer() (stdout *StdoutIndexer)

func (*StdoutIndexer) Flush

func (i *StdoutIndexer) Flush(parsed_log_data []byte) (err error)

func (*StdoutIndexer) Parse

func (i *StdoutIndexer) Parse(log_data chan []byte) (parsed_log_data []byte, err error)

type Watcher

type Watcher struct {
	Watcher *fsnotify.Watcher
}

func NewWatcher

func NewWatcher() (watcher *Watcher, err error)

func (*Watcher) Watch

func (watcher *Watcher) Watch(filename string)

type Windex

type Windex struct {
	LogData chan []byte
	Exit    chan bool
	// contains filtered or unexported fields
}

func New

func New(filename string, custom_indexer ...Indexer) (windex *Windex, err error)

func (*Windex) Filename

func (windex *Windex) Filename() (filename string)

func (*Windex) Index

func (windex *Windex) Index()

func (*Windex) Watch

func (windex *Windex) Watch()

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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