瀏覽代碼

conditional debug log of request header+content / response content

Peter Bieringer 1 年之前
父節點
當前提交
02019e73e6
共有 3 個文件被更改,包括 13 次插入6 次删除
  1. 7 3
      radicale/app/__init__.py
  2. 4 2
      radicale/app/base.py
  3. 2 1
      radicale/httputils.py

+ 7 - 3
radicale/app/__init__.py

@@ -82,6 +82,8 @@ class Application(ApplicationPartDelete, ApplicationPartHead,
         super().__init__(configuration)
         self._mask_passwords = configuration.get("logging", "mask_passwords")
         self._bad_put_request_content = configuration.get("logging", "bad_put_request_content")
+        self._request_header_on_debug = configuration.get("logging", "request_header_on_debug")
+        self._response_content_on_debug = configuration.get("logging", "response_content_on_debug")
         self._auth_delay = configuration.get("auth", "delay")
         self._internal_server = configuration.get("server", "_internal_server")
         self._max_content_length = configuration.get(
@@ -141,7 +143,8 @@ class Application(ApplicationPartDelete, ApplicationPartHead,
             answers = []
             if answer is not None:
                 if isinstance(answer, str):
-                    logger.debug("Response content:\n%s", answer)
+                    if self._response_content_on_debug:
+                        logger.debug("Response content:\n%s", answer)
                     headers["Content-Type"] += "; charset=%s" % self._encoding
                     answer = answer.encode(self._encoding)
                 accept_encoding = [
@@ -187,8 +190,9 @@ class Application(ApplicationPartDelete, ApplicationPartHead,
         logger.info("%s request for %r%s received from %s%s",
                     request_method, unsafe_path, depthinfo,
                     remote_host, remote_useragent)
-        logger.debug("Request headers:\n%s",
-                     pprint.pformat(self._scrub_headers(environ)))
+        if self._request_header_on_debug:
+            logger.debug("Request headers:\n%s",
+                         pprint.pformat(self._scrub_headers(environ)))
 
         # SCRIPT_NAME is already removed from PATH_INFO, according to the
         # WSGI specification.

+ 4 - 2
radicale/app/base.py

@@ -50,6 +50,7 @@ class ApplicationBase:
         self._web = web.load(configuration)
         self._encoding = configuration.get("encoding", "request")
         self._log_bad_put_request_content = configuration.get("logging", "bad_put_request_content")
+        self._response_content_on_debug = configuration.get("logging", "response_content_on_debug")
         self._hook = hook.load(configuration)
 
     def _read_xml_request_body(self, environ: types.WSGIEnviron
@@ -71,8 +72,9 @@ class ApplicationBase:
 
     def _xml_response(self, xml_content: ET.Element) -> bytes:
         if logger.isEnabledFor(logging.DEBUG):
-            logger.debug("Response content:\n%s",
-                         xmlutils.pretty_xml(xml_content))
+            if self._response_content_on_debug:
+                logger.debug("Response content:\n%s",
+                             xmlutils.pretty_xml(xml_content))
         f = io.BytesIO()
         ET.ElementTree(xml_content).write(f, encoding=self._encoding,
                                           xml_declaration=True)

+ 2 - 1
radicale/httputils.py

@@ -142,7 +142,8 @@ def read_request_body(configuration: "config.Configuration",
                       environ: types.WSGIEnviron) -> str:
     content = decode_request(configuration, environ,
                              read_raw_request_body(configuration, environ))
-    logger.debug("Request content:\n%s", content)
+    if configuration.get("logging", "request_content_on_debug"):
+        logger.debug("Request content:\n%s", content)
     return content