FAQ
Why a new library and why not using structlog?
structlog is a "non standard" library (with some kind of non-perfect adapters with the standard logging system) designed before contextvars.
It's clearly a very clever piece of code but we prefer to keep around the standard python logging.
Dependency free? Why?
Because log library are ubiquitous and to avoid any dependency conflicts, stlog
was built to have no dependency at all but:
- borrows plenty of ideas and code from daiquiri library (thanks to Mergify and Julien DANJOU.
- borrows some code from python-json-logger (thanks to Zakaria ZAJAC)
- borrows some ode from python-logmter (thanks to Joshua Taylor Eppinette)
- can use fancy stuff (colors, augmented traceback...) from the rich library (only if installed)
This is an opinionated choice. But we assume that.
Mixing stlog
and python standard logging?
Not a problem!
import stlog
import logging
# setup
stlog.setup()
# add some global context
stlog.LogContext.reset_context()
stlog.LogContext.add(request_id=123456, is_authenticated=True, http_method="GET")
# Let's make 2 loggers: a stlog one and a standard one
standard_logger = logging.getLogger()
stlog_logger = stlog.getLogger()
# Let's use them
standard_logger.warning("this is a warning from the standard logger")
stlog_logger.critical(
"this is a critical from stlog_logger with some extra context", xtra=123
)
Of course, when you use a classic python logger, you can't pass a specific context but the global context is automatically
reinjected. If you don't want this behavior, set reinject_context_in_standard_logging
to False
in setup
:
import stlog
import logging
# setup
stlog.setup(reinject_context_in_standard_logging=False)
# add some global context
stlog.LogContext.reset_context()
stlog.LogContext.add(request_id=123456, is_authenticated=True, http_method="GET")
# Let's make 2 loggers: a stlog one and a standard one
standard_logger = logging.getLogger()
stlog_logger = stlog.getLogger()
# Let's use them
standard_logger.warning("this is a warning from the standard logger")
stlog_logger.critical(
"this is a critical from stlog_logger with some extra context", xtra=123
)
Configuring stlog
library without setup()
and without Output
objects?
Yes, you can configure stlog
library without custom shortcuts like Output
or setup()
. FIXME
What about if I don't want to use UTC for my logs?
FIXME