Python TimedRotatingFileHandler with dictConfig

4.4k views Asked by At

I am trying to implement TimedRotatingFileHandler with dictConfig

Here is the code:

LOG_SETTINGS = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'default': {
            'format': '%(asctime)s: %(name)s: #%(lineno)d: %(levelname)s - %(message)s'
        }
    },
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'formatter': 'default',
            'filename': logfile_name,
            'when': 'midnight',
            'interval': 1,
            'backupCount': 5
        }
    },
    'loggers': {
        ' ': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True
        },
    }
}

logfile_name = $HOME/.local/share/app/file.log

Loading dictionary with logging.config.dictConfig(LOG_SETTINGS)

Writing to file: logging.debug("Some text")

This creates a file named file.log, but the file is completely empty. I got no errors/warnings.

I visited Basic logging dictConfig in Python. File seems quite similar.

Where did I go wrong? Using Python 3.6

1

There are 1 answers

4
Jefferson Houp On BEST ANSWER

Try that:

import logging  # there we import logging module
logger = logging.getLogger(' ')  # there we get logger by name from settings
logger.debug('yolo')  # there we test logging with custom message

UPDATE: In my company we use that always:

logger = logging.getLogger(__name__)

with define logger:

'loggers': {
    'app.views': {
        'handlers': ['handler_name'],
        'level': os.getenv('ERROR', 'INFO'), # needed level
        'propagate': True,
    },