follow

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2023 License: MIT Imports: 11 Imported by: 0

README

follow

CircleCI Build status

A file Reader that behaves like tail -F

func ExampleReader() {
	dir, _ := os.MkdirTemp("", "ExampleReader")
	logpath := filepath.Join(dir, "test.log")
	logfile, _ := os.Create(logpath)
	// Create follow.Reader.
	// follow.Reader is a file Reader that behaves like `tail -F`
	opts := []follow.OptionFunc{
		follow.WithPositionFile(posfile.InMemory(nil, 0)),
		follow.WithRotatedFilePathPatterns([]string{filepath.Join(dir, "test.log.*")}),
		follow.WithDetectRotateDelay(0),
		follow.WithWatchRotateInterval(100 * time.Millisecond),
	}
	reader, _ := follow.Open(logpath, opts...)
	defer reader.Close()
	// Reads log files while tracking their rotation
	go func() {
		for {
			b, _ := io.ReadAll(reader)
			if len(b) > 0 {
				fmt.Print(string(b))
			}
			time.Sleep(100 * time.Millisecond)
		}
	}()
	// Write to logfile
	fmt.Fprintln(logfile, "1")
	fmt.Fprintln(logfile, "2")
	// Rotate logfile
	logfile.Close()
	os.Rename(logpath, logpath+".1")
	logfile, _ = os.Create(logpath)
	// Write to new logfile
	fmt.Fprintln(logfile, "3")
	fmt.Fprintln(logfile, "4")
	logfile.Close()
	time.Sleep(time.Second)

	// Output:
	// 1
	// 2
	// 3
	// 4
}

Documentation

Index

Examples

Constants

View Source
const (
	DefaultDetectRotateDelay   = 5 * time.Second
	DefaultFollowRotate        = true
	DefaultReadFromHead        = false
	DefaultWatchRotateInterval = 100 * time.Millisecond
)

Default values

Variables

This section is empty.

Functions

This section is empty.

Types

type OptionFunc

type OptionFunc func(o *option)

OptionFunc let you change follow.Reader behavior.

func WithDetectRotateDelay

func WithDetectRotateDelay(v time.Duration) OptionFunc

WithDetectRotateDelay let you change detectRotateDelay

func WithFollowRotate

func WithFollowRotate(follow bool) OptionFunc

WithFollowRotate let you change followRotate

func WithPositionFile

func WithPositionFile(positionFile posfile.PositionFile) OptionFunc

WithPositionFile let you change positionFile

func WithPositionFilePath

func WithPositionFilePath(path string) (OptionFunc, error)

WithPositionFilePath let you change positionFile

func WithReadFromHead

func WithReadFromHead(v bool) OptionFunc

WithReadFromHead let you change readFromHead

func WithRotatedFilePathPatterns

func WithRotatedFilePathPatterns(globPatterns []string) OptionFunc

WithRotatedFilePathPatterns let you change rotatedFilePathPatterns

func WithWatchRotateInterval

func WithWatchRotateInterval(v time.Duration) OptionFunc

WithWatchRotateInterval let you change watchRotateInterval

type Reader

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

Reader is a file reader that behaves like tail -F

Example
package main

import (
	"fmt"
	"io"
	"os"
	"path/filepath"
	"time"

	"github.com/kei2100/follow"
	"github.com/kei2100/follow/posfile"
)

func main() {
	dir, _ := os.MkdirTemp("", "ExampleReader")
	logpath := filepath.Join(dir, "test.log")
	logfile, _ := os.Create(logpath)
	// Create follow.Reader.
	// follow.Reader is a file Reader that behaves like `tail -F`
	opts := []follow.OptionFunc{
		follow.WithPositionFile(posfile.InMemory(nil, 0)),
		follow.WithRotatedFilePathPatterns([]string{filepath.Join(dir, "test.log.*")}),
		follow.WithDetectRotateDelay(0),
		follow.WithWatchRotateInterval(100 * time.Millisecond),
	}
	reader, _ := follow.Open(logpath, opts...)
	defer reader.Close()
	// Reads log files while tracking their rotation
	go func() {
		for {
			b, _ := io.ReadAll(reader)
			if len(b) > 0 {
				fmt.Print(string(b))
			}
			time.Sleep(100 * time.Millisecond)
		}
	}()
	// Write to logfile
	fmt.Fprintln(logfile, "1")
	fmt.Fprintln(logfile, "2")
	// Rotate logfile
	logfile.Close()
	os.Rename(logpath, logpath+".1")
	logfile, _ = os.Create(logpath)
	// Write to new logfile
	fmt.Fprintln(logfile, "3")
	fmt.Fprintln(logfile, "4")
	logfile.Close()
	time.Sleep(time.Second)

}
Output:

1
2
3
4

func Open

func Open(name string, opts ...OptionFunc) (*Reader, error)

Open opens the named file and returns the follow.Reader

func (*Reader) Close

func (r *Reader) Close() error

Close closes the follow.Reader.

func (*Reader) Read

func (r *Reader) Read(p []byte) (n int, err error)

Read reads up to len(b) bytes from the File.

Directories

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

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