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

时光怂恿深爱的人放手 提交于 2019-12-07 10:31:24

问题


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().


回答1:


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.
  }
});


来源:https://stackoverflow.com/questions/9370092/how-to-follow-a-changing-log-file-in-node-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!