add message_id in log to keep track of an email processing

This commit is contained in:
Son NK 2021-03-15 19:41:42 +01:00
parent ca4d097f14
commit 6eb7ebc338
2 changed files with 28 additions and 2 deletions

View File

@ -9,9 +9,30 @@ from app.config import (
)
# this format allows clickable link to code source in PyCharm
_log_format = '%(asctime)s - %(name)s - %(levelname)s - "%(pathname)s:%(lineno)d" - %(funcName)s() - %(message)s'
_log_format = '%(asctime)s - %(name)s - %(levelname)s - "%(pathname)s:%(lineno)d" - %(funcName)s() - %(message_id)s - %(message)s'
_log_formatter = logging.Formatter(_log_format)
# used to keep track of an email lifecycle
_MESSAGE_ID = None
def set_message_id(message_id):
global _MESSAGE_ID
print("set message_id", message_id)
_MESSAGE_ID = message_id
class EmailHandlerFilter(logging.Filter):
"""automatically add message-id to keep track of an email processing"""
def filter(self, record):
message_id = self.get_message_id()
record.message_id = message_id if message_id else ""
return True
def get_message_id(self):
return _MESSAGE_ID
def _get_console_handler():
console_handler = logging.StreamHandler(sys.stdout)
@ -29,6 +50,8 @@ def _get_logger(name):
# leave the handlers level at NOTSET so the level checking is only handled by the logger
logger.addHandler(_get_console_handler())
logger.addFilter(EmailHandlerFilter())
# no propagation to avoid propagating to root logger
logger.propagate = False

View File

@ -114,7 +114,7 @@ from app.email_utils import (
)
from app.extensions import db
from app.greylisting import greylisting_needed
from app.log import LOG
from app.log import LOG, set_message_id
from app.models import (
Alias,
Contact,
@ -1779,6 +1779,9 @@ class MailHandler:
envelope.rcpt_tos,
)
# generate a different message_id to keep track of an email lifecycle
set_message_id(str(uuid.uuid4()))
app = new_app()
with app.app_context():
ret = handle(envelope)