-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_logger.py
56 lines (42 loc) · 1.52 KB
/
test_logger.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import logging
from logfmt_logger import getLogger
def test_log_with_context(capsys):
logger = getLogger("TEST1")
logger.error(
"log context",
extra={"context": {"test": 'this is "with double quote" a context'}},
)
captured = capsys.readouterr()
assert 'test="this is \\"with double quote\\" a context"' in captured.err
def test_log_level(capsys):
logger = getLogger("TEST2")
logger.info("info message")
captured = capsys.readouterr()
assert 'level=INFO' in captured.err
logger.warning("warn message")
captured = capsys.readouterr()
assert 'level=WARNING' in captured.err
logger.error("error message")
captured = capsys.readouterr()
assert 'level=ERROR' in captured.err
logger.critical("critical message")
captured = capsys.readouterr()
assert 'level=CRITICAL' in captured.err
def test_log_with_execeptions(capsys):
logger = getLogger("TEST3")
try:
raise ValueError('Exception message with "double quotes"')
except ValueError:
logger.info("exception", exc_info=True)
captured = capsys.readouterr()
assert (
'ValueError: Exception message with \\"double quotes\\"' in captured.err
)
def test_log_level_avoid_debug_when_in_info(capsys):
logger = getLogger("TEST4", level=logging.WARN)
logger.info("info message")
captured = capsys.readouterr()
assert 'level=INFO' not in captured.err
logger.error("error message")
captured = capsys.readouterr()
assert 'level=ERROR' in captured.err