Reputation: 28404
I've got a simple logging tool to write log data to disk, it works like this:
fs.createWriteStream
to create a writable stream using the flags 'a' for appending. This creates or opens the file for me to write data to.writeStream.write()
to write data to my log file.This works perfectly, except for in these two cases:
In both cases I can still call writeStream.write()
with no error.
Inspecting my writeStream
object after I either modify or delete the log file while the application is running shows the writeStream.writable
property to be true
. Each time I call writeStream.write()
the writelen
property increases which leads me to believe the writeStream
thinks that everything is OK, even though it is not.
My questions:
writeStream.write()
to stop writing data to my file? How do I fix it?writeStream.write()
to stop writing data to my file? How do I fix it?Upvotes: 0
Views: 102
Reputation: 13598
Everything is OK, as far the write stream is concerned. When you delete the file you are in effect removing an entry in the directory, but the original inode is not deleted, and your app that has it open keeps writing to the disk, only you don't have a pointer to it (a dir entry), so can't see. You will notice that free disk space keeps decreasing, which tells you that things are indeed written. When the app closes the file it is then removed (since no references are left pointing to it), at which point the free disk space will increase again.
The standard practice for you are trying to do is to send a signal (SIGHUP
or SIGUSR1
to the app), and in the signal handler have the app close and re-open the file. This is called "log rotation". There are many node packages that do this for you, or you can do this yourself for the practice :-)
Upvotes: 1