如何使用Rails 3.0.x配置Log4r?

时间:2011-04-14 13:38:57

标签: ruby-on-rails ruby-on-rails-3 log4r

我尝试根据本文配置log4r和Rails 3.0.4:http://www.dansketcher.com/2007/06/16/integrating-log4r-and-ruby-on-rails/

/Users/toto/.rvm/gems/ruby-1.9.2-p0/gems/log4r-1.1.9/lib/log4r/yamlconfigurator.rb:166:in `sub!': can't convert Pathname into String (TypeError)
    from /Users/toto/.rvm/gems/ruby-1.9.2-p0/gems/log4r-1.1.9/lib/log4r/yamlconfigurator.rb:166:in `block in paramsub'
    from /Users/toto/.rvm/gems/ruby-1.9.2-p0/gems/log4r-1.1.9/lib/log4r/yamlconfigurator.rb:165:in `each'
    from /Users/toto/.rvm/gems/ruby-1.9.2-p0/gems/log4r-1.1.9/lib/log4r/yamlconfigurator.rb:165:in `paramsub'
    from /Users/toto/.rvm/gems/ruby-1.9.2-p0/gems/log4r-1.1.9/lib/log4r/yamlconfigurator.rb:156:in `block in decode_hash_params'

我已经使用Googled进行了Rails 3集成,但还没有找到可行的解决方案。任何人都可以指向一个工作的代码片段,它将允许使用YAML文件进行日志配置,并在运行时进行初始化吗?

作为参考,我将示例logger.rb放在config / initializers文件夹中,将log4r.yml放在config目录中。

由于

3 个答案:

答案 0 :(得分:31)

Hehe ... Log4r的想法来自着名的“Log4j”,这是我在java编程生活中最喜欢的记录器。 但是log4r的doc真的很差,对于新手来说真的很难。让我展示我的解决方案:

步骤1。创建log4r配置文件:(文件名:config / log4r.yml)

log4r_config:
  # define all loggers ...
  loggers:
    - name      : production
      level     : WARN
      trace     : 'false'
      outputters :
      - datefile
    - name      : development
      level     : DEBUG
      trace     : 'true'
      outputters :
      - datefile

  # define all outputters (incl. formatters)
  outputters:
  - type: DateFileOutputter
    name: datefile
    dirname: "log"
    # notice the file extension is needed! 
    # if you want the file is named by the process, just comment it,
    # then it will automatically get the same name with its process,
    # e.g.  rails_2017-05-03.log
    filename: "my_app.log" 
    formatter:
      date_pattern: '%H:%M:%S'
      pattern     : '%d %l: %m '
      type        : PatternFormatter

步骤2。修改config / application.rb

require 'rails/all'
# add these line for log4r
require 'log4r'
require 'log4r/yamlconfigurator'
require 'log4r/outputter/datefileoutputter'
include Log4r

Bundler.require(:default, Rails.env) if defined?(Bundler)
module Zurich
  class Application < Rails::Application
    #...
    # assign log4r's logger as rails' logger.
    log4r_config= YAML.load_file(File.join(File.dirname(__FILE__),"log4r.yml"))
    YamlConfigurator.decode_yaml( log4r_config['log4r_config'] )
    config.logger = Log4r::Logger[Rails.env]
  end
end

步骤3。将此行添加到您的Gemfile中。

# which is the latest version and support "datefileoutputter"
gem 'log4r', '1.1.9'  

(如果您使用的是Rails 4+,仍然有步骤4:将此文件添加到config / initializers文件夹

# config/initializers/log4r_patch_for_rails4.rb
class Log4r::Logger
  def formatter()
  end
end    

已经完成了。现在“cd”进入你的Rails应用程序文件夹,运行“bundle”来安装log4r,然后“rails s”, 你会在“/ log”文件夹中找到这样的日志文件:

May  9 17:05 rails_2011-05-09.log
May 10 13:42 rails_2011-05-10.log

并且日志内容是(我最喜欢的格式):

$ tail log/rails_2011-05-10.log
Started GET "/????_settings/19/edit" for 127.0.0.1 at ...
13:42:11 INFO:   Processing by ????SettingsController ...
13:42:11 INFO:   Parameters: {"id"=>"19"}
13:42:12 DEBUG:   ????Setting Load (0.0ms)  SELECT "d ...
13:42:12 INFO: Completed 200 OK in 750ms

我的环境:

  1. 操作系统:在XP中运行的cygwin
  2. ruby​​ 1.8.7(2011-02-18 patchlevel 334)[i386-mingw32]
  3. rails:3.0.5
  4. gem:1.6.0
  5. 有任何问题请告诉我〜:-)

    请参阅:https://stackoverflow.com/a/20154414/445908

答案 1 :(得分:4)

模仿rails日志记录行为(登录到环境依赖日志文件)我使用以下log4r.yml:

log4r_config:
  # define all loggers ...
  loggers:
  - name      : production
    level     : WARN
    trace     : 'false'
    outputters :
    - datefile_production
  - name      : development
    level     : DEBUG
    trace     : 'true'
    outputters :
    - datefile_development
  - name      : test
    level     : DEBUG
    trace     : 'true'
    outputters :
    - datefile_test

  # define all outputters (incl. formatters)
  outputters:
  - type: DateFileOutputter
    name: datefile_production
    dirname: "log"
    filename: "production.log"
    formatter:
      date_pattern: '%H:%M:%S'
      pattern     : '%d %l: %m '
      type        : PatternFormatter
  - type: DateFileOutputter
    name: datefile_development
    dirname: "log"
    filename: "development.log"
    formatter:
      date_pattern: '%H:%M:%S'
      pattern     : '%d %l: %m '
      type        : PatternFormatter
  - type: DateFileOutputter
    name: datefile_test
    dirname: "log"
    filename: "test.log"
    formatter:
      date_pattern: '%H:%M:%S'
      pattern     : '%d %l: %m '
      type        : PatternFormatter

答案 2 :(得分:1)

  

任何人都可以指向一个允许日志的工作代码段   使用YAML文件配置,并在运行时初始化?

我用一种替换标准rails logger的方式编写了detailed blogpost about how to set up log4r。此外,我详细介绍了如何使用多个记录器,使用日志级别以及如何使用Log4r记录Mongoid,ActiveRecord和Exceptions(包括堆栈跟踪)。

另外一篇名为“Logging”的更成熟的宝石在与该文章对应的HackerNews thread的评论中被提出,因此对Log4r感兴趣的人可能也想检查这个宝石。