Ok so I am monitoring certain file and doing some tail -f analysis of that file. However another application has specific logic and it will create a new file on next date or even before.
I'm looking for a way to detect newly created file (host is linux machine) and then without restart of my current go service to start tailing newly created file.
Here is my current logic for tailing "current file":
func main(){
currentTime := time.Now().Local()
currentFileName := strings.Replace(currentTime.Format("2006-01-02"), "-","",2)
newFileName := currentFileName + "_1111.log.txt"
//using github.com/hpcloud/tail for tail
t, err := tail.TailFile("/var/log/"+newFileName, tail.Config{Follow: true, ReOpen: true})
for line := range t.Lines {
//fmt.Println("Line is:", line.Text)
//check do we have error inside new line
if strings.Contains(strings.ToLower(line.Text), "mfc"){
fmt.Println("MFC located: ", line.Text)
//now send e-mail to all interested parties
//sendEmail(line.Text)
}
}
fmt.Println(err)
}
Well current file instead 20161219_1111.log can be 20161219_2222.log or similar, and on next day it starts from 20161220_1111.log etc.
Any hints are welcome.
have a try with github.com/fsnotify/fsnotify
a simple example:
func newFileCheck() (newFilename chan string, err error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
// do some log
return
}
err = watcher.Watch("testDir")
if err != nil {
// do some log
return
}
newFilename = make(chan string)
// Process events
go func() {
for {
select {
case ev := <-watcher.Event:
log.Println("event:", ev)
newFilename <- ev.Name // Relative path to the file
case err := <-watcher.Error:
log.Println("error:", err)
}
}
}()
return
}
func yourApp() {
newFilenameChan, err := newFileCheck()
if err != nil {
// err check
}
go func() {
for {
select {
case name := <-newFilenameChan:
// close the old one and read new file
}
}
}()
}
more details, see the doc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With