Home > database >  Django logger not logging error in log file
Django logger not logging error in log file

Time:12-19

Hi all I am facing issues with logging in django. It is not logging error in my django.log file. Following is the code I am using in views.py :-

import logging
logger = logging.getLogger(__name__)

class SomeView(CreateAPIView):
    serializer_class = some_class
    queryset = models.model_name.objects

    def create(self, request, *args, **kwargs):
       try:
          some_code
          try:
            some_more_code

          except Exception as e:
            logger.error(e, exc_info=True)
            print(e)
       except:
          pass
       return 1

This works and inner block print error. Then in settings.py

import logging

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
    },
    'handlers': {
        'file': {
            'level': 'ERROR',
            'class': 'logging.FileHandler',
            'filename': 'django.log',
            'formatter': 'verbose'
        },
        'console': {
            'level': 'ERROR',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file', 'console'],
            'level': 'ERROR',
            'propagate': True,
        },
    },
}

this creates a django.log file but I dont see errors in django.log files when i trigger error. Can anyone please guide me. I am using pycharm which shows log format not recognized i f that is of any help. I am however able to log when I change level to debug

CodePudding user response:

There are a few things you can try to troubleshoot this issue:

Make sure that the django.log file has the correct permissions set so that it can be written to. You can check the permissions of the file by running the ls -l command in the terminal.

Make sure that the loggers section of your LOGGING setting is correctly configured. The 'django' logger should have the 'handlers' set to ['file', 'console'] and the 'level' set to 'ERROR'.

Make sure that you are using the logger object correctly in your view. You should call the error method on the logger object and pass the error message as an argument, like this: logger.error(e). You should also remove the exc_info=True argument, as this is not needed for logging errors.

If you are still not seeing the error messages in the django.log file, you can try setting the 'propagate' setting to False in the 'django' logger. This will prevent the error messages from being propagated to the parent logger, which may be causing the messages to be filtered out.

If you are using PyCharm and see a "log format not recognized" error, this may be due to a problem with the formatting of the log messages in your LOGGING setting. Make sure that the 'format' in the 'verbose' formatter is correctly formatted according to the Python logging documentation.

I hope these suggestions help you resolve the issue.

CodePudding user response:

Hi all I believe I have figured out the answer. the issue was due to logger defined as logger = logging.getLogger(name) . when I replaced it with logger = logging.getLogger('django') it started logging correctly

  • Related