mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2024-12-25 20:39:24 -05:00
Improve tests for structured logging. (#8916)
This commit is contained in:
parent
0a34cdfc66
commit
3af0672350
1
changelog.d/8916.misc
Normal file
1
changelog.d/8916.misc
Normal file
@ -0,0 +1 @@
|
|||||||
|
Improve structured logging tests.
|
@ -18,30 +18,35 @@ import logging
|
|||||||
from io import StringIO
|
from io import StringIO
|
||||||
|
|
||||||
from synapse.logging._terse_json import JsonFormatter, TerseJsonFormatter
|
from synapse.logging._terse_json import JsonFormatter, TerseJsonFormatter
|
||||||
|
from synapse.logging.context import LoggingContext, LoggingContextFilter
|
||||||
|
|
||||||
from tests.logging import LoggerCleanupMixin
|
from tests.logging import LoggerCleanupMixin
|
||||||
from tests.unittest import TestCase
|
from tests.unittest import TestCase
|
||||||
|
|
||||||
|
|
||||||
class TerseJsonTestCase(LoggerCleanupMixin, TestCase):
|
class TerseJsonTestCase(LoggerCleanupMixin, TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.output = StringIO()
|
||||||
|
|
||||||
|
def get_log_line(self):
|
||||||
|
# One log message, with a single trailing newline.
|
||||||
|
data = self.output.getvalue()
|
||||||
|
logs = data.splitlines()
|
||||||
|
self.assertEqual(len(logs), 1)
|
||||||
|
self.assertEqual(data.count("\n"), 1)
|
||||||
|
return json.loads(logs[0])
|
||||||
|
|
||||||
def test_terse_json_output(self):
|
def test_terse_json_output(self):
|
||||||
"""
|
"""
|
||||||
The Terse JSON formatter converts log messages to JSON.
|
The Terse JSON formatter converts log messages to JSON.
|
||||||
"""
|
"""
|
||||||
output = StringIO()
|
handler = logging.StreamHandler(self.output)
|
||||||
|
|
||||||
handler = logging.StreamHandler(output)
|
|
||||||
handler.setFormatter(TerseJsonFormatter())
|
handler.setFormatter(TerseJsonFormatter())
|
||||||
logger = self.get_logger(handler)
|
logger = self.get_logger(handler)
|
||||||
|
|
||||||
logger.info("Hello there, %s!", "wally")
|
logger.info("Hello there, %s!", "wally")
|
||||||
|
|
||||||
# One log message, with a single trailing newline.
|
log = self.get_log_line()
|
||||||
data = output.getvalue()
|
|
||||||
logs = data.splitlines()
|
|
||||||
self.assertEqual(len(logs), 1)
|
|
||||||
self.assertEqual(data.count("\n"), 1)
|
|
||||||
log = json.loads(logs[0])
|
|
||||||
|
|
||||||
# The terse logger should give us these keys.
|
# The terse logger should give us these keys.
|
||||||
expected_log_keys = [
|
expected_log_keys = [
|
||||||
@ -57,9 +62,7 @@ class TerseJsonTestCase(LoggerCleanupMixin, TestCase):
|
|||||||
"""
|
"""
|
||||||
Additional information can be included in the structured logging.
|
Additional information can be included in the structured logging.
|
||||||
"""
|
"""
|
||||||
output = StringIO()
|
handler = logging.StreamHandler(self.output)
|
||||||
|
|
||||||
handler = logging.StreamHandler(output)
|
|
||||||
handler.setFormatter(TerseJsonFormatter())
|
handler.setFormatter(TerseJsonFormatter())
|
||||||
logger = self.get_logger(handler)
|
logger = self.get_logger(handler)
|
||||||
|
|
||||||
@ -67,12 +70,7 @@ class TerseJsonTestCase(LoggerCleanupMixin, TestCase):
|
|||||||
"Hello there, %s!", "wally", extra={"foo": "bar", "int": 3, "bool": True}
|
"Hello there, %s!", "wally", extra={"foo": "bar", "int": 3, "bool": True}
|
||||||
)
|
)
|
||||||
|
|
||||||
# One log message, with a single trailing newline.
|
log = self.get_log_line()
|
||||||
data = output.getvalue()
|
|
||||||
logs = data.splitlines()
|
|
||||||
self.assertEqual(len(logs), 1)
|
|
||||||
self.assertEqual(data.count("\n"), 1)
|
|
||||||
log = json.loads(logs[0])
|
|
||||||
|
|
||||||
# The terse logger should give us these keys.
|
# The terse logger should give us these keys.
|
||||||
expected_log_keys = [
|
expected_log_keys = [
|
||||||
@ -96,20 +94,13 @@ class TerseJsonTestCase(LoggerCleanupMixin, TestCase):
|
|||||||
"""
|
"""
|
||||||
The Terse JSON formatter converts log messages to JSON.
|
The Terse JSON formatter converts log messages to JSON.
|
||||||
"""
|
"""
|
||||||
output = StringIO()
|
handler = logging.StreamHandler(self.output)
|
||||||
|
|
||||||
handler = logging.StreamHandler(output)
|
|
||||||
handler.setFormatter(JsonFormatter())
|
handler.setFormatter(JsonFormatter())
|
||||||
logger = self.get_logger(handler)
|
logger = self.get_logger(handler)
|
||||||
|
|
||||||
logger.info("Hello there, %s!", "wally")
|
logger.info("Hello there, %s!", "wally")
|
||||||
|
|
||||||
# One log message, with a single trailing newline.
|
log = self.get_log_line()
|
||||||
data = output.getvalue()
|
|
||||||
logs = data.splitlines()
|
|
||||||
self.assertEqual(len(logs), 1)
|
|
||||||
self.assertEqual(data.count("\n"), 1)
|
|
||||||
log = json.loads(logs[0])
|
|
||||||
|
|
||||||
# The terse logger should give us these keys.
|
# The terse logger should give us these keys.
|
||||||
expected_log_keys = [
|
expected_log_keys = [
|
||||||
@ -119,3 +110,31 @@ class TerseJsonTestCase(LoggerCleanupMixin, TestCase):
|
|||||||
]
|
]
|
||||||
self.assertCountEqual(log.keys(), expected_log_keys)
|
self.assertCountEqual(log.keys(), expected_log_keys)
|
||||||
self.assertEqual(log["log"], "Hello there, wally!")
|
self.assertEqual(log["log"], "Hello there, wally!")
|
||||||
|
|
||||||
|
def test_with_context(self):
|
||||||
|
"""
|
||||||
|
The logging context should be added to the JSON response.
|
||||||
|
"""
|
||||||
|
handler = logging.StreamHandler(self.output)
|
||||||
|
handler.setFormatter(JsonFormatter())
|
||||||
|
handler.addFilter(LoggingContextFilter(request=""))
|
||||||
|
logger = self.get_logger(handler)
|
||||||
|
|
||||||
|
with LoggingContext() as context_one:
|
||||||
|
context_one.request = "test"
|
||||||
|
logger.info("Hello there, %s!", "wally")
|
||||||
|
|
||||||
|
log = self.get_log_line()
|
||||||
|
|
||||||
|
# The terse logger should give us these keys.
|
||||||
|
expected_log_keys = [
|
||||||
|
"log",
|
||||||
|
"level",
|
||||||
|
"namespace",
|
||||||
|
"request",
|
||||||
|
"scope",
|
||||||
|
]
|
||||||
|
self.assertCountEqual(log.keys(), expected_log_keys)
|
||||||
|
self.assertEqual(log["log"], "Hello there, wally!")
|
||||||
|
self.assertEqual(log["request"], "test")
|
||||||
|
self.assertIsNone(log["scope"])
|
||||||
|
Loading…
Reference in New Issue
Block a user