The Rails logger has the auto_flushing method. It delays writing to the log until the number of entries is the same as whatever this has been set to. This has been around since Rails 2, and is meant to speed things up by stopping constant writes to disk under load.
This is fine most of the time, but we have short running jobs that never hang around long enough to get past the flush - any errors disappear.
Is there any way to guarantee that the log is flushed when the process dies?
EDIT
In the end I did this (but it's very verbose)
af = Rails.logger.auto_flushing
Rails.logger.auto_flushing = true
Rails.logger.error "my message"
Rails.logger.auto_flushing = af
This forces the message out but puts auto flushing back afterwards
For the really complete solution put this in an initializer:
class << Rails.logger
def flush_error( message )
Rails.logger.error message
Rails.logger.flush
end
end
Then just use the method as and when ...
If, for some reason, rails does not run the at_exit
hook (flying monkeys absconding with your sanity), and you really really want those log entries, do as some people said and run this after the important logging statements:
Rails.logger.flush
or
logger.flush
Voila.
Can't you set that to "1" and it will flush after every message?
config.logger.auto_flushing = 1
[EDIT: as madlep points out in a comment above - this is going to be a total resource hog and is likely not the best way of logging in general. But if you're bug-hunting - it's a way of finding it. That and switching your log to "debug level"]
You can also switch it off with:
config.logger.auto_flushing = false
But that just means you have to manually flush...
alternatively, you can specifically flush the log at the end of each of your scripts with:
Rails.logger.flush
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With