monitoring create/removal of log files
This commit is contained in:
@@ -0,0 +1,54 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"regexp"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/radovskyb/watcher"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
w := watcher.New()
|
||||||
|
|
||||||
|
w.FilterOps(watcher.Create, watcher.Remove)
|
||||||
|
|
||||||
|
r := regexp.MustCompile("^abc$")
|
||||||
|
w.AddFilterHook(watcher.RegexFilterHook(r, false))
|
||||||
|
|
||||||
|
// dispatch creation/removal events here
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case event := <-w.Event:
|
||||||
|
// fmt.Println(event)
|
||||||
|
fmt.Printf("%s (%s)\n", event.Path, event.Op)
|
||||||
|
case err := <-w.Error:
|
||||||
|
log.Fatalln(err)
|
||||||
|
case <-w.Closed:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err := w.Add("."); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for path, f := range w.WatchedFiles() {
|
||||||
|
fmt.Printf("%s: %s\n", path, f.Name())
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("=== startup complete ===")
|
||||||
|
|
||||||
|
// go func() {
|
||||||
|
// w.Wait()
|
||||||
|
// w.TriggerEvent(watcher.Create, nil)
|
||||||
|
// }()
|
||||||
|
|
||||||
|
if err := w.Start(time.Millisecond * 100); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user