Skip to content

Commit 51e8961

Browse files
authored
CM-59965 add additional logging for SCA verbose mode (#392)
1 parent f55dfbe commit 51e8961

4 files changed

Lines changed: 61 additions & 6 deletions

File tree

cycode/cli/files_collector/sca/base_restore_dependencies.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from cycode.cli.models import Document
88
from cycode.cli.utils.path_utils import get_file_content, get_file_dir, get_path_from_context, join_paths
99
from cycode.cli.utils.shell_executor import shell
10+
from cycode.logger import get_logger
11+
12+
logger = get_logger('SCA Restore')
1013

1114

1215
def build_dep_tree_path(path: str, generated_file_name: str) -> str:
@@ -19,6 +22,16 @@ def execute_commands(
1922
output_file_path: Optional[str] = None,
2023
working_directory: Optional[str] = None,
2124
) -> Optional[str]:
25+
logger.debug(
26+
'Executing restore commands, %s',
27+
{
28+
'commands_count': len(commands),
29+
'timeout_sec': timeout,
30+
'working_directory': working_directory,
31+
'output_file_path': output_file_path,
32+
},
33+
)
34+
2235
try:
2336
outputs = []
2437

@@ -32,7 +45,8 @@ def execute_commands(
3245
if output_file_path:
3346
with open(output_file_path, 'w', encoding='UTF-8') as output_file:
3447
output_file.writelines(joined_output)
35-
except Exception:
48+
except Exception as e:
49+
logger.debug('Unexpected error during command execution', exc_info=e)
3650
return None
3751

3852
return joined_output
@@ -75,8 +89,21 @@ def try_restore_dependencies(self, document: Document) -> Optional[Document]:
7589
)
7690
if output is None: # one of the commands failed
7791
return None
92+
else:
93+
logger.debug(
94+
'Lock file already exists, skipping restore commands, %s',
95+
{'restore_file_path': restore_file_path},
96+
)
7897

7998
restore_file_content = get_file_content(restore_file_path)
99+
logger.debug(
100+
'Restore file loaded, %s',
101+
{
102+
'restore_file_path': restore_file_path,
103+
'content_size': len(restore_file_content) if restore_file_content else 0,
104+
'content_empty': not restore_file_content,
105+
},
106+
)
80107
return Document(relative_restore_file_path, restore_file_content, self.is_git_diff)
81108

82109
def get_working_directory(self, document: Document) -> Optional[str]:

cycode/cli/files_collector/sca/go/restore_go_dependencies.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import typer
55

66
from cycode.cli.files_collector.sca.base_restore_dependencies import BaseRestoreDependencies
7-
from cycode.cli.logger import logger
87
from cycode.cli.models import Document
8+
from cycode.logger import get_logger
9+
10+
logger = get_logger('Go Restore Dependencies')
911

1012
GO_PROJECT_FILE_EXTENSIONS = ['.mod', '.sum']
1113
GO_RESTORE_FILE_NAME = 'go.mod.graph'

cycode/cli/files_collector/sca/sca_file_collector.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,17 @@ def _try_restore_dependencies(
106106

107107
restore_dependencies_document = restore_dependencies.restore(document)
108108
if restore_dependencies_document is None:
109-
logger.warning('Error occurred while trying to generate dependencies tree, %s', {'filename': document.path})
109+
logger.warning(
110+
'Error occurred while trying to generate dependencies tree, %s',
111+
{'filename': document.path, 'handler': type(restore_dependencies).__name__},
112+
)
110113
return None
111114

112115
if restore_dependencies_document.content is None:
113-
logger.warning('Error occurred while trying to generate dependencies tree, %s', {'filename': document.path})
116+
logger.warning(
117+
'Error occurred while trying to generate dependencies tree, %s',
118+
{'filename': document.path, 'handler': type(restore_dependencies).__name__},
119+
)
114120
restore_dependencies_document.content = ''
115121
else:
116122
is_monitor_action = ctx.obj.get('monitor', False)
@@ -124,6 +130,13 @@ def _try_restore_dependencies(
124130

125131
def _get_restore_handlers(ctx: typer.Context, is_git_diff: bool) -> list[BaseRestoreDependencies]:
126132
build_dep_tree_timeout = int(os.getenv('CYCODE_BUILD_DEP_TREE_TIMEOUT_SECONDS', BUILD_DEP_TREE_TIMEOUT))
133+
logger.debug(
134+
'SCA restore handler timeout, %s',
135+
{
136+
'timeout_sec': build_dep_tree_timeout,
137+
'source': 'env' if os.getenv('CYCODE_BUILD_DEP_TREE_TIMEOUT_SECONDS') else 'default',
138+
},
139+
)
127140
return [
128141
RestoreGradleDependencies(ctx, is_git_diff, build_dep_tree_timeout),
129142
RestoreMavenDependencies(ctx, is_git_diff, build_dep_tree_timeout),

cycode/cli/utils/shell_executor.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import subprocess
2+
import time
23
from typing import Optional, Union
34

45
import click
@@ -21,15 +22,27 @@ def shell(
2122
logger.debug('Executing shell command: %s', command)
2223

2324
try:
25+
start = time.monotonic()
2426
result = subprocess.run( # noqa: S603
2527
command, cwd=working_directory, timeout=timeout, check=True, capture_output=True
2628
)
27-
logger.debug('Shell command executed successfully')
29+
duration_sec = round(time.monotonic() - start, 2)
30+
stdout = result.stdout.decode('UTF-8').strip()
31+
stderr = result.stderr.decode('UTF-8').strip()
2832

29-
return result.stdout.decode('UTF-8').strip()
33+
logger.debug(
34+
'Shell command executed successfully, %s',
35+
{'duration_sec': duration_sec, 'stdout': stdout if stdout else '', 'stderr': stderr if stderr else ''},
36+
)
37+
38+
return stdout
3039
except subprocess.CalledProcessError as e:
3140
if not silent_exc_info:
3241
logger.debug('Error occurred while running shell command', exc_info=e)
42+
if e.stdout:
43+
logger.debug('Shell command stdout: %s', e.stdout.decode('UTF-8').strip())
44+
if e.stderr:
45+
logger.debug('Shell command stderr: %s', e.stderr.decode('UTF-8').strip())
3346
except subprocess.TimeoutExpired as e:
3447
logger.debug('Command timed out', exc_info=e)
3548
raise typer.Abort(f'Command "{command}" timed out') from e

0 commit comments

Comments
 (0)