1

The django settings.py has:

LOG_LEVEL = 'DEBUG'
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'logfile': {
            'level': LOG_LEVEL,
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': '/tmp/django.log',
            'maxBytes': 1000000,
            'backupCount': 10,
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['logfile'],
            'level': LOG_LEVEL,
            'propagate': True,
        },
    }
}

and views.py has:

import logging
def some_view(request):
    logging.error('something')

I've also tried the following straight out of the official django 1.8 documentation:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/path/to/django/debug.log',
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

I'm not getting this message in the log. However, if something throws and exception, that does show up in the django log file. Also, if I set DEBUG=False in the settings and use a print statement instead of the logger, then the message shows up in the console. At the bottom of the documentation, it says when DEBUG=True, all log levels should propagate, but they are not. Any ideas how to make the logger messages appear in the log file at least with DEBUG=True?

max
  • 9,708
  • 15
  • 89
  • 144

2 Answers2

1
import logging
def some_view(request):
    logging.error('something')

The problem is when you call the logger on the views. You should call the logger you created on the settings. Like this:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'logfile': {
            'level': LOG_LEVEL,
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': '/tmp/django.log',
            'maxBytes': 1000000,
            'backupCount': 10,
        },
    },
    'loggers': {
        'django': {
            'handlers': ['logfile'],
            'level': LOG_LEVEL,
            'propagate': True,
        },
    }
}

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

def some_view(request):

    logger.error('something')
-2

Django Logging

please go with following steps. add the configs on django project root settings.py file.

LOGGING = {
    'handlers': {
        'default': {
            #'class': 'logging.handlers.TimedRotatingFileHandler',
            'class': 'cloghandler.ConcurrentRotatingFileHandler',
            'formatter': 'standard',
            #'level': 'DEBUG',
            #'filters': [],
            #'class': 'django.utils.log.AdminEmailHandler'
            #'filename': 'project/project/error.log',
            'filename' : 'None',
            'maxBytes': 1024*1024*10,
            'backupCount': 1000,
            #'when': 'midnight',
            #'interval': 1,
            #'utc': True,
        },
    },
    'loggers': {
        '': {
            'handlers': ['default'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

In Views.py file

import logging
log = logging.getLogger(__name__)
def test_function(request):
    log.debug("Im Here to help you", exc_info=True)
Cadmus
  • 665
  • 7
  • 13