如何使用 Rails 3.0.x 配置 Log4r?
我尝试根据这篇文章使用 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 目录中。
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
呵呵...Log4r的思想来自于著名的“Log4j”,它是我java编程生涯中最喜欢的记录器。
不过log4r的文档实在是太差了,对于新手来说真的很难。让我展示我的解决方案:
Step1。创建log4r配置文件:(文件名:config/log4r.yml
)修改 config/application.rb
步骤3。将此行添加到您的 Gemfile 中。
(如果你使用的是Rails 4+(包括Rails6),还有第4步:将此文件添加到config/initializers文件夹中
)
就完成了。现在“cd”进入 Rails 应用程序文件夹,运行“bundle”安装 log4r,然后运行“rails s”,
你会在“/log”文件夹中找到这样的日志文件:
日志内容是(我最喜欢的格式):
我的环境:
有任何问题请告诉我~ :-)
参考: 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)
step2. modify config/application.rb
step3. add this line to your Gemfile.
(if you are using Rails 4+ (including Rails6), there still is step4: add this file to config/initializers folder
)
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:
and the log content is( my favorite format ) :
My environment:
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
为了模仿 Rails 日志记录行为(登录到环境相关的日志文件),我使用以下 log4r.yml:
To mimic rails logging behavior (logging into environment dependend logfiles) I use the following log4r.yml:
我写了一篇关于如何设置 log4r 的详细博文,它取代了标准轨道记录仪。此外,我还详细介绍了如何使用多个记录器、使用日志级别以及如何使用 Log4r 记录 Mongoid、ActiveRecord 和异常(包括堆栈跟踪)。
Logging" 的评论中建议了另一个显然更成熟的 gem,名为“Logging” /news.ycombinator.com/item?id=6237419" rel="nofollow">HackerNews 主题 对应于该文章,因此对 Log4r 感兴趣的人可能也想检查一下这个 gem。
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.