only call expensive debug logging on debug level

This commit is contained in:
Peter Bieringer
2025-12-28 08:01:05 +01:00
parent 6bd7a5b5e3
commit 76abfe719b

View File

@@ -30,6 +30,7 @@ import base64
import cProfile import cProfile
import datetime import datetime
import io import io
import logging
import pprint import pprint
import pstats import pstats
import random import random
@@ -253,8 +254,10 @@ class Application(ApplicationPartDelete, ApplicationPartHead,
if answer is not None: if answer is not None:
if isinstance(answer, str): if isinstance(answer, str):
if self._response_content_on_debug: if self._response_content_on_debug:
if logger.isEnabledFor(logging.DEBUG):
logger.debug("Response content (nonXML):\n%s", utils.textwrap_str(answer)) logger.debug("Response content (nonXML):\n%s", utils.textwrap_str(answer))
else: else:
if logger.isEnabledFor(logging.DEBUG):
logger.debug("Response content: suppressed by config/option [logging] response_content_on_debug") logger.debug("Response content: suppressed by config/option [logging] response_content_on_debug")
headers["Content-Type"] += "; charset=%s" % self._encoding headers["Content-Type"] += "; charset=%s" % self._encoding
answer = answer.encode(self._encoding) answer = answer.encode(self._encoding)
@@ -276,8 +279,10 @@ class Application(ApplicationPartDelete, ApplicationPartHead,
headers.update(self._extra_headers) headers.update(self._extra_headers)
if self._response_header_on_debug: if self._response_header_on_debug:
if logger.isEnabledFor(logging.DEBUG):
logger.debug("Response header:\n%s", utils.textwrap_str(pprint.pformat(headers))) logger.debug("Response header:\n%s", utils.textwrap_str(pprint.pformat(headers)))
else: else:
if logger.isEnabledFor(logging.DEBUG):
logger.debug("Response header: suppressed by config/option [logging] response_header_on_debug") logger.debug("Response header: suppressed by config/option [logging] response_header_on_debug")
# Start response # Start response