0
votes

I have read from python docs https://docs.python.org/3/howto/logging.html#advanced-logging-tutorial that the best way to configure module level logging is to name the logger:

logger = logging.getLogger(__name__)  

I the main application logging works fine:

if __name__ == '__main__':  
    logging.config.fileConfig('logging.conf')  
    # create logger  
    logger = logging.getLogger(__name__)  

However in another module when I set the logger in the module scope:

logger = logging.getLogger(__name__)  

the logger does not log anything. When I create the logger within a method logging works fine:

class TestDialog(QDialog, Ui_TestDialog):  
def __init__(self, fileName, parent=None):  
    super(TestDialog, self).__init__(parent)  
    self.logger = logging.getLogger(__name__)  
    logger.debug("--_init_() "+str(fileName))  

Then I would need to use the self.logger.. formatting to get a logger in all methods in the class - which I have never seen before. I tried to set the logging.conf to log where the call is comming from:

[loggers]
keys=root,fileLogger
...
[formatter_consoleFormatter]
format=%(asctime)s - %(module)s - %(lineno)d - %(name)s - %(levelname)s - %(message)s
datefmt=

However when the logger is set in the module scope logging still doesn't work even with this configuration.

I also tried:

logger = logging.getLogger('root')

at the start of a module, again no logger. However If I use:

logger = logging.getLogger('fileLogger')

at the start of a module, logging works fine and with config file above I can see which module the call is comming from.

Why is configuring logger using name not inheriting it's config from root? Why does configuring using root not work while using fileHandler does, when both root and fileHandler are configured in the logging.conf file?

1
side note: In order to get the "root" logger, do just logger = logging.getLogger().Lohmar ASHAR

1 Answers

2
votes

To avoid surprises, use disable_existing_loggers=False in your fileConfig() call, as documented in this section of the docs.

You should never need to use a self.logger instance variable - module level loggers should be fine.