Documentation
¶
Index ¶
- Constants
- type OptionFunc
- func WithDetectRotateDelay(v time.Duration) OptionFunc
- func WithFollowRotate(follow bool) OptionFunc
- func WithPositionFile(positionFile posfile.PositionFile) OptionFunc
- func WithPositionFilePath(path string) (OptionFunc, error)
- func WithReadFromHead(v bool) OptionFunc
- func WithRotatedFilePathPatterns(globPatterns []string) OptionFunc
- func WithWatchRotateInterval(v time.Duration) OptionFunc
- type Reader
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/ioutil"
"os"
"path/filepath"
"time"
"github.com/kei2100/follow"
"github.com/kei2100/follow/posfile"
)
func main() {
dir, _ := ioutil.TempDir("", "follow-test")
path := filepath.Join(dir, "test.log")
f1, _ := os.Create(path)
// create follow.Reader.
// follow.Reader is a file Reader that behaves like `tail -F`
options := []follow.OptionFunc{
// position-file supported
follow.WithPositionFile(posfile.InMemory(nil, 0)),
follow.WithRotatedFilePathPatterns([]string{filepath.Join(dir, "*.log.*")}),
follow.WithDetectRotateDelay(0),
follow.WithWatchRotateInterval(100 * time.Millisecond),
}
reader, _ := follow.Open(path, options...)
f1.WriteString("1")
b, _ := ioutil.ReadAll(reader)
fmt.Printf("%s\n", b)
// rotate
f1.Close()
os.Rename(path, path+".f1")
f2, _ := os.Create(path)
f2.WriteString("2")
time.Sleep(500 * time.Millisecond) // wait for detect rotate
b, _ = ioutil.ReadAll(reader)
fmt.Printf("%s\n", b)
// write and rotate while closing the follow.Reader
reader.Close()
f2.WriteString("3")
f2.Close()
os.Rename(path, path+".f2")
f3, _ := os.Create(path)
defer f3.Close()
f3.WriteString("4")
// re-open follow.Reader
reader, _ = follow.Open(path, options...)
defer reader.Close()
time.Sleep(500 * time.Millisecond) // wait for detect rotate
b, _ = ioutil.ReadAll(reader)
fmt.Printf("%s\n", b)
}
Output: 1 2 34
Click to show internal directories.
Click to hide internal directories.