diff --git a/debug_toolbar/panels/logging.py b/debug_toolbar/panels/logging.py index 1ee19cefe..c5b4165da 100644 --- a/debug_toolbar/panels/logging.py +++ b/debug_toolbar/panels/logging.py @@ -6,6 +6,7 @@ import threading except ImportError: threading = None +from django.conf import settings from django.utils.translation import ungettext, ugettext_lazy as _ from debug_toolbar.panels import Panel from debug_toolbar.utils import ThreadCollector @@ -15,6 +16,10 @@ class LogCollector(ThreadCollector): + def __init__(self): + ThreadCollector.__init__(self) + self.enabled = False + def collect(self, item, thread=None): # Avoid logging SQL queries since they are already in the SQL panel # TODO: Make this check whether SQL panel is enabled @@ -22,6 +27,22 @@ def collect(self, item, thread=None): return super(LogCollector, self).collect(item, thread) + def enable_logging(self): + """ + Enable logging if it has not already been enabled and DEBUG is True. + """ + if self.enabled: + return + + # Check to make sure DEBUG is enabled to prevent silent memory leaks. + if settings.DEBUG: + # We don't use enable/disable_instrumentation because logging is global. + # We can't add thread-local logging handlers. Hopefully logging is cheap. + logging_handler = ThreadTrackingHandler(collector) + logging.root.setLevel(logging.NOTSET) + logging.root.addHandler(logging_handler) + self.enabled = True + class ThreadTrackingHandler(logging.Handler): def __init__(self, collector): @@ -45,13 +66,8 @@ def emit(self, record): self.collector.collect(record) -# We don't use enable/disable_instrumentation because logging is global. -# We can't add thread-local logging handlers. Hopefully logging is cheap. - collector = LogCollector() -logging_handler = ThreadTrackingHandler(collector) -logging.root.setLevel(logging.NOTSET) -logging.root.addHandler(logging_handler) +collector.enable_logging() class LoggingPanel(Panel): diff --git a/docs/changes.rst b/docs/changes.rst index 2f9755687..e0463ea40 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -11,6 +11,7 @@ New features * The SQL panel colors queries depending on the stack level. * The Profiler panel allows configuring the maximum depth. +* The Logging panel will not attempt to collect data when DEBUG is False. Bugfixes ~~~~~~~~ diff --git a/tests/panels/test_logging.py b/tests/panels/test_logging.py index 288efadf1..1ddc0759d 100644 --- a/tests/panels/test_logging.py +++ b/tests/panels/test_logging.py @@ -4,16 +4,21 @@ from debug_toolbar.panels.logging import ( collector, MESSAGE_IF_STRING_REPRESENTATION_INVALID) +from django.test.utils import override_settings from ..base import BaseTestCase +@override_settings(DEBUG=True) class LoggingPanelTestCase(BaseTestCase): def setUp(self): super(LoggingPanelTestCase, self).setUp() self.panel = self.toolbar.get_panel_by_id('LoggingPanel') self.logger = logging.getLogger(__name__) + # DEBUG may be set to False initially, preventing the default tracking + # from executing. Force an enable here to ensure that logging is activated. + collector.enable_logging() collector.clear_collection() def test_happy_case(self):