Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails log file stops logging if I touch it. How to get it logging again?

My log files generate a LOT of output, and it's difficult to tell where the current action I'm looking at started logging. I though it would be as simple as deleting what was currently in the log file immediately before making the action (or at least adding some spacers), but editing the log files seems to make the application stop logging completely!

I can reset the application to start it logging again, but that brings me right back to square one - digging through a whole bunch of unneeded logs without knowing where the bits I actually wanted start.

Is there a way to edit the log file without killing Rail's ability to log?

like image 990
GlyphGryph Avatar asked Jan 29 '26 06:01

GlyphGryph


2 Answers

Instead of deleting the file, do

cat /dev/null > log/production.log # or whatever the path to your file is

The problem is that when you delete the file, Rails still has the file open, so it is continuing to write to this now "deleted" file. By catting /dev/null to the file, you will clear out all of its contents without changing where the file handle is writing to.

like image 102
Rob Di Marco Avatar answered Jan 30 '26 19:01

Rob Di Marco


My best guess as to why this is happening is that the I/O stream is being interrupted and corrupted (because you're modifying the file) while it's open (your application is running). You either need to restart your app or use Rob's solution.

However, an alternative solution is to use tail to view only the last 10 lines, you can specify more lines with tail -n 30, or whatever amount you wish. An even better solution is to use tail -f, which will show the file's tail and then print out new data as it is written to the file; very useful for monitoring log files.

like image 40
Andrew Marshall Avatar answered Jan 30 '26 18:01

Andrew Marshall