Django logging does not work properly and logs are not saved to dedicated files

Question:

I’m trying to do logging in Django.

I want the all_logs handler to write all logs to a common file.
The error_file handler writes logs to the error file, warning_file to the warning file, and info_file to the informative message file.

But as a result I see errors in the error_file and I see everything in the info_file: info, warnings and errors. And there is no warning_file at all.

Only the handler for recording all_logs works well.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '{asctime} | {threadName}({thread}) [{levelname}] {message} ({name} {module}:{lineno})',
            'style': '{',
        },
        'simple': {
            'format': '{asctime} [{levelname}] {message} ({name}:{lineno})',
            'datefmt': '%H:%M:%S',
            'style': '{',
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
        },

        'all_logs': {
            'class': 'logging.handlers.WatchedFileHandler',
            'filename': str(BASE_DIR.joinpath('log', 'all.log')),
            'formatter': 'verbose',
        },

        'error_file': {
            'level': 'ERROR',
            'class': 'logging.FileHandler',
            'filename': str(BASE_DIR.joinpath('log', 'error.log')),
            'formatter': 'verbose',
        },

        'warning_file': {
            'level': 'WARNING',
            'class': 'logging.FileHandler',
            'filename': str(BASE_DIR.joinpath('log', 'warning.log')),
            'formatter': 'verbose',
        },

        'info_file': {
            'level': 'INFO',
            'class': 'logging.FileHandler',
            'filename': str(BASE_DIR.joinpath('log', 'info.log')),
            'formatter': 'verbose',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['all_logs', 'console', 'error_file', 'warning_file', 'info_file'],
            'propagate': False,
            'formatter': 'verbose'
        },
        # my app
        'applications': {
            'handlers': ['all_logs', 'console', 'error_file', 'warning_file', 'info_file'],
            'propagate': False,
            'formatter': 'verbose'
        },
    },
}

So, I am expecting that every log level will be written to dedicated file. I also would like to know how can I restrict log file size and divide logs in difirent files if log file is too big.

Asked By: Ruslan

||

Answers:

But as a result I see errors in the error_file and I see everything in the info_file: info, warnings and errors.

This is caused by how level works in Django. Here’s what the documentation says:

When a message is given to the logger, the log level of the message is compared to the log level of the logger. If the log level of the message meets or exceeds the log level of the logger itself, the message will undergo further processing. If it doesn’t, the message will be ignored.

In other words, an INFO level log contains INFO messages, and anything more serious than that.

And there is no warning_file at all.

Look at the file associated with that one:

'filename': str(BASE_DIR.joinpath('log', 'error.log')),

It’s also the error log.

Answered By: Nick ODell
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.