Skip to content
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

docs(logging): advise to use log_exceptions="always" #3577

Merged
merged 1 commit into from
Jun 17, 2024
Merged
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
25 changes: 18 additions & 7 deletions docs/usage/logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Application and request level loggers can be configured using the :class:`~lites
formatters={
"standard": {"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"}
},
log_exceptions="always",
)

app = Litestar(route_handlers=[my_router_handler], logging_config=logging_config)
Expand All @@ -32,12 +33,21 @@ Application and request level loggers can be configured using the :class:`~lites
is keyed as ``queue_listener`` in the logging configuration. The above example is using this handler,
which is optimal for async applications. Make sure to use it in your own loggers as in the above example.

.. attention::

Exceptions won't be logged by default, except in debug mode. Make sure to use ``log_exceptions="always"`` as in the
example above to log exceptions if you need it.


Using Python standard library
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Standard Library Logging (Manual Configuration)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`logging <https://docs.python.org/3/howto/logging.html>`_ is Python's builtin standard logging library and can be
configured through ``LoggingConfig``.

`logging <https://docs.python.org/3/howto/logging.html>`_ is Python's builtin standard logging library and can be integrated with `LoggingConfig` as the `root` logging. By using `logging_config()()` you can build a `logger` to be used around your project.
The ``LoggingConfig.configure()`` method returns a reference to ``logging.getLogger`` which can be used to access a
logger instance. Thus, the root logger can retrieved with ``logging_config.configure()()`` as shown in the example
below:

.. code-block:: python

Expand All @@ -47,10 +57,11 @@ Standard Library Logging (Manual Configuration)
from litestar.logging import LoggingConfig

logging_config = LoggingConfig(
root={"level": logging.getLevelName(logging.INFO), "handlers": ["console"]},
root={"level": "INFO", "handlers": ["queue_listener"]},
formatters={
"standard": {"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"}
},
log_exceptions="always",
)

logger = logging_config.configure()()
Expand All @@ -67,7 +78,7 @@ Standard Library Logging (Manual Configuration)
logging_config=logging_config,
)

The above example is the same as using logging without the litestar LoggingConfig.
The above example is the same as using logging without the litestar ``LoggingConfig``.

.. code-block:: python

Expand Down Expand Up @@ -112,8 +123,8 @@ the part of the user. That is, if ``picologging`` is present the previous exampl
Using StructLog
^^^^^^^^^^^^^^^

`StructLog <https://www.structlog.org/en/stable/>`_ is a powerful structured-logging library. Litestar ships with a dedicated
logging plugin and config for using it:
`StructLog <https://www.structlog.org/en/stable/>`_ is a powerful structured-logging library. Litestar ships with a
dedicated logging plugin and config for using it:

.. code-block:: python

Expand Down
Loading