Skip to content

Commit 3a97be4

Browse files
authored
Merge pull request #36 from kei2100/fix-example
fix example
2 parents 989b4e9 + 6ec05c8 commit 3a97be4

File tree

2 files changed

+63
-83
lines changed

2 files changed

+63
-83
lines changed

README.md

+31-41
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,46 @@ A file Reader that behaves like `tail -F`
66

77
```go
88
func ExampleReader() {
9-
dir, _ := ioutil.TempDir("", "follow-test")
10-
path := filepath.Join(dir, "test.log")
11-
f1, _ := os.Create(path)
12-
13-
// create follow.Reader.
9+
dir, _ := os.MkdirTemp("", "ExampleReader")
10+
logpath := filepath.Join(dir, "test.log")
11+
logfile, _ := os.Create(logpath)
12+
// Create follow.Reader.
1413
// follow.Reader is a file Reader that behaves like `tail -F`
15-
options := []follow.OptionFunc{
16-
// position-file supported
14+
opts := []follow.OptionFunc{
1715
follow.WithPositionFile(posfile.InMemory(nil, 0)),
18-
follow.WithRotatedFilePathPatterns([]string{filepath.Join(dir, "*.log.*")}),
16+
follow.WithRotatedFilePathPatterns([]string{filepath.Join(dir, "test.log.*")}),
1917
follow.WithDetectRotateDelay(0),
2018
follow.WithWatchRotateInterval(100 * time.Millisecond),
2119
}
22-
reader, _ := follow.Open(path, options...)
23-
24-
f1.WriteString("1")
25-
b, _ := ioutil.ReadAll(reader)
26-
fmt.Printf("%s\n", b)
27-
28-
// rotate
29-
f1.Close()
30-
os.Rename(path, path+".f1")
31-
f2, _ := os.Create(path)
32-
33-
f2.WriteString("2")
34-
time.Sleep(500 * time.Millisecond) // wait for detect rotate
35-
b, _ = ioutil.ReadAll(reader)
36-
fmt.Printf("%s\n", b)
37-
38-
// write and rotate while closing the follow.Reader
39-
reader.Close()
40-
f2.WriteString("3")
41-
f2.Close()
42-
43-
os.Rename(path, path+".f2")
44-
f3, _ := os.Create(path)
45-
defer f3.Close()
46-
f3.WriteString("4")
47-
48-
// re-open follow.Reader
49-
reader, _ = follow.Open(path, options...)
20+
reader, _ := follow.Open(logpath, opts...)
5021
defer reader.Close()
51-
52-
time.Sleep(500 * time.Millisecond) // wait for detect rotate
53-
b, _ = ioutil.ReadAll(reader)
54-
fmt.Printf("%s\n", b)
22+
// Reads log files while tracking their rotation
23+
go func() {
24+
for {
25+
b, _ := io.ReadAll(reader)
26+
if len(b) > 0 {
27+
fmt.Print(string(b))
28+
}
29+
time.Sleep(100 * time.Millisecond)
30+
}
31+
}()
32+
// Write to logfile
33+
fmt.Fprintln(logfile, "1")
34+
fmt.Fprintln(logfile, "2")
35+
// Rotate logfile
36+
logfile.Close()
37+
os.Rename(logpath, logpath+".1")
38+
logfile, _ = os.Create(logpath)
39+
// Write to new logfile
40+
fmt.Fprintln(logfile, "3")
41+
fmt.Fprintln(logfile, "4")
42+
logfile.Close()
43+
time.Sleep(time.Second)
5544

5645
// Output:
5746
// 1
5847
// 2
59-
// 34
48+
// 3
49+
// 4
6050
}
6151
```

example_test.go

+32-42
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package follow_test
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"io"
66
"os"
77
"path/filepath"
88
"time"
@@ -12,55 +12,45 @@ import (
1212
)
1313

1414
func ExampleReader() {
15-
dir, _ := ioutil.TempDir("", "follow-test")
16-
path := filepath.Join(dir, "test.log")
17-
f1, _ := os.Create(path)
18-
19-
// create follow.Reader.
15+
dir, _ := os.MkdirTemp("", "ExampleReader")
16+
logpath := filepath.Join(dir, "test.log")
17+
logfile, _ := os.Create(logpath)
18+
// Create follow.Reader.
2019
// follow.Reader is a file Reader that behaves like `tail -F`
21-
options := []follow.OptionFunc{
22-
// position-file supported
20+
opts := []follow.OptionFunc{
2321
follow.WithPositionFile(posfile.InMemory(nil, 0)),
24-
follow.WithRotatedFilePathPatterns([]string{filepath.Join(dir, "*.log.*")}),
22+
follow.WithRotatedFilePathPatterns([]string{filepath.Join(dir, "test.log.*")}),
2523
follow.WithDetectRotateDelay(0),
2624
follow.WithWatchRotateInterval(100 * time.Millisecond),
2725
}
28-
reader, _ := follow.Open(path, options...)
29-
30-
f1.WriteString("1")
31-
b, _ := ioutil.ReadAll(reader)
32-
fmt.Printf("%s\n", b)
33-
34-
// rotate
35-
f1.Close()
36-
os.Rename(path, path+".f1")
37-
f2, _ := os.Create(path)
38-
39-
f2.WriteString("2")
40-
time.Sleep(500 * time.Millisecond) // wait for detect rotate
41-
b, _ = ioutil.ReadAll(reader)
42-
fmt.Printf("%s\n", b)
43-
44-
// write and rotate while closing the follow.Reader
45-
reader.Close()
46-
f2.WriteString("3")
47-
f2.Close()
48-
49-
os.Rename(path, path+".f2")
50-
f3, _ := os.Create(path)
51-
defer f3.Close()
52-
f3.WriteString("4")
53-
54-
// re-open follow.Reader
55-
reader, _ = follow.Open(path, options...)
26+
reader, _ := follow.Open(logpath, opts...)
5627
defer reader.Close()
57-
58-
time.Sleep(500 * time.Millisecond) // wait for detect rotate
59-
b, _ = ioutil.ReadAll(reader)
60-
fmt.Printf("%s\n", b)
28+
// Reads log files while tracking their rotation
29+
go func() {
30+
for {
31+
b, _ := io.ReadAll(reader)
32+
if len(b) > 0 {
33+
fmt.Print(string(b))
34+
}
35+
time.Sleep(100 * time.Millisecond)
36+
}
37+
}()
38+
// Write to logfile
39+
fmt.Fprintln(logfile, "1")
40+
fmt.Fprintln(logfile, "2")
41+
// Rotate logfile
42+
logfile.Close()
43+
os.Rename(logpath, logpath+".1")
44+
logfile, _ = os.Create(logpath)
45+
// Write to new logfile
46+
fmt.Fprintln(logfile, "3")
47+
fmt.Fprintln(logfile, "4")
48+
logfile.Close()
49+
time.Sleep(time.Second)
6150

6251
// Output:
6352
// 1
6453
// 2
65-
// 34
54+
// 3
55+
// 4
6656
}

0 commit comments

Comments
 (0)