Question

Questions about the message

No handlers could be found for logger "X"

from Python's logging module seem to be quite common here on SO, but I have yet to find one that addresses my case.

My application only has this issue when it is run as a daemon, so I assume I am not setting something up properly there. I am using the python-daemon package in Python 2.7.

My __init__.py file initializes the logger with the following function:

def init_logger(logger_name, log_file):
    '''
    Returns: (Logger, [LoggerFH])
    '''
    logger = logging.getLogger(logger_name)

    ch = logging.StreamHandler()


    ch.setLevel(level=logging.DEBUG)
    fh = logging.FileHandler(log_file)
    fh.setLevel(level=logging.DEBUG)
    formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
    fh.setFormatter(formatter)

    logger.addHandler(fh)
    return logger, [fh, ch]

The function is called in the following manner:

logger, logger_fhs = init_logger('logger_name', 'logger_file_path')

Then the daemon is initialized like this:

context = daemon.DaemonContext(
              files_preserve=map(operator.attrgetter('stream'), logger_fhs)
          )
with context:
    bombs_away(args) # This application does not actually launch any bombs :)

What am I missing? What else can I check for to find the source of the message?

Était-ce utile?

La solution

One of my modules tried to call the logger and log a message during import time which would be before init_logger was called.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top