Skip to content
This repository has been archived by the owner on Apr 2, 2018. It is now read-only.

Commit

Permalink
Watch for logs within subdirectories.
Browse files Browse the repository at this point in the history
Signed-off-by: Saman Alvi <[email protected]>
  • Loading branch information
andrew-su authored and pivotal-saman-alvi committed Oct 27, 2016
1 parent fd8a846 commit fa4c4a5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
34 changes: 21 additions & 13 deletions file_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,32 @@ func (f *fileWatcher) Watch() {
continue
}

logFiles, err := ioutil.ReadDir(tagDirPath)
if err != nil {
f.logger.Fatalf("could not list files in log dir %s: %s\n", tag, err)
}
f.findLogsToWatch(tag, tagDirPath, fileInfo)

}

for _, logFile := range logFiles {
if !strings.HasSuffix(logFile.Name(), ".log") {
continue
}
time.Sleep(POLL_INTERVAL)
}
}

logFileFullPath := filepath.Join(tagDirPath, logFile.Name())
if _, found := f.dynamicGroupClient.Get(logFileFullPath); !found {
f.dynamicGroupClient.Inserter() <- f.memberForFile(logFileFullPath)
}
func (f *fileWatcher) findLogsToWatch(tag string, filePath string, file os.FileInfo) {
if !file.IsDir() {
if strings.HasSuffix(file.Name(), ".log") {
if _, found := f.dynamicGroupClient.Get(filePath); !found {
f.dynamicGroupClient.Inserter() <- f.memberForFile(filePath)
}
}
return
}

time.Sleep(POLL_INTERVAL)
dirContents, err := ioutil.ReadDir(filePath)
if err != nil {
f.logger.Fatalf("could not list files in log dir %s: %s\n", tag, err)
}

for _, content := range dirContents {
currentFilePath := filepath.Join(filePath, content.Name())
f.findLogsToWatch(tag, currentFilePath, content)
}
}

Expand Down
19 changes: 14 additions & 5 deletions integration/syslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,13 @@ var _ = Describe("Blackbox", func() {
Expect(message.Content).To(ContainSubstring(Hostname()))
})

It("ignores subdirectories in tag directories", func() {
err := os.Mkdir(filepath.Join(logDir, tagName, "ignore-me"), os.ModePerm)
It("does not ignore subdirectories in tag directories", func() {
err := os.Mkdir(filepath.Join(logDir, tagName, "do-not-ignore-me"), os.ModePerm)
Expect(err).NotTo(HaveOccurred())

err = ioutil.WriteFile(
filepath.Join(logDir, tagName, "ignore-me", "and-my-son.log"),
[]byte("some-data"),
childLog, err := os.OpenFile(
filepath.Join(logDir, tagName, "do-not-ignore-me", "and-my-son.log"),
os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
os.ModePerm,
)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -356,6 +356,15 @@ var _ = Describe("Blackbox", func() {
Expect(message.Content).To(ContainSubstring("test-tag"))
Expect(message.Content).To(ContainSubstring(Hostname()))

childLog.WriteString("child data\n")
childLog.Sync()
childLog.Close()

Eventually(inbox.Messages, "5s").Should(Receive(&message))
Expect(message.Content).To(ContainSubstring("child data"))
Expect(message.Content).To(ContainSubstring("test-tag"))
Expect(message.Content).To(ContainSubstring(Hostname()))

blackboxRunner.Stop()
})

Expand Down

0 comments on commit fa4c4a5

Please sign in to comment.