https://github.com/kei2100/follow
An io.Reader that keeps track of the file rotations (behaves like `tail -F`)
https://github.com/kei2100/follow
go golang log logging reader tail
Last synced: 4 months ago
JSON representation
An io.Reader that keeps track of the file rotations (behaves like `tail -F`)
- Host: GitHub
- URL: https://github.com/kei2100/follow
- Owner: kei2100
- License: mit
- Created: 2019-05-06T12:22:30.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2025-09-14T08:04:04.000Z (9 months ago)
- Last Synced: 2025-09-14T10:05:59.446Z (9 months ago)
- Topics: go, golang, log, logging, reader, tail
- Language: Go
- Homepage:
- Size: 74.2 KB
- Stars: 11
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# follow
[](https://circleci.com/gh/kei2100/follow)
[](https://ci.appveyor.com/project/kei2100/follow/branch/master)
A file Reader that behaves like `tail -F`
```go
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
}
```