Rails Custom Logger Writes To Log Sporadically

左心房为你撑大大i 提交于 2019-12-13 02:48:43

问题


I've set up a custom logger in an initializer:

# /config/initializers/logging.rb

log_file = File.open("#{Example::Application.config.root}/log/app.log", "a")
AppLogger = ActiveSupport::BufferedLogger.new(log_file)
AppLogger.level = Logger::DEBUG
AppLogger.auto_flushing = true
AppLogger.debug "App Logger Up"

Although it creates the log file when I start the application, it doesn't write to the log file. Either from AppLogger.debug "App Logger Up" in the initializer or similar code elsewhere in the running application.

However, intermittently I do find log statements in the file, though seemingly without any pattern. It would seem that it is buffering the log messages and dumping them when it feels like it. However I'm setting auto_flushing to true which should cause it to flush the buffer immediately.

What is going on and how can I get it working?

Note: Tailing the log with $ tail -f "log/app.log" also doesn't pick up changes.

I'm using POW, but I've also tried with WEBrick and get the same (lack of) results.


回答1:


Found the answer in a comment to the accepted answer to on this question:

I added:

log_file.sync = true

And it now works.



来源:https://stackoverflow.com/questions/20707300/rails-custom-logger-writes-to-log-sporadically

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