mirror of
https://github.com/simple-login/app.git
synced 2025-02-24 07:43:54 +08:00
add message_id in log to keep track of an email processing
This commit is contained in:
parent
ca4d097f14
commit
6eb7ebc338
2 changed files with 28 additions and 2 deletions
25
app/log.py
25
app/log.py
|
@ -9,9 +9,30 @@ from app.config import (
|
||||||
)
|
)
|
||||||
|
|
||||||
# this format allows clickable link to code source in PyCharm
|
# 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)
|
_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():
|
def _get_console_handler():
|
||||||
console_handler = logging.StreamHandler(sys.stdout)
|
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
|
# leave the handlers level at NOTSET so the level checking is only handled by the logger
|
||||||
logger.addHandler(_get_console_handler())
|
logger.addHandler(_get_console_handler())
|
||||||
|
|
||||||
|
logger.addFilter(EmailHandlerFilter())
|
||||||
|
|
||||||
# no propagation to avoid propagating to root logger
|
# no propagation to avoid propagating to root logger
|
||||||
logger.propagate = False
|
logger.propagate = False
|
||||||
|
|
||||||
|
|
|
@ -114,7 +114,7 @@ from app.email_utils import (
|
||||||
)
|
)
|
||||||
from app.extensions import db
|
from app.extensions import db
|
||||||
from app.greylisting import greylisting_needed
|
from app.greylisting import greylisting_needed
|
||||||
from app.log import LOG
|
from app.log import LOG, set_message_id
|
||||||
from app.models import (
|
from app.models import (
|
||||||
Alias,
|
Alias,
|
||||||
Contact,
|
Contact,
|
||||||
|
@ -1779,6 +1779,9 @@ class MailHandler:
|
||||||
envelope.rcpt_tos,
|
envelope.rcpt_tos,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# generate a different message_id to keep track of an email lifecycle
|
||||||
|
set_message_id(str(uuid.uuid4()))
|
||||||
|
|
||||||
app = new_app()
|
app = new_app()
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
ret = handle(envelope)
|
ret = handle(envelope)
|
||||||
|
|
Loading…
Reference in a new issue