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"
"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
Click to show internal directories.
Click to hide internal directories.