Island: Add PyWSGILoggingFilter

This commit is contained in:
Mike Salvatore 2022-07-14 11:09:17 -04:00
parent bdd432fab6
commit 52bc877f86
2 changed files with 27 additions and 0 deletions

View File

@ -0,0 +1 @@
from .pywsgi_logging_filter import PyWSGILoggingFilter

View File

@ -0,0 +1,26 @@
import re
from logging import Filter, LogRecord
class PyWSGILoggingFilter(Filter):
"""
Remove the superfluous timestamp that gevent.pywsgi.WSGIServer inserts into its log messages
The WSGIServer.format_request() hard-codes its own log format. This filter modifies the log
message and removes the superfluous timestamp.
See https://github.com/guardicore/monkey/issues/2059 for more information.
"""
TIMESTAMP_REGEX = re.compile(r"- - \[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\]")
def filter(self, record: LogRecord) -> bool:
"""
Remove the superfluous timestamp in gevent.pywsgi.WSGIServer log messages
:param LogRecord: A log record to modify
:return: True
"""
record.msg = PyWSGILoggingFilter.TIMESTAMP_REGEX.sub("-", record.msg)
return True