Original code copyright (C) 2009-2022 Rudolf Cardinal (

This file is part of cardinal_pythonlib.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

WSGI middleware to log incoming request/response details.

class cardinal_pythonlib.wsgi.request_logging_mw.RequestLoggingMiddleware(app: ~typing.Callable[[~typing.Dict[str, str], ~typing.Callable[[str, ~typing.List[~typing.Tuple[str, str]], ~typing.Tuple[~typing.Type[BaseException] | None, BaseException | None, ~types.TracebackType | None] | None], ~typing.Callable[[str], None]]], ~typing.Iterable[bytes]], logger: ~logging.Logger = <BraceStyleAdapter cardinal_pythonlib.wsgi.request_logging_mw (WARNING)>, loglevel: int = 20, show_request_immediately: bool = True, show_response: bool = True, show_timing: bool = True)[source]

WSGI middleware to log incoming request details (+/- the response status code and timing information).

  • app – The WSGI application to wrap

  • logger – The Python logger to write to

  • loglevel – The log level to use (e.g. logging.DEBUG, logging.INFO)

  • show_request_immediately – Show the request immediately, so it’s written to the log before the WSGI app does its processing, and is guaranteed to be visible even if the WSGI app hangs? The only reason to use False is probably if you intend to show response and/or timing information and you want to minimize the number of lines written to the log; in this case, only a single line is written to the log (after the wrapped WSGI app has finished processing).

  • show_response – Show the HTTP response code?

  • show_timing – Show the time that the wrapped WSGI app took?

log(msg) None[source]

Writes a message to the chosen log.