Ruby On Rails 日志文件名和行号

Ruby On Rails log file name and line number

现在,我在 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

有什么解决办法吗?

它没有显示在 documentation but from looking at the code 你可以看到你应该用 "T" 替换 "t" 以仅获取文件名。

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