File tree 3 files changed +30
-1
lines changed 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):
45
45
self .collector .collect (record )
46
46
47
47
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
+
48
66
# We don't use enable/disable_instrumentation because logging is global.
49
67
# We can't add thread-local logging handlers. Hopefully logging is cheap.
50
68
Original file line number Diff line number Diff line change 1
1
import logging
2
+ from unittest .mock import patch
2
3
3
4
from debug_toolbar .panels .logging import (
4
5
MESSAGE_IF_STRING_REPRESENTATION_INVALID ,
@@ -86,3 +87,10 @@ def view(request):
86
87
self .assertEqual (
87
88
MESSAGE_IF_STRING_REPRESENTATION_INVALID , records [0 ]["message" ]
88
89
)
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 11
11
12
12
INTERNAL_IPS = ["127.0.0.1" ]
13
13
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
+ }
15
18
16
19
17
20
# Application definition
You can’t perform that action at this time.
0 commit comments