10

在 Rails 项目中配置日志记录功能的最佳方法是什么?我正在寻找可用于 Rails 的类似 Log4J 的东西。我找到了 log4r,它在 Logger 类中内置,并且还尝试了“Logging”gem,它在配置为审计记录器时存在一些问题。请让我知道您对此主题的建议,因为我是该主题的初学者。

我在 logging.rb 中使用了下面的代码块并包含在 environment.rb 但是我收到一个关于“returning”关键字的错误,因为它在 rails 2.8 上已被弃用

配置/环境.rb

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

Rails::Initializer.run do |config|

配置/日志记录.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
4

2 回答 2

4

看看以下线程:

Rails 日志记录 API

登录 Rails 应用程序

什么是铁轨的良好日志替代品?

于 2011-07-29T04:59:51.140 回答
2

它应该是这样的:

配置/日志记录.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
于 2011-11-08T14:04:12.993 回答