From a1f2a058c81c6d9abc5800b6fb8a507c8586b0ec Mon Sep 17 00:00:00 2001 From: Amber Brown Date: Fri, 1 Jun 2018 21:28:55 +1000 Subject: [PATCH 1/3] put python's logs into trial --- tests/unittest.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/tests/unittest.py b/tests/unittest.py index 7b478c4294b2..6fe6495f1edd 100644 --- a/tests/unittest.py +++ b/tests/unittest.py @@ -14,21 +14,30 @@ # limitations under the License. import twisted from twisted.trial import unittest +from synapse.util.logcontext import LoggingContextFilter +from twisted.logger import Logger, LogLevel import logging -# logging doesn't have a "don't log anything at all EVARRRR setting, -# but since the highest value is 50, 1000000 should do ;) -NEVER = 1000000 +# Set up putting Synapse's logs into Trial's. +rootLogger = logging.getLogger() -handler = logging.StreamHandler() -handler.setFormatter(logging.Formatter( - "%(levelname)s:%(name)s:%(message)s [%(pathname)s:%(lineno)d]" -)) -logging.getLogger().addHandler(handler) -logging.getLogger().setLevel(NEVER) -logging.getLogger("synapse.storage.SQL").setLevel(NEVER) -logging.getLogger("synapse.storage.txn").setLevel(NEVER) +log_format = ( + "%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(request)s" + " - %(message)s" +) + +class ToTwistedHandler(logging.Handler): + tx_log = Logger() + def emit(self, record): + log_entry = self.format(record) + self.tx_log.emit(LogLevel.levelWithName(record.levelname.lower()), log_entry) + +handler = ToTwistedHandler() +formatter = logging.Formatter(log_format) +handler.setFormatter(formatter) +handler.addFilter(LoggingContextFilter(request="")) +rootLogger.addHandler(handler) def around(target): @@ -61,7 +70,7 @@ def __init__(self, methodName, *args, **kwargs): method = getattr(self, methodName) - level = getattr(method, "loglevel", getattr(self, "loglevel", NEVER)) + level = getattr(method, "loglevel", getattr(self, "loglevel", logging.ERROR)) @around(self) def setUp(orig): From 755da8d2d1a46aa2b4cbf4bc180ea6bd6dd540e4 Mon Sep 17 00:00:00 2001 From: Amber Brown Date: Mon, 4 Jun 2018 15:54:09 +1000 Subject: [PATCH 2/3] resort imports --- tests/unittest.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unittest.py b/tests/unittest.py index 6fe6495f1edd..780bdec89759 100644 --- a/tests/unittest.py +++ b/tests/unittest.py @@ -12,12 +12,12 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import logging + import twisted from twisted.trial import unittest -from synapse.util.logcontext import LoggingContextFilter -from twisted.logger import Logger, LogLevel -import logging +from synapse.util.logcontext import LoggingContextFilter # Set up putting Synapse's logs into Trial's. rootLogger = logging.getLogger() From 8fa4dbf058cf4dd5487bfcf4141c487942f0aefb Mon Sep 17 00:00:00 2001 From: Amber Brown Date: Mon, 4 Jun 2018 15:54:35 +1000 Subject: [PATCH 3/3] prevent tests failing, cleanup --- tests/unittest.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tests/unittest.py b/tests/unittest.py index 780bdec89759..184fe880f3ef 100644 --- a/tests/unittest.py +++ b/tests/unittest.py @@ -12,9 +12,11 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + import logging import twisted +import twisted.logger from twisted.trial import unittest from synapse.util.logcontext import LoggingContextFilter @@ -23,15 +25,18 @@ rootLogger = logging.getLogger() log_format = ( - "%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(request)s" - " - %(message)s" + "%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(request)s - %(message)s" ) + class ToTwistedHandler(logging.Handler): - tx_log = Logger() + tx_log = twisted.logger.Logger() + def emit(self, record): log_entry = self.format(record) - self.tx_log.emit(LogLevel.levelWithName(record.levelname.lower()), log_entry) + log_level = record.levelname.lower().replace('warning', 'warn') + self.tx_log.emit(twisted.logger.LogLevel.levelWithName(log_level), log_entry) + handler = ToTwistedHandler() formatter = logging.Formatter(log_format)