Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.2.x: How to change logging levels without restarting the application

I would like to change the logging levels of a running Rails 3.2.x application without restarting the application. My intent is to use it to do short-time debugging and information gathering before reverting it to the usual logging level.

I also understand that the levels in ascending order are debug, info, warn, error, and fatal, and that production servers log info and higher, while development logs debug and higher.

I understand that if I run

Rails.logger.level=:debug #or :info, :warn, :error, :fatal

Will this change the logging level immediately?

If so, can I do this by writing a Rake task to adjust the logging level, or do I need to support this by adding a route? For example in config/routes.rb:

match "/set_logging_level/:level/:secret" => "logcontroller#setlevel" 

and then setting the levels in the logcontroller. (:level is the logging level, and :secret which is shared between client and server, is something to prevent random users from tweaking the log levels)

Which is more appropriate, rake task or /set_logging_level?

like image 577
Jay Godse Avatar asked Jan 29 '13 19:01

Jay Godse


People also ask

How do you change the logging level?

To change log levels as a root user, perform the following: To enable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=DEBUG) To disable debug logging, run the following command: /subsystem=logging/root-logger=ROOT:change-root-log-level(level=INFO)

How do I change the log level in console?

The console opens in a new browser window. Click Troubleshooting > Logs and Trace. In the Logging and Tracing page, click the name of the Dashboard Application Services Hub (for example, server1). Click Change Log Detail Levels.

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.


2 Answers

In Rails console, you can simply do:

Rails.logger.level = :debug

Now all executed code will run with this log level

like image 196
axsuul Avatar answered Feb 16 '23 01:02

axsuul


Why don't you use operating system signals for that? For example on UNIX user1 and user2 signals are free to use for your application:

config/initializers/signals.rb:

trap('USR1') do
  Rails.logger.level = Logger::DEBUG
end

trap('USR2') do
  Rails.logger.level = Logger::WARN
end

Then just do this:

kill -SIGUSR1 pid
kill -SIGUSR2 pid

Just make sure you dont override signals of your server - each server leverages various signals for things like log rotation, child process killing and terminating and so on.

like image 43
lzap Avatar answered Feb 16 '23 00:02

lzap