How do I get django to tell me where I am inside my code
Question: I am starting to us logging in my django project at the moment (on step at a
time) and I was wondering if there was a way to put where I am in my code into
the error message using python. I.e. If I am in
`something.views.something_view` how do I get this class/function location to
then tag it onto `logging.error("something went wrong in "+???)`?


Answer: You should configure the logging at application level (in settings.py) using a
dictionary, in this way:

    
    
    LOGGING = {
        'version': 1,
        'disable_existing_loggers': True,
        'formatters': {
            'standard': {
                'format': '[%(levelname)s] [%(asctime)s] [%(name)s:%(lineno)s] %(message)s',
                'datefmt': '%d/%b/%Y %H:%M:%S'
            },
        },
        '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': os.path.join(LOGS_DIR, 'application.log'),
                'maxBytes': 5242880, # 5MB
                'backupCount': 10,
                'formatter': 'standard',
            },
            'console':{
                'level':'DEBUG',
                'class':'logging.StreamHandler',
                'formatter': 'standard'
            },
        },
        'loggers': {
            'django': {
                'handlers':['console'],
                'level':'DEBUG',
                'propagate': False,
            },
            'django.db.backends': {
                'handlers': ['console'],
                'level': 'DEBUG',
                'propagate': False,
            },
            'django.request': {
                'handlers': ['mail_admins'],
                'level': 'DEBUG',
                'propagate': False,
            },
            'com.mysite': {
                'handlers': ['console', 'logfile'],
                'level': 'DEBUG',
                'propagate': True,
            },
        }
    }
    

The line:

    
    
    'format': '[%(levelname)s] [%(asctime)s] [%(name)s:%(lineno)s] %(message)s'
    

will produce a log output like:

    
    
    [DEBUG] [11/Aug/2013 12:34:43] [com.mysite.apps.myapp.middleware.MyMiddleware:28] My log message
    

where:

    
    
    com.mysite.apps.myapp.middleware.MyMiddleware
    

is the class that has logged your message and :28 the line in your code. A
logger is configured in this way at module-level:

    
    
    import logging
    
    logger = logging.getLogger(__name__)
    

in this way, your logger will automatically be resolved using the fully
qualified class name!

