追加日志
package main
import (
"time"
"os"
"strconv"
"fmt"
)
func main() {
// If the file exists, removing it.
if _, err := os.Stat("./output.txt"); err == nil {
os.Remove("./output.txt")
fmt.Println("File Removed.")
}
f, _ := os.Create("./output.txt")
i := 1
defer f.Close()
// Append something into the file.
for {
f.WriteString(strconv.Itoa(i))
f.WriteString("\n")
i++;
time.Sleep(100 * time.Millisecond)
}
}
Go语言tail日志
package main
import (
"fmt"
"github.com/hpcloud/tail"
)
func main() {
t, _ := tail.TailFile("./output.txt", tail.Config{Follow: true, Location: &tail.SeekInfo{0, 2}, Logger:tail.DiscardingLogger})
for line := range t.Lines {
fmt.Println(line.Text)
}
}