Skip to content

Commit 7f43ba4

Browse files
committed
CM-59965 add additional logging for SCA verbose mode (cycodehq#392)
1 parent a9d93b1 commit 7f43ba4

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

cycode/cli/files_collector/sca/base_restore_dependencies.py

Lines changed: 28 additions & 2 deletions
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,9 +22,18 @@ 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
if not commands:
2336
return None
24-
2537
try:
2638
outputs = []
2739

@@ -35,7 +47,8 @@ def execute_commands(
3547
if output_file_path:
3648
with open(output_file_path, 'w', encoding='UTF-8') as output_file:
3749
output_file.writelines(joined_output)
38-
except Exception:
50+
except Exception as e:
51+
logger.debug('Unexpected error during command execution', exc_info=e)
3952
return None
4053

4154
return joined_output
@@ -78,8 +91,21 @@ def try_restore_dependencies(self, document: Document) -> Optional[Document]:
7891
)
7992
if output is None: # one of the commands failed
8093
return None
94+
else:
95+
logger.debug(
96+
'Lock file already exists, skipping restore commands, %s',
97+
{'restore_file_path': restore_file_path},
98+
)
8199

82100
restore_file_content = get_file_content(restore_file_path)
101+
logger.debug(
102+
'Restore file loaded, %s',
103+
{
104+
'restore_file_path': restore_file_path,
105+
'content_size': len(restore_file_content) if restore_file_content else 0,
106+
'content_empty': not restore_file_content,
107+
},
108+
)
83109
return Document(relative_restore_file_path, restore_file_content, self.is_git_diff)
84110

85111
def get_manifest_dir(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
@@ -112,11 +112,17 @@ def _try_restore_dependencies(
112112

113113
restore_dependencies_document = restore_dependencies.restore(document)
114114
if restore_dependencies_document is None:
115-
logger.warning('Error occurred while trying to generate dependencies tree, %s', {'filename': document.path})
115+
logger.warning(
116+
'Error occurred while trying to generate dependencies tree, %s',
117+
{'filename': document.path, 'handler': type(restore_dependencies).__name__},
118+
)
116119
return None
117120

118121
if restore_dependencies_document.content is None:
119-
logger.warning('Error occurred while trying to generate dependencies tree, %s', {'filename': document.path})
122+
logger.warning(
123+
'Error occurred while trying to generate dependencies tree, %s',
124+
{'filename': document.path, 'handler': type(restore_dependencies).__name__},
125+
)
120126
restore_dependencies_document.content = ''
121127
else:
122128
is_monitor_action = ctx.obj.get('monitor', False)
@@ -130,6 +136,13 @@ def _try_restore_dependencies(
130136

131137
def _get_restore_handlers(ctx: typer.Context, is_git_diff: bool) -> list[BaseRestoreDependencies]:
132138
build_dep_tree_timeout = int(os.getenv('CYCODE_BUILD_DEP_TREE_TIMEOUT_SECONDS', BUILD_DEP_TREE_TIMEOUT))
139+
logger.debug(
140+
'SCA restore handler timeout, %s',
141+
{
142+
'timeout_sec': build_dep_tree_timeout,
143+
'source': 'env' if os.getenv('CYCODE_BUILD_DEP_TREE_TIMEOUT_SECONDS') else 'default',
144+
},
145+
)
133146
return [
134147
RestoreGradleDependencies(ctx, is_git_diff, build_dep_tree_timeout),
135148
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)