userver: /home/antonyzhilin/arcadia/taxi/uservices/userver/testsuite/pytest_plugins/pytest_userver/plugins/logging.py Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
logging.py
1from __future__ import annotations
2
3import logging
4import pathlib
5
6import pytest
7
8from pytest_userver.utils import colorize
9
10logger = logging.getLogger(__name__)
11
12
13@pytest.fixture(scope='session')
14def service_logfile_path(
15 pytestconfig,
16 service_tmpdir: pathlib.Path,
17) -> pathlib.Path | None:
18 """
19 Holds optional service logfile path. You may want to override this
20 in your service.
21
22 By default returns value of --service-logs-file option or creates
23 temporary file.
24 """
25 if pytestconfig.option.service_logs_file:
26 return pytestconfig.option.service_logs_file
27 return service_tmpdir / 'service.log'
28
29
30@pytest.fixture(scope='session')
31def _service_logfile_path(
32 userver_register_logfile,
33 service_logfile_path: pathlib.Path | None,
34) -> pathlib.Path | None:
35 if not service_logfile_path:
36 return None
37 userver_register_logfile(
38 service_logfile_path,
39 title='userver/log',
40 truncate=True,
41 )
42 return service_logfile_path
43
44
45@pytest.fixture(scope='session')
46def userver_register_logfile(servicelogs_register_logfile, _userver_log_formatter_factory):
47 """
48 Register logfile. Registered logfile is monitored in case of test failure
49 and its contents is attached to pytest report.
50
51 :param path: pathlib.Path corresponding to log file
52 :param title: title to be used in pytest report
53 :param truncate: file is truncated if True
54
55 ```python
56 def register_logfile(
57 path: pathlib.Path, *, title: str, truncate: bool = False,
58 ) -> None:
59 ```
60 """
61
62 def do_truncate(path):
63 with path.open('wb+') as fp:
64 fp.truncate()
65
66 def register_logfile(
67 path: pathlib.Path,
68 *,
69 title: str,
70 truncate: bool = False,
71 ) -> None:
72 if truncate:
73 do_truncate(path)
74 servicelogs_register_logfile(
75 path,
76 title=title,
77 formatter_factory=_userver_log_formatter_factory,
78 )
79 return path
80
81 return register_logfile
82
83
84@pytest.fixture(scope='session')
85def _userver_log_formatter_factory(pytestconfig, testsuite_colors_enabled):
86 def colorizer_factory():
87 verbose = pytestconfig.option.service_logs_pretty == 'verbose'
88 colorizer = colorize.Colorizer(
89 verbose=verbose,
90 colors_enabled=testsuite_colors_enabled,
91 )
92
93 def format_line(rawline):
94 line = rawline.decode(encoding='utf-8', errors='backslashreplace')
95 if not line.startswith('tskv\t'):
96 return None
97 return colorizer.colorize_line(line)
98
99 return format_line
100
101 def default_factory():
102 def format_line(rawline):
103 return rawline.decode(encoding='utf-8', errors='backslashreplace')
104
105 return format_line
106
107 if pytestconfig.option.service_logs_pretty:
108 return colorizer_factory
109 return default_factory