Adding filename to Rails logger info messages

2019-02-27 23:51发布

How to add the file name and line number in Rails' log message? My current format is something like

[INFO : 12-09-27 10:12:30]

I want to change it to

[INFO: 12-09-27 10:12:30 application_controller.rb:35]

or something like this. Any ideas?

2条回答
祖国的老花朵
2楼-- · 2019-02-28 00:33

In case any one is looking for a similar solution for Rails 4. It would be:

class ActiveSupport::Logger::SimpleFormatter
  def call(severity, time, progname, msg)
    "[#{severity} #{time} #{caller(0).first.match(/.*:\d+/)[0]}] #{msg}\n"
  end
end
查看更多
等我变得足够好
3楼-- · 2019-02-28 00:41

Create an initializer logger.rb in your config/initializers directory and try putting this

class Logger::SimpleFormatter
  def call(severity, time, progname, msg)
    "[#{severity} #{time} #{caller(0).first.match(/.*:\d+/)[0]}] #{msg}\n"
  end
end

Should work on Ruby 1.9+

查看更多
登录 后发表回答