6

I have deployed my django site on Apache and there's a problem which did not occur on my development machine, so I want to print some variables out to see what is happening.

I tried to use python logging module, where I did this:

import os, logging
FILE = os.getcwd()
logging.basicConfig(filename=os.path.join(FILE,'log.txt'),level=logging.DEBUG)  
logging.debug('Write')

On my development machine there could be a log.txt showing up in the root directory in my django project. However when I did the same thing, there is not log.txt file showing up in the corresponding directory on my server.

Does anyone know how to debug it? Thanks!

Robert
  • 2,189
  • 6
  • 31
  • 38

3 Answers3

1

You're assuming the current working directory is the Django project, but that's unlikely to be the case unless you specifically changed it in the wsgi file.

However, you should not be logging to a separate file. The default logging setup will log either to Apache's own log in /var/log or in a site-specific one in a subdirectory of that. Remove your extra configuration and let Django log there.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

Well, here's a small config I'm using for logging. You should also check your filepermissions - is www-user allowed to write into the directory?!

settins.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format': "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            'datefmt': "%d/%b/%Y %H:%M:%S"
        },
    },
    'handlers': {
        'null': {
            'level': 'DEBUG',
            'class': 'django.utils.log.NullHandler',
        },
        'logfile': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': BASE_DIR + "/logfile",
            'maxBytes': 50000,
            'backupCount': 2,
            'formatter': 'standard',
        },
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'standard'
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'propagate': True,
            'level': 'WARN',
        },
        'django.db.backends': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': False,
        },
        '': {
            'handlers': ['console', 'logfile'],
            'level': 'DEBUG',
        },
    }
}

something.py

import logging
try:  # Python 2.7+
    from logging import NullHandler
except ImportError:
    class NullHandler(logging.Handler):
        def emit(self, record):
            pass

log = logging.getLogger(__name__)
log.addHandler(NullHandler())

def demo(foo):
    if foo not bar:
        log.debug('doh!')
    return
Thomas Schwärzl
  • 9,518
  • 6
  • 43
  • 69
-3

You need a full stacktrace to debug an unknown exception for web application. Usually you also want to check the context variables of each stack to find the root cause. Logs may help you stat exceptions, but it is so inefficient for debugging on production servers.

Sentry provides a really nice UI for tracking and debugging python error. It's open sourced. It's one of the must have django packages in jacobian's (Django Co-Creator) last year Pycon talk. Use it.

sentry

sentry st

Leonardo.Z
  • 9,425
  • 3
  • 35
  • 38