Skip to content

Instantly share code, notes, and snippets.

@jamesmishra
Last active May 16, 2021 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmishra/56ec0c3b519138d8ea0065e2f7bb346f to your computer and use it in GitHub Desktop.
Save jamesmishra/56ec0c3b519138d8ea0065e2f7bb346f to your computer and use it in GitHub Desktop.
log-with-context example
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