File tree Expand file tree Collapse file tree 3 files changed +30
-1
lines changed Expand file tree Collapse file tree 3 files changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,24 @@ def emit(self, record):
4545 self .collector .collect (record )
4646
4747
48+ # Preserve Python's fallback log mechanism before adding ThreadTrackingHandler.
49+
50+ # If the root logger has no handlers attached then everything that reaches it goes
51+ # to the "handler of last resort".
52+ # So a Django app that doesn't explicitly configure the root logger actually logs
53+ # through logging.lastResort.
54+ # However, logging.lastResort is not used after ThreadTrackingHandler gets added to
55+ # the root logger below. This means that users who have LoggingPanel enabled might
56+ # find their logs are gone from their app as soon as they install DDT.
57+ # Explicitly adding logging.lastResort to logging.root's handler sidesteps this
58+ # potential confusion.
59+ # Note that if root has already been configured, or logging.lastResort has been
60+ # removed, then the configuration is unchanged, so users who configured their
61+ # logging aren't exposed to the opposite confusion of seeing extra log lines from
62+ # their app.
63+ if not logging .root .hasHandlers () and logging .lastResort is not None :
64+ logging .root .addHandler (logging .lastResort )
65+
4866# We don't use enable/disable_instrumentation because logging is global.
4967# We can't add thread-local logging handlers. Hopefully logging is cheap.
5068
Original file line number Diff line number Diff line change 11import logging
2+ from unittest .mock import patch
23
34from debug_toolbar .panels .logging import (
45 MESSAGE_IF_STRING_REPRESENTATION_INVALID ,
@@ -86,3 +87,10 @@ def view(request):
8687 self .assertEqual (
8788 MESSAGE_IF_STRING_REPRESENTATION_INVALID , records [0 ]["message" ]
8889 )
90+
91+ @patch ("sys.stderr" )
92+ def test_fallback_logging (self , mock_stderr ):
93+ # make sure the log reaches stderr even though logging set up
94+ # its own handler during its import
95+ self .logger .warning ("hello" )
96+ mock_stderr .write .assert_called_once_with ("hello\n " )
Original file line number Diff line number Diff line change 1111
1212INTERNAL_IPS = ["127.0.0.1" ]
1313
14- LOGGING_CONFIG = None # avoids spurious output in tests
14+ LOGGING = { # avoids spurious output in tests
15+ "version" : 1 ,
16+ "disable_existing_loggers" : True ,
17+ }
1518
1619
1720# Application definition
You can’t perform that action at this time.
0 commit comments