16

我尝试根据这篇文章使用 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'

我在 Google 上搜索了 Rails 3 集成,但没有找到可行的解决方案。谁能给我指出一个允许使用 YAML 文件进行日志配置并在运行时初始化的工作代码片段?

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

谢谢

4

3 回答 3

31

呵呵……Log4r的想法来源于著名的“Log4j”,这是我java编程生涯中最喜欢的logger。但是log4r的文档真的很差,新手也很难。让我展示我的解决方案:

步骤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

第三步。将此行添加到您的 Gemfile。

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

(如果您使用的是 Rails 4+(包括 Rails6),还有步骤 4:将此文件添加到 config/initializers 文件夹

# config/initializers/log4r_patch_for_rails4.rb
class Log4r::Logger
  def formatter()       # for rails4+
    Proc.new{|severity, time, progname, msg|
      formatted_severity = sprintf("%-5s",severity.to_s)
      formatted_time = time.strftime("%Y-%m-%d %H:%M:%S")
      "[#{formatted_severity} #{formatted_time} #{$$}]\n #{msg}\n"
    }

  end
  def formatter= temp   # for rails6+
  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 补丁级别 334) [i386-mingw32]
  3. 导轨:3.0.5
  4. 宝石:1.6.0

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

参考:https ://stackoverflow.com/a/20154414/445908 和rails6 log4r tagged_logging.rb:22:in `call': 参数数量错误

于 2011-05-11T03:49:19.080 回答
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
于 2012-09-06T08:32:18.800 回答
1

谁能给我指出一个允许使用 YAML 文件进行日志配置并在运行时初始化的工作代码片段?

我写了一篇关于如何设置 log4r以取代标准 rails 记录器的详细博文。此外,我详细介绍了如何使用多个记录器、使用日志级别以及如何使用 Log4r 记录 Mongoid、ActiveRecord 和异常(包括堆栈跟踪)。

在与文章对应的HackerNews 线程的评论中建议了另一个明显更成熟的 gem,称为“ Logging ” ,因此对 Log4r 感兴趣的人可能也想查看这个 gem。

于 2013-10-15T02:49:04.527 回答