Logging in Ruby on Rails in Production Mode
Asked Answered
N

3

52

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?

Notability answered 29/5, 2013 at 21:41 Comment(0)
M
127

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
Mantelletta answered 30/5, 2013 at 9:30 Comment(1)
Keep in mind that the lower the log level, the more resources used in production as mentioned in docsBernita
W
9

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.

Wholly answered 28/10, 2015 at 19:1 Comment(0)
B
1

If you have a production app that you may need to temporarily change log levels, you might want to use an environment variable to be able to set level without modifying code and redeployment, though your app would need to be restarted, you could take this approach:

config/environment/production.rb

MyApp::Application.configure do
#...
  log_level = :info #default state
  [:debug, :info, :warn, :error, :fatal, :unknown].each do |level|
    if ENV['LOG_LEVEL']&.to_sym == level
      log_level = level
    end
  end
 config.log_level = log_level
#...
end
Bernita answered 12/10, 2021 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.