0

I'm having a problem, what on Earth just happened after I added a formatter to the LOGGING configuration? The logging file is empty.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '{asctime} [{module}]:: {message}',
            'style': '{',
        },
        'simple': {
            'format': '{levelname} {message}',
            'style': '{',
        },
    },
    'handlers': {
        'console':{
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        },
        'file.DEBUG': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': 'logs/debug.log',
            'maxBytes' : 1024*1024*10,
            'backupCount': 10,
            'formatter':'verbose'
        },
        'file.INFO': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': 'logs/info.log',
            'maxBytes' : 1024*1024*10,
            'formatter':'verbose'
        },
        'file.ERROR': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': 'logs/error.log',
            'maxBytes' : 1024*1024*10,
            'backupCount': 10,
            'formatter':'verbose'
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['file.DEBUG'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'django': {
            'handlers': ['file.INFO'],
            'level': 'INFO',
            'propagate': True,
        },
        'django': {
            'handlers': ['file.ERROR'],
            'level': 'ERROR',
            'propagate': True,
        },
    },
}

Now all the outputed debug files are empty except for the error file which I tested on the view with this code

import logging
logger = logging.getLogger('django')

logger.info('tests')
logger.error('tests')
logger.debug('tests')
logger.debug('tests')
logger.error('tests')
logger.info('tests')

Only the error is being logged on the file, why is this happening? I'm using the new version of django which is 2.2

Update Don't know what just I did but its working now This is my new LOGGING setting

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
            'verbose': {
                'format': '{levelname} {asctime} [{module}]:: {message}',
                'style': '{',
            },
            'simple': {
                'format': '{levelname} {message}',
                'style': '{',
            },
        },
    'handlers': {
        'file_debug': {
            'level': 'DEBUG', 
            'class': 'logging.FileHandler',
            'filename': 'logs/debug.log',
            'formatter':'verbose'
        },
        'file_error': {
            'level': 'ERROR',
            'class': 'logging.FileHandler',
            'filename': 'logs/error.log',
            'formatter':'verbose'
        },
        'file_info': {
            'level': 'INFO',
            'class': 'logging.FileHandler',
            'filename': 'logs/info.log',
            'formatter':'verbose'
        },
    },
    'loggers': {
        'django.server': {
            'handlers': ['file_debug'], 
            'level': 'DEBUG',
            'propagate': True, 
        },
        'django': {
            'handlers': ['file_error'], 
            'level': 'ERROR',
            'propagate': True, 
        },
        'django': {
            'handlers': ['file_info'], 
            'level': 'INFO',
            'propagate': True, 
        },
    },
}

1 Answer 1

2

Because you did not configure the LOGGING. In your settings you have two entries for django, and django is writing logs based on the last entry. Instead of that, you should have put the configuration like this:

'loggers': {
        'django.request': {
            'handlers': ['file.DEBUG'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'django': {
            'handlers': ['file.INFO', 'file.ERROR'],  # <-- Here
            'level': 'INFO',
            'propagate': True,
        }
}

Please see the last example of the examples section of documentation.

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

1 Comment

It was working before I added formatter, also I want every log levels has its own file. But thanks to the info, about django.request :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.