The intention of following code is to dynamically change the logging level of a running python process that has imported this module.

import logging
import time
import signal

logger = logging.getLogger(__name__)
# create logger object with 'ERROR' level logging.

def handler1(signum, frame):

def handler2(signum, frame):

signal.signal(signal.SIGUSR1, handler1)
# if process receives SIGUSR1 signal, the logging level changes to DEBUG.

signal.signal(signal.SIGUSR2, handler2)
# if process receives SIGUSR1 signal, the logging level changes to INFO.

while True:
    logger.info('info logging')
    logger.debug('debug logging')
    logger.error('error logging')
    logger.warn('warn logging')

The above script runs as expected but I am not sure if this the right way to full fill the goal. Please review the code, as well as issues in above resolution.

  • 2
    \$\begingroup\$ There are problems with the indentation of the code, please fix them. Is there any particular reason why you want to use signals for this? \$\endgroup\$
    – Daniel
    Jun 9, 2018 at 18:04
  • \$\begingroup\$ there is no particular reason of using the signal. \$\endgroup\$ Jun 10, 2018 at 4:15

1 Answer 1


Logger.setLevel() is the right way to dynamically change the logging level of a Logger object. Your script will work fine.

I'll nitpick:

  • Code is read from top to bottom. You should place the comments above the group of statements they are attached to, not below them. If the reader reads the comment first, they won't have to spend extra effort to decipher the meaning of the following lines, because they have read the annotation first.

  • Be wary of using asynchronous signal handlers when you are using the logging module. I know that this is a tiny example script, but always refer to the documentation when you are using the Python standard library.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.