Set logging levels in Ruby on Rails
Ruby on-RailsRuby on-Rails-3LoggingRuby on-Rails Problem Overview
I'm using following code to configure logging in my Ruby on Rails application:
environment.rb:
Rails.logger = Logger.new(STDOUT)
class Logger
def format_message(severity, timestamp, progname, msg)
"#{timestamp.to_formatted_s(:db)} #{severity} #{msg}\n"
end
end
I'm trying to set the logging level to warn now using
config.log_level = :warn
in my production.rb, but it doens't seem to work. Am I missing something here?
If I put Rails.logger.level = 4 in my environment.rb, it does seem to work. But I would like to configure things in my environment initializers.
Ruby on-Rails Solutions
Solution 1 - Ruby on-Rails
According to the official documentation, you should be using:
config.log_level = :warn # In any environment initializer, or
Rails.logger.level = 0 # at any time
If neither of those work for you, try:
config.log_level = Logger::WARN
And if that doesn't work, try:
config.logger.level = Logger::WARN
Note: The final method appears to be conflating the two official strategies, but works in some situations
Solution 2 - Ruby on-Rails
For Rails 4.0 you can set the value in production.rb
(or your desired environment) like so:
# Set to :debug to see everything in the log.
config.log_level = :warn
Update The docs state it like so:
> ### 2.2 Log Levels
> When something is logged it's printed into the corresponding log if the log level of the message is equal or higher than the configured log level. If you want to know the current log level you can call the Rails.logger.level
method.
>
> The available log levels are: :debug
, :info
, :warn
, :error
, :fatal
, and :unknown
, corresponding to the log level numbers from 0 up to 5 respectively. To change the default log level, use
>
> config.log_level = :warn # In any environment initializer, or
> Rails.logger.level = 0 # at any time
> This is useful when you want to log under development or staging, but you don't want to flood your production log with unnecessary information.
>
> > The default Rails log level is info
in production mode and debug in development and test mode.