Skip to content

Commit e632e58

Browse files
committed
Evaluate symlinks before starting tailer
When the current directory or the absolute path provided to `--file` is a symlink, we need to convert it to the realpath on the disk. The reason for this, is that inotify will always emit events with the realpath and not the symlink path. Without this change, when the log files exist on the time of starting the tail, everything will be fine. But when you define log files that do not yet exist at the time of starting, they will never be picked up if they are located in a symlink path.
1 parent c1c5888 commit e632e58

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

local/logs/tailer.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,25 @@ func (tailer *Tailer) Watch(pidFile *pid.PidFile) error {
147147
if err := inotify.Watch(dir, watcherChan, inotify.Create); err != nil {
148148
return errors.Wrap(err, "unable to watch the applog directory")
149149
}
150+
151+
// Evaluate possible symlinks in the applog path, this is needed because
152+
// inotify will notify us on source path, and not the symlink path.
153+
if _, err := os.Stat(applog); err == nil {
154+
realAppLog, err := filepath.EvalSymlinks(applog)
155+
if err != nil {
156+
return errors.Wrapf(err, "unable to evaluate symlinks for %s", applog)
157+
}
158+
applog = realAppLog
159+
} else if errors.Is(err, os.ErrNotExist) {
160+
realDir, err := filepath.EvalSymlinks(dir)
161+
if err != nil {
162+
return errors.Wrapf(err, "unable to evaluate symlinks for %s", dir)
163+
}
164+
applog = filepath.Join(realDir, filepath.Base(applog))
165+
} else {
166+
return errors.Wrapf(err, "unable to evaluate symlinks for %s", applog)
167+
}
168+
150169
go func(applog string) {
151170
for {
152171
e := <-watcherChan

0 commit comments

Comments
 (0)