5

I am working on a Python library with multiple packages and a main routine outside these packages. I have set up a logger to log to the standard output and a log file using a configuration file as suggested in the question Using Python logging in multiple modules. When I put the main routine (main.py) into a package (mypackage), everything works as intended. However, if I start the main routine in the project's root folder, the code breaks (ConfigParser.NoSectionError: No section: 'formatters').

Is there a suggested solution for this problem?

Here is a minimal working example:

. ├── logging.conf ├── main.py ├── mypackage │   ├── __init__.py │   └── mymodule.py └── smodels.log 

logging.conf:

[loggers] keys=root [handlers] keys=consoleHandler,fileHandler [formatters] keys=consoleFormatter,fileFormatter [logger_root] level=DEBUG handlers=consoleHandler,fileHandler [handler_consoleHandler] class=StreamHandler level=DEBUG formatter=consoleFormatter args=(sys.stdout,) [handler_fileHandler] class=FileHandler level=INFO formatter=fileFormatter args=('smodels.log',) [formatter_consoleFormatter] format=%(asctime)-8s.%(msecs)03d %(levelname)-8s %(name)s:%(lineno)-3s %(message)s datefmt=%H:%M:%S [formatter_fileFormatter] format=%(asctime)-16s %(levelname)-8s %(filename)-s:%(lineno)-3s %(message)s datefmt=%Y-%m-%d %H:%M 

main.py:

#!/usr/bin/env python from mypackage import mymodule import logging logger = logging.getLogger(__name__) def main(): logger.debug('This is a debug message.') logger.info('This is an info message.') mymodule.test() if __name__=='__main__': main() 

__init__.py:

import mymodule import logging.config logging.config.fileConfig('../logging.conf',disable_existing_loggers=False) logger = logging.getLogger(__name__) 

mymodule.py:

import logging logger = logging.getLogger(__name__) def test(): logger.debug('Module debug.') logger.info('Module info.') 

1 Answer 1

4

The error says that logging.conf is not able to read:

Please update your _init_.py as below:

 import os import logging.config # logging.config.fileConfig('../logging.conf', disable_existing_loggers=False) basepath = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) logging.config.fileConfig('%s/logging.conf' % basepath) logger = logging.getLogger(__name__) 

And please let me know if that fix your problem.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, your solution works and I can now see my mistake. However, the disable_existing_loggers=False option is still needed for logging.config.fileConfig() when using multiple packages, each package containing this __init__.py.
ah please used that, forgot to add it back.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.