I finally figured out that the best solution, that will affect also the log format that other gems that my application is using is the following:
- Open the class
Logger::SimpleFormatter
and override the methodcall
. You can do that in an initializing file (config/initializers
). - Change the method
call
to log the file and line from thecaller[5]
.
Here is a sample implementation that worked for me:
class Logger::SimpleFormatter
def call(severity, time, progname, msg)
call_details = Kernel.caller[5].gsub(/#{Rails.root}/, '')
call_details.match /(.+):(.+):/
filename = $1
line = $2
length = 40
filename = "#{filename[-length, filename.length]}" if filename.length >= length
filename = filename.rjust(length + 2, '.')
"[#{severity} #{time} #{filename}:#{line}] #{msg}\n"
end
end
In my solution above, I have also limited the filename to the last 40 characters so that I avoid printing big file paths.