Rails Custom Logger偶尔写入日志 [英] Rails Custom Logger Writes To Log Sporadically

查看:69
本文介绍了Rails Custom Logger偶尔写入日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在初始化程序中设置了自定义记录器:

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"

尽管在我启动应用程序时它会创建日志文件,但它不会写入日志文件.可以使用初始化程序中的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.

但是,我断断续续地在文件中找到了日志语句,尽管看似没有任何模式.似乎它正在缓冲日志消息并在感觉到时转储它们.但是,我将auto_flushing设置为true,这应该导致它立即刷新缓冲区.

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.

发生了什么事,如何使它正常工作?

注意:用$ tail -f "log/app.log"跟踪日志也不会获取更改.

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

我正在使用POW,但我也尝试过使用WEBrick并获得相同的(缺乏)结果.

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

推荐答案

在对我添加了:

log_file.sync = true

现在可以使用了.

这篇关于Rails Custom Logger偶尔写入日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆