Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python logging from multiple modules to same log file

Tags:

python

logging

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.')
like image 556
sfat Avatar asked Oct 20 '22 12:10

sfat


1 Answers

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.

like image 125
James Avatar answered Oct 23 '22 10:10

James