Created
May 22, 2025 16:06
-
-
Save sortega/fd9d8b10dc6044fa19504fb07b0a821a to your computer and use it in GitHub Desktop.
Sample setup code of how to keep using the standard logging frontend but using structlog to manage richer logging context
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import sys | |
import structlog | |
from pythonjsonlogger import jsonlogger | |
# Normal standard logging setup | |
logging.basicConfig( | |
format="%(message)s", | |
stream=sys.stderr, | |
level=logging.INFO | |
) | |
# Takeover setup | |
structlog.configure( | |
processors=[ | |
structlog.stdlib.filter_by_level, | |
structlog.stdlib.add_logger_name, | |
structlog.stdlib.add_log_level, | |
structlog.stdlib.PositionalArgumentsFormatter(), | |
structlog.processors.StackInfoRenderer(), | |
structlog.processors.format_exc_info, | |
structlog.processors.UnicodeDecoder(), | |
# Transform event dict into `logging.Logger` method arguments. | |
# "event" becomes "msg" and the rest is passed as a dict in | |
# "extra". IMPORTANT: This means that the standard library MUST | |
# render "extra" for the context to appear in log entries! See | |
# warning below. | |
structlog.stdlib.render_to_log_kwargs, | |
], | |
logger_factory=structlog.stdlib.LoggerFactory(), | |
wrapper_class=structlog.stdlib.BoundLogger, | |
cache_logger_on_first_use=True, | |
) | |
handler = logging.StreamHandler(sys.stdout) | |
handler.setFormatter(jsonlogger.JsonFormatter()) | |
root_logger = logging.getLogger() | |
root_logger.handlers = [handler] | |
# What you get in each module that logs | |
log = logging.getLogger(__name__) | |
log.info("Will it log?", extra={"key": "value"}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment