Last active
May 16, 2021 18:40
-
-
Save jamesmishra/56ec0c3b519138d8ea0065e2f7bb346f to your computer and use it in GitHub Desktop.
log-with-context example
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 logging.config | |
from log_with_context import add_logging_context, Logger | |
logging.config.dictConfig({ | |
"version": 1, | |
"disable_existing_loggers": True, | |
"formatters": { | |
"json": {"()": "json_log_formatter.JSONFormatter"}, | |
}, | |
"handlers": { | |
"console": { | |
"formatter": "json", | |
"class": "logging.StreamHandler", | |
} | |
}, | |
"loggers": { | |
"": {"handlers": ["console"], "level": "INFO"}, | |
}, | |
}) | |
LOGGER = Logger(__name__) | |
LOGGER.info("First message. No context") | |
with add_logging_context(current_request="hi"): | |
LOGGER.info("Level 1") | |
with add_logging_context(more_info="this"): | |
LOGGER.warning("Level 2") | |
LOGGER.info("Back to level 1") | |
LOGGER.error("No context at all...") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment