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

发布于 2024-10-31 23:03:54 字数 1052 浏览 5 评论 0 原文

我尝试根据这篇文章使用 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 目录中。

谢谢

I tried configuring log4r with Rails 3.0.4 based on this article: 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'

I have Googled for a Rails 3 integration, but have not found a working solution. Can anyone point me to a working code snippet that will allow log configuration using a YAML file, and initialization at runtime?

Just as a reference, I placed the sample logger.rb in the config/initializers folder and the log4r.yml in the config directory.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深陷 2024-11-07 23:03:54

呵呵...Log4r的思想来自于著名的“Log4j”,它是我java编程生涯中最喜欢的记录器。
不过log4r的文档实在是太差了,对于新手来说真的很难。让我展示我的解决方案:

Step1。创建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

)修改 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+(包括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. 操作系统: cygwin running in XP
  2. ruby​​ 1.8.7 (2011-02-18 patchlevel 334) [i386 -mingw32]
  3. Rails: 3.0.5
  4. gem: 1.6.0

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

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

Hehe ...The idea of Log4r comes from the famous "Log4j", which is my favorite logger in my java programming life.
However log4r's doc is really poor, and it's really hard for newbies. Let me show my solution:

Step1. create the log4r config file: (file name: 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

step2. modify 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

step3. add this line to your Gemfile.

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

(if you are using Rails 4+ (including Rails6), there still is step4: add this file to config/initializers folder

# 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  

)

It's done. Now "cd" into your Rails application folder, run "bundle" to install log4r, then "rails s",
you will find the log files in "/log" folder like this:

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

and the log content is( my favorite format ) :

$ 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

My environment:

  1. OS: cygwin running in XP
  2. ruby 1.8.7 (2011-02-18 patchlevel 334) [i386-mingw32]
  3. rails: 3.0.5
  4. gem: 1.6.0

Any question please let me know~ :-)

refer to: https://stackoverflow.com/a/20154414/445908
,and rails6 log4r tagged_logging.rb:22:in `call': wrong number of arguments

花期渐远 2024-11-07 23:03:54

为了模仿 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

To mimic rails logging behavior (logging into environment dependend logfiles) I use the following 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
残疾 2024-11-07 23:03:54

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

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

Logging" 的评论中建议了另一个显然更成熟的 gem,名为“Logging” /news.ycombinator.com/item?id=6237419" rel="nofollow">HackerNews 主题 对应于该文章,因此对 Log4r 感兴趣的人可能也想检查一下这个 gem。

Can anyone point me to a working code snippet that will allow log
configuration using a YAML file, and initialization at runtime?

I wrote a detailed blogpost about how to set up log4r in a way that it replaces the standard rails logger. Furthermore I went into detail about how to use multiple loggers, use log levels as well as how to log Mongoid, ActiveRecord and Exceptions(incluing Stack traces) using Log4r.

Another apparently more mature gem called "Logging" was suggested in comments on the HackerNews thread corresponding to the article, so people interested in Log4r might want to check on this gem as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文