-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathtest_logging.py
38 lines (28 loc) · 1009 Bytes
/
test_logging.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import logging
from io import StringIO
import structlog
from structlog.testing import capture_logs
from codegate.codegate_logging import (
LogFormat,
LogLevel,
setup_logging,
)
def test_setup_logging():
setup_logging(log_level=LogLevel.DEBUG, log_format=LogFormat.JSON)
with capture_logs() as cap_logs:
logger = structlog.get_logger("codegate")
logger.debug("Debug message")
# cap_logs is a dictionary with the list of log entries
log_entry = cap_logs[0]
assert log_entry["log_level"] == "debug"
assert log_entry["event"] == "Debug message"
def test_logging_stream_output():
setup_logging(log_level=LogLevel.DEBUG, log_format=LogFormat.TEXT)
logger = logging.getLogger("codegate")
log_output = StringIO()
handler = logging.StreamHandler(log_output)
logger.addHandler(handler)
logger.debug("Debug message")
log_output.seek(0)
formatted_log = log_output.getvalue().strip()
assert "Debug message" in formatted_log