Nathan English
Nathan English

Reputation: 784

Python logging not using specified handler

Using the Python logger I have two handlers, one used for root and one for everything else. When I set logging as 'main', I can see no handler is set in the logger.handler variable. It instead uses the root handler of syslog.

Below is a copy of the code and Configuration in use.

Code:

def load_config(self):
    logging.config.fileConfig('/etc/msdb/integration/logging.conf')
    log = logging.getLogger('main')
    log.debug("Hello")

Config:

[loggers]
keys=root,props,main,thread,rabbit,blockchain

[handlers]
keys=file,syslog

[formatters]
keys=simple

[logger_root]
level=DEBUG
handlers=syslog

[logger_props]
level=DEBUG
handlers=file
qualname=Properties
propagate=0

[logger_main]
level=DEBUG
handlers=file
qualname=Main
propagate=0

[logger_thread]
level=DEBUG
handlers=file
qualname=Thread
propagate=0

[logger_rabbit]
level=DEBUG
handlers=file
qualname=RabbitMQ
propagate=0

[logger_blockchain]
level=DEBUG
handlers=file
qualname=BigChainDB
propagate=0

[handler_file]
class=logging.handlers.RotatingFileHandler
level=NOTSET
formatter=simple
args=('/var/log/msdb/msdb.intergration.log','a', 100000, 1, 'utf8')

[handler_syslog]
class=StreamHandler
level=DEBUG
formatter=simple
args=(sys.stdout,)

[formatter_simple]
format=%(asctime)s - %(levelname)%s - %(threadName)%s - %(filename)s:%(lineno)d - %(message)s
datefmt=

Upvotes: 1

Views: 680

Answers (2)

Vinay Sajip
Vinay Sajip

Reputation: 99297

It's because your logger in the config is called Main (the qualname setting), but the logger you log to is called main. Logger names are case-sensitive: the names should match exactly for things to work as expected.

Upvotes: 3

Tianjin Gu
Tianjin Gu

Reputation: 784

Suppose you're call load_config in main.py, Change logger_main section of logging.conf

[logger_main]
level=DEBUG
handlers=file
qualname=__main__  ## or the __name__ attr of your .py file
propagate=0

Upvotes: 0

Related Questions