Skip to content

Update logging.rst with example #286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/how_to_guides/logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,33 @@ The logger class can be customised by changing the ``logger_class``
attribute of the ``Config`` class. This is only possible when using
the python based configuration file. The
``hypercorn.logging.Logger`` class is used by default.

Here is an example demonstrating how to avoid logging paths starting with ``'/media'``.

.. code-block:: python

# custom_logger.py
from hypercorn.logging import Logger
class CustomLogger(Logger):
"""In a separate module to avoid PickleError"""
async def access(self, request, response, request_time):
if request and not request.get('path', '').startswith('/media'):
await super().access(request, response, request_time)

.. code-block:: python

# hypercorn_conf.py
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__))) # assuming `custom_logger.py` is in the same directory
from custom_logger import CustomLogger

logger_class=CustomLogger
bind='0.0.0.0:8080'
accesslog='/logs/access.log'

And following the advice of the configs how-to-guide, we point hypercorn to ``hypercorn_conf.py`` using this.

.. code-block:: python

/path/to/venv/bin/hypercorn --config file:/path/to/hypercorn_conf.py /path/to/main:app