Skip to content

Commit f0aa540

Browse files
committed
bugfix when watching multiple directories
1 parent ba8d15c commit f0aa540

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

tailer/fswatcher/fswatcher.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,11 @@ func (t *fileTailer) watchDirs(log logrus.FieldLogger) Error {
251251

252252
func (t *fileTailer) syncFilesInDir(dir *Dir, readall bool, log logrus.FieldLogger) Error {
253253
watchedFilesAfter := make(map[string]*fileWithReader)
254+
for path, file := range t.watchedFiles {
255+
if filepath.Dir(path) != dir.Path() {
256+
watchedFilesAfter[path] = file
257+
}
258+
}
254259
fileInfos, Err := dir.ls()
255260
if Err != nil {
256261
return Err

tailer/fswatcher/fswatcher_linux.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ func (w *watcher) processEvent(t *fileTailer, fsevent fsevent, log logrus.FieldL
118118
if dir == nil {
119119
return NewError(NotSpecified, nil, "watch list inconsistent: received a file system event for an unknown directory")
120120
}
121-
log.WithField("directory", dir.path).Debugf("received event: %v", event)
121+
dirLogger := log.WithField("directory", dir.path)
122+
dirLogger.Debugf("received event: %v", event)
122123
if event.Mask&syscall.IN_IGNORED == syscall.IN_IGNORED {
123124
unwatchDirByEvent(t, event) // need to remove it from watchedDirs, because otherwise we close the removed dir on shutdown which causes an error
124125
return NewErrorf(NotSpecified, nil, "%s: directory was removed while being watched", dir.path)
@@ -139,7 +140,7 @@ func (w *watcher) processEvent(t *fileTailer, fsevent fsevent, log logrus.FieldL
139140
}
140141
file.reader.Clear()
141142
}
142-
readErr := t.readNewLines(file, log)
143+
readErr := t.readNewLines(file, dirLogger)
143144
if readErr != nil {
144145
return readErr
145146
}
@@ -152,7 +153,7 @@ func (w *watcher) processEvent(t *fileTailer, fsevent fsevent, log logrus.FieldL
152153
// Trying to figure out what happened from the events would be error prone.
153154
// Therefore, we don't care which of the above events we received, we just update our watched files with the current
154155
// state of the watched directory.
155-
err := t.syncFilesInDir(dir, true, log)
156+
err := t.syncFilesInDir(dir, true, dirLogger)
156157
if err != nil {
157158
return err
158159
}

tailer/fswatcher_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,38 @@ const tests = `
7070
- [expect, test line 3 logfile 1, logdir/logfile1.log]
7171
- [log, test line 4 logfile 2, logdir/logfile2.log]
7272
- [expect, test line 4 logfile 2, logdir/logfile2.log]
73+
74+
- name: multiple directories
75+
commands:
76+
- [mkdir, logdir1]
77+
- [mkdir, logdir2]
78+
- [log, test line 1 dir 1 file 1, logdir1/logfile-1.log]
79+
- [log, test line 1 dir 2 file 1, logdir2/logfile-1.log]
80+
- [start file tailer, readall=true, fail_on_missing_logfile=false, logdir1/*.log, logdir2/*.log]
81+
- [log, test line 1 dir 1 file 2, logdir1/logfile-2.log]
82+
- [log, test line 1 dir 2 file 2, logdir2/logfile-2.log]
83+
- [log, test line 2 dir 1 file 1, logdir1/logfile-1.log]
84+
- [log, test line 2 dir 1 file 2, logdir1/logfile-2.log]
85+
- [log, test line 2 dir 2 file 1, logdir2/logfile-1.log]
86+
- [log, test line 2 dir 2 file 2, logdir2/logfile-2.log]
87+
- [expect, test line 1 dir 1 file 1, logdir1/logfile-1.log]
88+
- [expect, test line 1 dir 1 file 2, logdir1/logfile-2.log]
89+
- [expect, test line 1 dir 2 file 1, logdir2/logfile-1.log]
90+
- [expect, test line 1 dir 2 file 2, logdir2/logfile-2.log]
91+
- [expect, test line 2 dir 1 file 1, logdir1/logfile-1.log]
92+
- [expect, test line 2 dir 1 file 2, logdir1/logfile-2.log]
93+
- [expect, test line 2 dir 2 file 1, logdir2/logfile-1.log]
94+
- [expect, test line 2 dir 2 file 2, logdir2/logfile-2.log]
95+
- [logrotate, logdir1/logfile-1.log, logdir1/logfile-1.log.1]
96+
- [logrotate, logdir2/logfile-1.log, logdir2/logfile-1.log.1]
97+
- [log, test line 3 dir 1 file 1, logdir1/logfile-1.log]
98+
- [log, test line 3 dir 1 file 2, logdir1/logfile-2.log]
99+
- [log, test line 3 dir 2 file 1, logdir2/logfile-1.log]
100+
- [log, test line 3 dir 2 file 2, logdir2/logfile-2.log]
101+
- [expect, test line 3 dir 1 file 1, logdir1/logfile-1.log]
102+
- [expect, test line 3 dir 1 file 2, logdir1/logfile-2.log]
103+
- [expect, test line 3 dir 2 file 1, logdir2/logfile-1.log]
104+
- [expect, test line 3 dir 2 file 2, logdir2/logfile-2.log]
73105
`
74106

75107
// // The following test fails on Windows in tearDown() when removing logdir.
@@ -233,6 +265,11 @@ func setUp(t *testing.T, testName string, loggerCfg loggerConfig, tailerCfg file
233265
}
234266
logger := logrus.New()
235267
logger.Level = logrus.DebugLevel
268+
logger.SetFormatter(&logrus.TextFormatter{
269+
DisableColors: true,
270+
TimestampFormat: "2006-01-02 15:04:05.000",
271+
FullTimestamp: true,
272+
})
236273
ctx.log = logger.WithField("test", testName).WithField("params", params(ctx))
237274
ctx.basedir = mkTempDir(t, ctx)
238275
return ctx

0 commit comments

Comments
 (0)