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

use*_*620 16 ruby-on-rails log4r ruby-on-rails-3

我尝试根据本文使用Rails 3.0.4配置log4r: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'
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢

Siw*_*申思维 31

呵呵...... 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
Run Code Online (Sandbox Code Playgroud)

第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
Run Code Online (Sandbox Code Playgroud)

第3步.将此行添加到您的Gemfile中.

# which is the latest version and support "datefileoutputter"
gem 'log4r', '1.1.9'  
Run Code Online (Sandbox Code Playgroud)

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

# config/initializers/log4r_patch_for_rails4.rb
class Log4r::Logger
  def formatter()
  end
end    
Run Code Online (Sandbox Code Playgroud)

)

完成.现在"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
Run Code Online (Sandbox Code Playgroud)

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

$ 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
Run Code Online (Sandbox Code Playgroud)

我的环境:

  1. 操作系统:在XP中运行的cygwin
  2. ruby 1.8.7(2011-02-18 patchlevel 334)[i386-mingw32]
  3. rails:3.0.5
  4. 宝石:1.6.0

有任何问题请告诉我〜:-)

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

  • 在你的配置中,Rails没有覆盖输出器的"文件"配置值 - 参数是"filename":)它默认为启动进程的文件/命令的名称.请注意,您指定的任何自定义文件名__must__都有文件扩展名,否则将无法将日期保留在文件名中! (3认同)