Skip to content

Commit f451f3e

Browse files
authored
PYSCAN-25: Replace the usage of print with a proper logger (#4)
1 parent 8d05de8 commit f451f3e

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,5 @@ addopts = [
7676
]
7777

7878
[tool.black]
79-
line-length = 89
79+
line-length = 120
8080
target-version = ["py38", "py39", "py310", "py311", "py312"]

src/py_sonar_scanner/scanner.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
# along with this program; if not, write to the Free Software Foundation,
1818
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
#
20+
import logging
21+
from logging import Logger
2022
import threading
2123
from threading import Thread
2224
from subprocess import Popen, PIPE
@@ -26,14 +28,23 @@
2628

2729
class Scanner:
2830
cfg: Configuration
31+
log: Logger
2932

3033
def __init__(self, cfg: Configuration):
3134
self.cfg = cfg
35+
self.log = logging.getLogger(__name__)
36+
self._setup_logger(self.log)
37+
38+
def _setup_logger(self, log: Logger):
39+
log.setLevel(logging.INFO)
40+
handler = logging.StreamHandler()
41+
handler.terminator = ""
42+
log.addHandler(handler)
3243

3344
def scan(self):
3445
process = self.execute_command()
35-
output_thread = threading.Thread(target=self._print_output, args=(process.stdout,))
36-
error_thread = threading.Thread(target=self._print_output, args=(process.stderr,))
46+
output_thread = threading.Thread(target=self._log_output, args=(process.stdout,))
47+
error_thread = threading.Thread(target=self._log_output, args=(process.stderr,))
3748
return self.process_output(output_thread, error_thread, process)
3849

3950
def process_output(self, output_thread: Thread, error_thread: Thread, process: Popen) -> int:
@@ -45,16 +56,16 @@ def process_output(self, output_thread: Thread, error_thread: Thread, process: P
4556

4657
return process.returncode
4758

59+
def execute_command(self) -> Popen:
60+
cmd = self.compute_command()
61+
return Popen(cmd, stdout=PIPE, stderr=PIPE)
62+
4863
def compute_command(self) -> list[str]:
4964
if not self.cfg.sonar_scanner_executable_path:
5065
raise ValueError("No executable path provided")
5166
return [self.cfg.sonar_scanner_executable_path] + self.cfg.scan_arguments
5267

53-
def execute_command(self) -> Popen:
54-
cmd = self.compute_command()
55-
return Popen(cmd, stdout=PIPE, stderr=PIPE)
56-
57-
def _print_output(self, stream: list[bytes]):
68+
def _log_output(self, stream: list[bytes]):
5869
for line in stream:
59-
decoded_line = line.decode('utf-8')
60-
print(decoded_line, end='', flush=True)
70+
decoded_line = line.decode("utf-8")
71+
self.log.info(decoded_line)

0 commit comments

Comments
 (0)