Skip to content

Python logging

Colored logging

structlog

Install:

sudo pacman -S python-structlog python-rich

Set default log level:

structlog.configure(
  wrapper_class=structlog.make_filtering_bound_logger(logging.DEBUG)
)

Change default from stdout to stderr:

structlog.configure(logger_factory=structlog.PrintLoggerFactory(sys.stderr))

Hello World example:

import structlog
log = structlog.get_logger()
log.info("hello, %s!", "world", key="value!", more_than_strings=[1, 2, 3])

Other options

Libraries using logging

see python3-urllib3:

grep logg -ir /usr/lib/python3/dist-packages/urllib3

Initialised in /usr/lib/python3/dist-packages/urllib3/__init__.py:

Modules used this:

import logging
log = logging.getLogger(__name__)