论坛首页 编程语言技术论坛

按日期存储Rails日志

浏览 3422 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-09-19  

最简单的方法是

 

RAILS_DEFAULT_LOGGER = Logger.new("#{RAILS_ROOT}/log/#{RAILS_ENV}.log", "daily") 

 

实际上Rails做了一些优化,通过ActiveSupport::BufferedLogger来提高产品环境下的性能。所以比较好的做法是在config/environment.rb加入如下代码:

 

	config.logger = begin
		path = config.paths.log.to_a.first
		logger = ActiveSupport::BufferedLogger.new(path, "daily")
		logger.level = ActiveSupport::BufferedLogger.const_get(config.log_level.to_s.upcase)
		logger.auto_flushing = false if Rails.env.production?
		logger
	rescue StandardError => e
		logger = ActiveSupport::BufferedLogger.new(STDERR)
		logger.level = ActiveSupport::BufferedLogger::WARN
		logger.warn(
			"Rails Error: Unable to access log file. Please ensure that #{path} exists and is chmod 0666. " +
			"The log level has been raised to WARN and the output directed to STDERR until the problem is fixed."
		)
		logger
	end
 

 

 

   发表时间:2010-10-08  
很好,学习了。日志的名称如何命名呢?
0 请登录后投票
   发表时间:2010-10-09   最后修改:2010-10-09
这个方式以前在实际在生产环境中使用发现是有问题的,因为生产环境是多app进程,而日志的I/0被在第一个进程中打开后就阻塞了其他进程的写入。最后是通过设置计划任务来切割主日志
不知道新版的Rails解决里没
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics