logger

package module
v0.0.0-...-10fb0a3 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2018 License: MIT Imports: 10 Imported by: 0

README

logger

Like tail -F /log/this/file

go report card Build Status MIT license GoDoc

Usage:

package main

import (
    "fmt"
    "log"
    "os"
    "path/filepath"

    "github.com/xellio/logger"
)

var logFiles []string

func init() {
    for _, file := range os.Args[1:] {
        file = filepath.Clean(file)
        logFiles = append(logFiles, file)
    }
}

func main() {

    if len(logFiles) <= 0 {
        fmt.Println(`Usage:
    logger /path/to/logfile /path/to/another/logfile`)
        os.Exit(1)
    }

    resCh := make(chan *logger.Change)
    errCh := make(chan error)
    go func() {
        err := logger.Start(resCh, logFiles...)
        if err != nil {
            errCh <- err
        }
    }()

    for {
        select {
        case change := <-resCh:
            for _, line := range change.Lines {
                fmt.Println(string(line.Content))
            }

        case err := <-errCh:
            log.Fatalf("Error: %s", err.Error())
            os.Exit(1)
        }
    }

}

Documentation

Index

Constants

View Source
const (
	// ChangeEventCreate ...
	ChangeEventCreate = "CREATE"
	// ChangeEventWrite ...
	ChangeEventWrite = "WRITE"
	// ChangeEventRename ...
	ChangeEventRename = "RENAME"
	// ChangeEventRemove ...
	ChangeEventRemove = "REMOVE"
	// ChangeEventChmod ...
	ChangeEventChmod = "CHMOD"
	// ChangeEventUnknown ...
	ChangeEventUnknown = "UNKNOWN"
)
View Source
const BufferSize = 32

BufferSize for reading the files.

Variables

This section is empty.

Functions

func INode

func INode(file string) (uint64, error)

INode returns the inode for the given file(path).

func Size

func Size(file string) (int64, error)

Size reutrns the filesize of the given file(path).

func Start

func Start(result chan *Change, files ...string) error

Start the logger. New changes in the given files will return on the given channel.

Types

type Change

type Change struct {
	Event string
	File  *File
	Lines []*Line
}

Change struct holds information about *NEW* changes in a logfile.

type File

type File struct {
	Path         string
	INode        uint64
	Size         int64
	LastReadByte int64
}

File struct holds information about a logged file.

func NewFile

func NewFile(path string) (File, error)

NewFile creates and returns a new File struct for the given path.

func (*File) HandleEvent

func (f *File) HandleEvent(op fsnotify.Op) (change *Change, err error)

HandleEvent ...

func (*File) NextLine

func (f *File) NextLine() (line []byte, err error)

NextLine returns the next, unprocessed line in the file.

func (*File) Parse

func (f *File) Parse() (lines []*Line, err error)

Parse will read and return the unparsed/new lines of the file.

func (*File) UpdateSize

func (f *File) UpdateSize() error

UpdateSize will update the files size. If a change to the file is triggered, we should update the size.

type Line

type Line struct {
	File    *File
	Content string
}

Line struct holds information about a single line in a file.

func NewLine

func NewLine(content []byte, file *File) (*Line, error)

NewLine creates and returns a new Line struct for the given content and file.

Directories

Path Synopsis
cmd
cli

Jump to

Keyboard shortcuts

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