6

By reading the official django documentation I haven't understood much about it. https://docs.djangoproject.com/en/dev/topics/logging/#configuring-logging

I would like to enable logging also if DEBUG in settings.py is set to True. I would like the errors to be logged in a file.

How to do that?

These are the default django settings for logging which I currently have now:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

PS: I'm using Apache + mod_wsgi in my development environment because I use a development machine which i access remotely on my LAN, this means i'm not using the django development server and I can't see the console log messages.

2 Answers 2

4

Django logging is not disabled by default in either DEBUG mode unless you have set it to.

Add below to your handlers part of LOGGING

'file':
        {
            'level':
                'INFO',
            'class':
                'logging.FileHandler',
            'formatter':
                'verbose',
            'filename':
                'myapp.log'

        }

It will log to myapp.log file in your project root. You can specify a complete path.

And add a formatters field to the Logging dict

'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format':
                '%(levelname)s %(message)s'
        },
    },
Sign up to request clarification or add additional context in comments.

Comments

1

This is an example of working settings inspired by Simple Log to File example for django 1.3+

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'logfile': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': SITE_ROOT + "/debug.log",
            'maxBytes': 50000,
            'backupCount': 2,
            'formatter': 'custom',
        },
    },
    'loggers': {
        #'django': {
        #    'handlers':['logfile'],
        #    'level':'DEBUG',
        #    'propagate': True,
        #},
        #'django.request': {
        #    'handlers': ['mail_admins', 'logfile'],
        #    'level': 'DEBUG',
        #    'propagate': True,
        #},
        'nodeshot.core.mailing': {
            'handlers': ['logfile'],
            'level': 'DEBUG',
        },
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
        'custom': {
            'format': '%(levelname)s %(asctime)s\n%(message)s'
        },
    },
}

Comments

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.