Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging in Ruby on Rails in Production Mode

I would like to view some variables in controller, it tried the following:

Rails.logger.debug "Year: #{Time.now.year}"

puts "Year: #{Time.now.year}, Month: #{@month}"

where can I see the output for Logger or Puts in production mode? Do I need so set something up to view these somewhere?

like image 482
user1135541 Avatar asked May 29 '13 21:05

user1135541


People also ask

Can you console log in rails?

One of the best tools in a rails developers arsenal is the rails console, being extremely useful for brainstorming, debugging, and testing. Having it log active record queries directly to the console can improve readability and convenience over looking through the development logs to see what SQL queries have been run.

How do I create a log in rails?

Rails makes use of the ActiveSupport::Logger class to write log information. Other loggers, such as Log4r , may also be substituted. By default, each log is created under Rails. root/log/ and the log file is named after the environment in which the application is running.

How do I check logs in Ruby on rails?

All kinds of information can be logged: With this information, you can diagnose & find the source of problems in your application faster. In a Rails app, logs are stored under the /log folder. In development mode, the development. log file is used & you see log output on the terminal you're running rails server on.


2 Answers

The normal log level in production is info, so debug logs are not shown.
Change your logging to

Rails.logger.info "Year: #{Time.now.year}" 

to show it in production.log.

Alternatively (but not a good idea) you can raise the logging level in /config/environments/production.rb:

config.log_level = :debug 

Update Rails 4.2:

Now the default debug level in all environments is :debug (as @nabilh mentioned).
If you want you production environment less chattery, you can reset your log level in /config/environments/production.rb to the former :info:

config.log_level = :info 
like image 189
Martin M Avatar answered Sep 28 '22 12:09

Martin M


While I believe @martin-m is right that you probably don't want to clutter your logs by using config.log_level = :debug in /config/environments/production.rb, I believe the default logging level in all environments is debug as of Rails 4.2. So debug logs (and all levels up) will be shown in production unless you specify otherwise.

So in response to your question, you can now write:

Rails.logger.debug "Year: #{Time.now.year}" and see the results in /log/production.log.

See here for more detailed documentation. Here is the documentation for Rails 4.1 for comparison.

like image 36
nabilh Avatar answered Sep 28 '22 10:09

nabilh