rich/docs/source/logging.rst

71 lines
2.6 KiB
ReStructuredText
Raw Normal View History

2020-03-27 08:58:22 +00:00
Logging Handler
===============
2020-03-27 08:53:58 +00:00
Rich supplies a :ref:`logging handler<logging>` which will format and colorize text written by Python's logging module.
Here's an example of how to set up a rich logger::
import logging
from rich.logging import RichHandler
FORMAT = "%(message)s"
logging.basicConfig(
2020-05-19 12:38:13 +00:00
level="NOTSET", format=FORMAT, datefmt="[%X]", handlers=[RichHandler()]
2020-03-27 08:53:58 +00:00
)
2020-03-27 08:58:22 +00:00
2020-03-27 08:53:58 +00:00
log = logging.getLogger("rich")
2020-06-30 20:40:11 +00:00
log.info("Hello, World!")
2021-12-12 11:17:48 +00:00
Rich logs won't render :ref:`console_markup` in logging by default as most libraries won't be aware of the need to escape literal square brackets, but you can enable it by setting ``markup=True`` on the handler. Alternatively you can enable it per log message by supplying the ``extra`` argument as follows::
2020-06-30 20:40:11 +00:00
log.error("[bold red blink]Server is shutting down![/]", extra={"markup": True})
2020-06-30 20:40:11 +00:00
2021-12-12 11:17:48 +00:00
Similarly, the highlighter may be overridden per log message::
log.error("123 will not be highlighted", extra={"highlighter": None})
Handle exceptions
-------------------
2022-01-03 09:04:35 +00:00
The :class:`~rich.logging.RichHandler` class may be configured to use Rich's :class:`~rich.traceback.Traceback` class to format exceptions, which provides more context than a built-in exception. To get beautiful exceptions in your logs set ``rich_tracebacks=True`` on the handler constructor::
2020-09-27 11:41:56 +00:00
import logging
from rich.logging import RichHandler
2020-09-30 08:34:12 +00:00
2020-09-27 11:41:56 +00:00
logging.basicConfig(
level="NOTSET",
format="%(message)s",
datefmt="[%X]",
handlers=[RichHandler(rich_tracebacks=True)]
)
log = logging.getLogger("rich")
try:
2020-09-27 11:41:56 +00:00
print(1 / 0)
except Exception:
log.exception("unable print!")
2020-09-26 14:56:27 +00:00
There are a number of other options you can use to configure logging output, see the :class:`~rich.logging.RichHandler` reference for details.
2022-01-21 22:25:49 +00:00
Suppressing Frames
------------------
If you are working with a framework (click, django etc), you may only be interested in seeing the code from your own application within the traceback. You can exclude framework code by setting the `suppress` argument on `Traceback`, `install`, and `Console.print_exception`, which should be a list of modules or str paths.
Here's how you would exclude `click <https://click.palletsprojects.com/en/8.0.x/>`_ from Rich exceptions::
import click
import logging
from rich.logging import RichHandler
logging.basicConfig(
level="NOTSET",
format="%(message)s",
datefmt="[%X]",
handlers=[RichHandler(rich_tracebacks=True, tracebacks_suppress)]
)
Suppressed frames will show the line and file only, without any code.