How to follow a (changing) log file in node.js

2019-05-07 05:28发布

OK this might appear to be an easy question but I couldn't find the answer from here so I am posting it in hope someone might have encountered the similar problem.

I need to monitor a symlink which points to a web server file (/var/log/lighttpd/error.log to be more specific, thanks to Linus G Thiel I figured out how to follow symlinks). I know I can set up fs.fileWatch to monitor it but I should also point out that the error.log file also got rotated at a specific time, depending on whatever the log daemon settings are. When that happens, fs.fileWatch stops working. I also know I can spawn a child process and run

tail -F ./symlink_to_error.log

from node to work around the problem caused by log rotation, but I prefer to use native node functions. Anyone can shed some light on this?

[ EDIT ]

Actually monitoring the actual log file works without any problem, even when the log file got rotated. The problem is actually caused by the symlink. The reason I am monitoring the symlink is because the actual log file name gets changed when the size reaches certain limit. /var/log/lighttpd/error.log is just given as an example. I have no control over how the log file is renamed but I do have a crontab that updates the symlink every minute that updates the symlink.

[ EDIT 2/28/2012 ]

Actually I am using the following method (through spawn)

tail -F ./symlink_to_error.log

in a log monitoring project that I am working on as it works quite reliably event though it's not as efficient as watchFile().

标签: node.js tail fs
1条回答
可以哭但决不认输i
2楼-- · 2019-05-07 06:01

In addition to watching the symlink and/or its target file, watch the directory that contains the target log file and on the "change" event check to see if the log file has rolled to a different name. If it has then setup a new file watcher on the new log file.

fs.watchFile(logDir, function(curr, prev) {
  if (curr.nlink != prev.nlink) {
    // The number of links in the directory has changed, now
    // see if there is a new log file and start watching it.
  }
});
查看更多
登录 后发表回答