Ticket #18993: 18993-1.diff

File 18993-1.diff, 3.8 KB (added by Claude Paroz, 12 years ago)

Output messages from django logger when DEBUG is True

  • django/utils/log.py

    diff --git a/django/utils/log.py b/django/utils/log.py
    index df2089f..4a63e40 100644
    a b  
    11import logging
     2import sys
    23import traceback
    34
    45from django.conf import settings
    except ImportError:  
    2324
    2425getLogger = logging.getLogger
    2526
    26 # Ensure the creation of the Django logger
    27 # with a null handler. This ensures we don't get any
     27
     28class RequireDebugFalse(logging.Filter):
     29    def filter(self, record):
     30        return not settings.DEBUG
     31
     32class RequireDebugTrue(logging.Filter):
     33    def filter(self, record):
     34        return settings.DEBUG
     35
     36
     37# Ensure the creation of the Django logger with at least one handler.
     38# This ensures we don't get any
    2839# 'No handlers could be found for logger "django"' messages
    2940logger = getLogger('django')
    3041if not logger.handlers:
    31     logger.addHandler(NullHandler())
     42    stream_h = logging.StreamHandler()
     43    stream_h.addFilter(RequireDebugTrue())
     44    logger.addHandler(stream_h)
    3245
    3346
    3447class AdminEmailHandler(logging.Handler):
    class CallbackFilter(logging.Filter):  
    98111        if self.callback(record):
    99112            return 1
    100113        return 0
    101 
    102 
    103 class RequireDebugFalse(logging.Filter):
    104     def filter(self, record):
    105         return not settings.DEBUG
  • docs/topics/logging.txt

    diff --git a/docs/topics/logging.txt b/docs/topics/logging.txt
    index 28baf87..20fe94f 100644
    a b Django provides three built-in loggers.  
    424424``django`` is the catch-all logger. No messages are posted directly to
    425425this logger.
    426426
     427.. versionchanged:: 1.5
     428
     429    Previously, all messages reaching this logger were discarded unless the
     430    logger had been customized. In Django 1.5, and when :setting:`DEBUG` is
     431    ``True``, all messages are printed to ``stderr`` by default.
     432
    427433``django.request``
    428434~~~~~~~~~~~~~~~~~~
    429435
    logging module.  
    567573                'class': 'django.utils.log.AdminEmailHandler'
    568574            }
    569575        },
     576
     577.. class:: RequireDebugTrue()
     578
     579   .. versionadded:: 1.5
     580
     581   This filter will only pass on records when settings.DEBUG is True.
  • tests/regressiontests/logging_tests/tests.py

    diff --git a/tests/regressiontests/logging_tests/tests.py b/tests/regressiontests/logging_tests/tests.py
    index 4ae3c1b..7ba5126 100644
    a b  
    11from __future__ import unicode_literals
    22
    33import copy
     4import logging
     5import sys
    46import warnings
    57
    68from django.conf import compat_patch_logging_config
    from django.core import mail  
    810from django.test import TestCase, RequestFactory
    911from django.test.utils import override_settings
    1012from django.utils.log import CallbackFilter, RequireDebugFalse, getLogger
     13from django.utils.six import StringIO
    1114
    1215
    1316# logging config prior to using filter with mail_admins
    class PatchLoggingConfigTest(TestCase):  
    106109        self.assertEqual(config, new_config)
    107110
    108111
     112class DefaultLoggingTest(TestCase):
     113    def setUp(self):
     114        self.logger = logging.getLogger('django')
     115        self.old_stream = self.logger.handlers[0].stream
     116
     117    def tearDown(self):
     118        self.logger.handlers[0].stream = self.old_stream
     119
     120    def test_django_logger(self):
     121        """
     122        The 'django' base logger only output anything when DEBUG=True.
     123        """
     124        output = StringIO()
     125        self.logger.handlers[0].stream = output
     126        self.logger.error("Hey, this is an error.")
     127        self.assertEqual(output.getvalue(), '')
     128
     129        with self.settings(DEBUG=True):
     130            self.logger.error("Hey, this is an error.")
     131            self.assertEqual(output.getvalue(), 'Hey, this is an error.\n')
     132
     133
    109134class CallbackFilterTest(TestCase):
    110135    def test_sense(self):
    111136        f_false = CallbackFilter(lambda r: False)
Back to Top