Last active
December 1, 2022 12:19
-
-
Save rmminusrslash/77ed5795070fbec644b3acff07f1e7da to your computer and use it in GitHub Desktop.
add request id to all log lines
This file contains 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
from loguru import logger | |
def task(): | |
logger.info("Processing!") | |
if __name__ == "__main__": | |
# remove default logger | |
logger.remove(0) | |
# configure the logger format so that extra or certain fields of extra are printed | |
logger.add( | |
sys.stderr, | |
format="<red>[{level}]</red> Message : <green>{message}</green> @ {time} | Request_id {extra[request_id]}", | |
colorize=True, | |
) | |
with logger.contextualize(request_id=123): | |
task() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment