【问题标题】:Ruby On Rails log file name and line numberRuby On Rails 日志文件名和行号
【发布时间】:2016-03-29 17:53:13
【问题描述】:

现在,我在 Rails 中使用 log4r 作为记录器来打印文件名和行号:

======== config/log4r.yml ========

log4r_config:
  # define all loggers:
  loggers:
    - name          : rails
      level         : DEBUG
      trace         : 'true'
      outputters    : 
      - console

  # define all outputters (incl. formatters)
  outputters:
  - type: StdoutOutputter
    name: console
    formatter:
      date_pattern: '%Y-%m-%d %H:%M:%S'
      pattern     : '%d %l %t : %m'
      type        : PatternFormatter

但是它打印的是文件路径而不是文件名,而且它太长了:

2015-12-23 18:05:37 INFO /Users/fudy/Workspace/RubyWorkspace/HelloWorld/app/controllers/welcome_controller.rb:3: in `index' : hello

但我想要这种格式:

2015-12-23 18:05:37 INFO welcome_controller.rb:3: hello

有什么解决办法吗?

【问题讨论】:

  • 您确定这是您使用的模式吗?它看起来更像 '%d %p %t : %m' 对吗?
  • 检查,这是我使用的模式
  • 抱歉,我以为您使用的是 log4j 而不是 log4r。看我的回答

标签: ruby-on-rails ruby logging log4r


【解决方案1】:

它没有显示在 documentation 中,但通过查看 code 您可以看到应该将“t”替换为“T”以仅获取文件名。

另一种选择是实现自定义格式,如 here 解释的那样

【讨论】: