Skip to content Skip to sidebar Skip to footer

Python 2.4.3: Configparser.nosectionerror: No Section: 'formatters'

Trying to use a logging configuration file to implement TimedRotatinigFileHandler. Just won't take the config file for some reason. Any suggestions appreciated. x.py: import loggi

Solution 1:

The error message is strictly accurate but misleading.

The reason the "formatters" section is missing, is because the logging module can't find the file you passed to logging.config.fileConfig.

Try using an absolute file path.

Solution 2:

Yes, @ekhumoro was right. It seems that logging expects an absolute path. Python team should change this error message to something more readable; it just cannot see my file, not because the config file itself is wrong.

I managed to solve this by defining a BASE_DIR variable in a config file and import it as the prefix of the path, just as Django does. And, remember to create the log file's parent dir(s) if they are not created.

I define the path and the logger in config.py:

import os
BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

import logging
import logging.config
logging.config.fileConfig(os.path.join(BASE_DIR, 'utils', 'logger.conf')) # the `logger.conf` locates under 'myproject/utils/'
logger = logging.getLogger("mylog") # 'mylog' should exist in `logger.conf` in the logger part

In other modules:

from config import logger
...

logger.info("Your loggings modules should work now!! - WesternGun")

Post a Comment for "Python 2.4.3: Configparser.nosectionerror: No Section: 'formatters'"