-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
68 additions
and
22 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import contextvars | ||
import logging | ||
|
||
# Create a context variable to store request_id per request | ||
request_id_var = contextvars.ContextVar("request_id", default="") | ||
|
||
class ContextFilter(logging.Filter): | ||
"""Logging filter to add request_id from contextvars.""" | ||
def filter(self, record): | ||
record.__dict__.setdefault("request_id", request_id_var.get()) | ||
return True | ||
|
||
def config_logging(): | ||
formatter = logging.Formatter( | ||
"%(asctime)s - %(name)s - %(levelname)s - request_id=%(request_id)s %(message)s", | ||
defaults={"request_id": ""}, | ||
datefmt='%Y-%m-%d %H:%M:%S' | ||
) | ||
handler = logging.StreamHandler() | ||
handler.setFormatter(formatter) | ||
handler.setLevel(logging.INFO) | ||
logger = logging.getLogger() # Root logger | ||
logger.setLevel(logging.INFO) | ||
logger.addHandler(handler) | ||
logger.addFilter(ContextFilter()) # Attach the filter to all loggers |
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
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
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