Package: logging-context
Python Logging Context1. Descriptionimportloggingfromlogging_contextimportget_logging_context,setup_logging_contextfromlogging_context.formatterimportLoggingContextFormatterlogger=logging.getLogger("your_logger")logger.setLevel(logging.INFO)handler=logging.StreamHandler()handler.setFormatter(LoggingContextFormatter("%(asctime)s-%(name)s-%(levelname)s- context=%(context)s-%(message)s"))handler.setLevel(logging.INFO)logger.addHandler(handler)logger.info("before setup")# 2021-11-30 16:49:52,060 - your_logger - INFO - context=None - before setupcontext=get_logging_context()setup_logging_context(context)logger.info("after setup")# 2021-11-30 16:50:12,916 - your_logger - INFO - context={} - after setupcontext.set_value("var",100)logger.info("after set value")# 2021-11-30 16:50:36,562 - your_logger - INFO - context={"var": 100} - after set valuecontext.set_value("var",200)logger.info("after change value")# 2021-11-30 16:50:53,912 - your_logger - INFO - context={"var": 200} - after change valuecontext.delete_value("var")logger.info("after delete value")# 2021-11-30 16:51:18,369 - your_logger - INFO - context={} - after delete value2. Installationpipinstalllogging-context3. Usage3.1 Setup logging record factoryFunctionsetup_logging_contextwill add arecord.contextattribute into your log record.If you don't run the function, logging can't found any attributecontextand raiseKeyErrorexception.fromlogging_contextimportget_logging_context,setup_logging_contextcontext=get_logging_context()setup_logging_context(context)3.2 Setup logging formatFunctionsetup_logging_contextwill add arecord.contextattribute into your log record.You can add%(context)sinto your log format to show entire context values in your logYou should useLoggingContextFormatterinstead of defaultlogging.Formatter.LoggingContextFormatteraddedrecord.contextto your log record by default.context=get_logging_context()context.set_value("var1",200)context.set_value("var2","var2 value")handler=logging.StreamHandler()handler.setFormatter(LoggingContextFormatter("%(asctime)s-%(name)s-%(levelname)s- context=%(context)s-%(message)s"))logger.addHandler(handler)logger.info("log message")# 2021-11-30 17:08:14,263 - your_logger - INFO - context={"var1": 200, "var2": "var2 value"} - log message3.3 Context objectWherever you want to use context, you should call functionget_logging_contextto get current contextfromlogging_contextimportget_logging_contextcontext=get_logging_context()You can set/update/delete any context value and you can clear any values of your contextlogger.info("before set value")# 2021-11-30 17:12:40,413 - your_logger - INFO - context={} - before set valuecontext.set_value("var1",200)context.set_value("var2","var2 value")logger.info("after set value")# 2021-11-30 17:13:09,508 - your_logger - INFO - context={"var1": 200, "var2": "var2 value"} - after set valuecontext.set_value("var1",100)logger.info("after change value")# 2021-11-30 17:14:20,317 - your_logger - INFO - context={"var1": 100, "var2": "var2 value"} - after change valuecontext.delete_value("var2")logger.info("after delete value")# 2021-11-30 17:14:52,383 - your_logger - INFO - context={"var1": 100} - after delete valuecontext.set_value("var1",150)context.set_value("var2","var2 value")# 2021-11-30 17:16:05,623 - your_logger - INFO - context={"var1": 150, "var2": "var2 value"} - before cleancontext.clean()logger.info("after clean")# 2021-11-30 17:16:45,567 - your_logger - INFO - context={} - after cleanDevelopmentClone this project and run following commands to setup environmentcdlogging_context
makevirtualenvsource.venv/bin/activate
makeinstall