什么是最好的Rails Logging Gem

时间:2011-07-26 17:25:40

标签: ruby-on-rails logging log4j log4r

在rails项目上配置日志功能的最佳方法是什么?我正在寻找Rails可用的Log4J之类的东西。我发现log4r并且它在Logger类中构建了冲突并且还尝试了“Logging”gem并且它在配置为审计记录器时存在一些问题。由于我是这方面的初学者,请让我知道你对这个主题的建议。

我在logging.rb中使用了以下代码块并包含在environment.rb中 但是我收到了'returns'关键字的错误,因为它已在rails 2.8上弃用了

到config / environment.rb

# Logging
require File.join(File.dirname(__FILE__), 'logging')

Rails::Initializer.run do |config|

配置/ logging.rb

require 'logging'

# Logging.init is required to avoid 
#   unknown level was given 'info' (ArgumentError)
# or
#   uninitialized constant Logging::MAX_LEVEL_LENGTH (NameError)
# when an Appender or Layout is created BEFORE any Logger is instantiated:
Logging.init :debug, :info, :warn, :error, :fatal

layout = Logging::Layouts::Pattern.new :pattern => "[%d] [%-5l] %m\n"

# Default logfile, history kept for 10 days
default_appender = Logging::Appenders::RollingFile.new 'default', \
  :filename => 'log/default.log', :age => 'daily', :keep => 10, :safe => true, :layout => layout

# Audit logfile, history kept forever
audit_appender = Logging::Appenders::RollingFile.new 'audit', \
  :filename => 'log/audit.log', :age => 'daily', :safe => true, :layout => layout

# Production logfile, history kept forever
prod_appender = Logging::Appenders::RollingFile.new 'prod', \
  :filename => 'log/production.log', :age => 'daily', :safe => true, :layout => layout

DEFAULT_LOGGER = returning Logging::Logger['server'] do |l|
  l.add_appenders default_appender
end

2 个答案:

答案 0 :(得分:4)

答案 1 :(得分:2)

应该是这样的:

<强>配置/ logging.rb

require 'logging'

# Logging.init is required to avoid
#   unknown level was given 'info' (ArgumentError)
# or
#   uninitialized constant Logging::MAX_LEVEL_LENGTH (NameError)
# when an Appender or Layout is created BEFORE any Logger is instantiated:
Logging.init :debug, :info, :warn, :error, :fatal

layout = Logging::Layouts::Pattern.new :pattern => "[%d] [%-5l] %m\n"

# Default logfile, history kept for 30 days
default_appender = Logging::Appenders::RollingFile.new 'default', \
  :filename => "log/#{Rails.env}.log", :age => 'daily', :keep => 30, :safe => true, :layout => layout

log = Logging::Logger[:root]
log.add_appenders default_appender
log.level = :info

Rails.logger = log