Skip to content

Commit 21d5879

Browse files
committed
Refactoring
1 parent a13350c commit 21d5879

File tree

1 file changed

+47
-28
lines changed

1 file changed

+47
-28
lines changed

reportportal_client/logs/__init__.py

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,33 @@
1616
import logging
1717
import sys
1818
import threading
19+
from typing import TYPE_CHECKING, Any, Optional, Union
1920
from urllib.parse import urlparse
2021

2122
# noinspection PyProtectedMember
2223
from reportportal_client._internal.local import current, set_current
2324
from reportportal_client.helpers import TYPICAL_MULTIPART_FOOTER_LENGTH, timestamp
2425

26+
if TYPE_CHECKING:
27+
from reportportal_client.client import RP
28+
29+
LOG_LEVEL_MAPPING: dict[int, str] = {
30+
logging.NOTSET: "TRACE",
31+
logging.DEBUG: "DEBUG",
32+
logging.INFO: "INFO",
33+
logging.WARNING: "WARN",
34+
logging.ERROR: "ERROR",
35+
logging.CRITICAL: "ERROR",
36+
}
37+
2538
MAX_LOG_BATCH_SIZE: int = 20
2639
MAX_LOG_BATCH_PAYLOAD_SIZE: int = int((64 * 1024 * 1024) * 0.98) - TYPICAL_MULTIPART_FOOTER_LENGTH
2740

2841

29-
class RPLogger(logging.getLoggerClass()):
42+
class RPLogger(logging.getLoggerClass()): # type: ignore
3043
"""RPLogger class for low-level logging in tests."""
3144

32-
def __init__(self, name, level=0):
45+
def __init__(self, name: str, level: int = 0) -> None:
3346
"""
3447
Initialize RPLogger instance.
3548
@@ -38,7 +51,17 @@ def __init__(self, name, level=0):
3851
"""
3952
super(RPLogger, self).__init__(name, level=level)
4053

41-
def _log(self, level, msg, args, exc_info=None, extra=None, stack_info=False, attachment=None, **kwargs):
54+
def _log(
55+
self,
56+
level: int,
57+
msg: Any,
58+
args: tuple,
59+
exc_info: Optional[Union[bool, tuple]] = None,
60+
extra: Optional[dict] = None,
61+
stack_info: bool = False,
62+
attachment: Optional[dict] = None,
63+
**kwargs: Any,
64+
) -> None:
4265
"""
4366
Low-level logging routine which creates a LogRecord and then calls.
4467
@@ -82,24 +105,21 @@ class RPLogHandler(logging.Handler):
82105
"""RPLogHandler class for logging tests."""
83106

84107
# Map loglevel codes from `logging` module to ReportPortal text names:
85-
_loglevel_map = {
86-
logging.NOTSET: "TRACE",
87-
logging.DEBUG: "DEBUG",
88-
logging.INFO: "INFO",
89-
logging.WARNING: "WARN",
90-
logging.ERROR: "ERROR",
91-
logging.CRITICAL: "ERROR",
92-
}
93-
_sorted_levelnos = sorted(_loglevel_map.keys(), reverse=True)
108+
_loglevel_map: dict[int, str]
109+
_sorted_levelnos: list[int]
110+
filter_client_logs: bool
111+
ignored_record_names: tuple[str, ...]
112+
endpoint: Optional[str]
113+
rp_client: Optional["RP"]
94114

95115
def __init__(
96116
self,
97-
level=logging.NOTSET,
98-
filter_client_logs=False,
99-
endpoint=None,
100-
ignored_record_names=tuple("reportportal_client"),
101-
rp_client=None,
102-
):
117+
level: int = logging.NOTSET,
118+
filter_client_logs: bool = False,
119+
endpoint: Optional[str] = None,
120+
ignored_record_names: tuple = tuple("reportportal_client"),
121+
rp_client: Optional["RP"] = None,
122+
) -> None:
103123
"""
104124
Initialize RPLogHandler instance.
105125
@@ -112,12 +132,14 @@ def __init__(
112132
(with startswith method)
113133
"""
114134
super(RPLogHandler, self).__init__(level)
135+
self._loglevel_map = LOG_LEVEL_MAPPING.copy()
136+
self._sorted_levelnos = sorted(self._loglevel_map.keys(), reverse=True)
115137
self.filter_client_logs = filter_client_logs
116138
self.ignored_record_names = ignored_record_names
117139
self.endpoint = endpoint
118140
self.rp_client = rp_client
119141

120-
def filter(self, record):
142+
def filter(self, record: logging.LogRecord) -> bool:
121143
"""Filter specific records to avoid sending those to RP.
122144
123145
:param record: A log record to be filtered
@@ -131,23 +153,20 @@ def filter(self, record):
131153
if record.name.startswith("urllib3.connectionpool"):
132154
# Filter the reportportal_client requests instance
133155
# urllib3 usage
134-
hostname = urlparse(self.endpoint).hostname
135-
if hostname:
136-
if hasattr(hostname, "decode") and callable(hostname.decode):
137-
if hostname.decode("utf-8") in self.format(record):
138-
return False
139-
else:
140-
if str(hostname) in self.format(record):
156+
if self.endpoint:
157+
hostname = urlparse(self.endpoint).hostname
158+
if hostname:
159+
if hostname in self.format(record):
141160
return False
142161
return True
143162

144-
def _get_rp_log_level(self, levelno):
163+
def _get_rp_log_level(self, levelno: int) -> str:
145164
return next(
146165
(self._loglevel_map[level] for level in self._sorted_levelnos if levelno >= level),
147166
self._loglevel_map[logging.NOTSET],
148167
)
149168

150-
def emit(self, record):
169+
def emit(self, record: logging.LogRecord) -> None:
151170
"""
152171
Emit function.
153172

0 commit comments

Comments
 (0)