I'm new to Ruby/Rails programming and still finding my way. I have created a sample web app by following this tutorial. It's working fine. It's using Ruby version 1.9.3 with Rails version 4.0.8.
However I would like to implement logging. So I inserted the following lineq into one of my controllers:
logger.debug "Hello! I'm a DEBUG message"
logger.info "Hello! I'm an INFO message"
This produced the following logs:
Hello! I'm a DEBUG message
Hello! I'm an INFO message
That's not too helpful. I want to see more details in the log output. When was this log created? What was the log level? What is the filename and line number of this log? I want to see all of that.
So I implemented the solution described here by creating a file config/initializers/logger.rb with the following contents:
class Logger::SimpleFormatter
def call(severity, time, progname, msg)
"[#{severity} #{time} #{caller(0).first.match(/.*:\d+/)[0]}] #{msg}\n"
end
end
But it makes no difference. The log lines still come out bare, without any of the other essential information I'm looking for. How to fix this? Why didn't creating my logger.rb have any effect?
I sincerely do not understand why the logging in rails is so "bare". The default logger rails use is even better, but rails dumbs it further down.
Things I need in logging:
There are some good alternatives though, you can have a look at :
In Rails 4, the default logger for all modes (except
production
) isActiveSupport::Logger::SimpleFormatter
. See the documentation here: http://guides.rubyonrails.org/configuring.html#rails-general-configurationSo, your
logger.rb
needs to be:Please note that this will not work in production mode. For
production
you will have to customizeLogger::Formatter
if you need to.