rich/docs/source/logging.rst

24 lines
1008 B
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!")
2020-07-23 15:49:07 +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
There are a number of options you can use to configure logging output, see the :class:`~rich.logging.RichHandler` reference for details.