Adding filename to Rails logger info messages

后端 未结 2 633
长情又很酷
长情又很酷 2021-01-24 21:33

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 chang

相关标签:
2条回答
  • 2021-01-24 21:39

    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+

    0 讨论(0)
  • 2021-01-24 22:02

    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
    
    0 讨论(0)
提交回复
热议问题