Created
October 20, 2011 17:14
-
-
Save TwP/1301701 to your computer and use it in GitHub Desktop.
Enter debug mode via USR1 signal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'servolux' | |
require 'logging' | |
# This is a simple demonstration server that shows how to register a signal handler | |
# for toggling the log level of your application. This is very useful in a production | |
# application where you want to enable debug messages for a brief period of time. The | |
# Servolux::Server class looks for a few special methods and registers them as signal | |
# handlers. Here, the "usr1" method will be called when the USR1 signal is sent to the | |
# process. | |
# | |
class DemoServer < Servolux::Server | |
# Configure our demo server. | |
# | |
def initialize | |
super( 'demo_server', | |
:logger => Logging.logger[self], | |
:interval => 30 # invoke the run method every 30 seconds | |
) | |
end | |
# The main run loop will be called at a regular interval. | |
# | |
def run | |
logger.debug 'Entering main run loop' | |
# do some work here ... | |
end | |
# If SIGUSR1 is sent to the process, then the logging level is | |
# toggled between the normal level and the debug level. | |
# | |
def usr1 | |
@__level ||= nil | |
r = Logging.logger.root | |
if @__level.nil? | |
r.level, @__level = :debug, r.level | |
logger.debug "Entering debug mode." | |
else | |
logger.debug "Exiting debug mode." | |
r.level, @__level = @__level, nil | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment