Skip to content

Commit

Permalink
Merge pull request #492 from DataDog/s.obregoso/fix_stderrlog
Browse files Browse the repository at this point in the history
Change logging stream to stderr
  • Loading branch information
sobregosodd authored Nov 28, 2024
2 parents e10771a + af679e9 commit cfeeb6b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
14 changes: 7 additions & 7 deletions guarddog/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ def cli(log_level):
"""
logger = logging.getLogger("guarddog")
logger.setLevel(logging.getLevelName(log_level))
stdoutHandler = logging.StreamHandler(stream=sys.stdout)
stdoutHandler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
logger.addHandler(stdoutHandler)
stderrHandler = logging.StreamHandler(stream=sys.stderr)
stderrHandler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
logger.addHandler(stderrHandler)


def _get_all_rules(ecosystem: ECOSYSTEM) -> set[str]:
Expand All @@ -141,7 +141,7 @@ def _get_rule_param(
rule_param = all_rules - set(exclude_rules)

if len(rules) > 0:
print("--rules and --exclude-rules cannot be used together")
log.error("--rules and --exclude-rules cannot be used together")
sys.exit(1)

return rule_param
Expand All @@ -159,7 +159,7 @@ def _verify(
rule_param = _get_rule_param(rules, exclude_rules, ecosystem)
scanner = get_project_scanner(ecosystem)
if scanner is None:
sys.stderr.write(f"Command verify is not supported for ecosystem {ecosystem}")
log.error(f"Command verify is not supported for ecosystem {ecosystem}")
exit(1)

def display_result(result: dict) -> None:
Expand Down Expand Up @@ -211,7 +211,7 @@ def _scan(
rule_param = _get_rule_param(rules, exclude_rules, ecosystem)
scanner = get_package_scanner(ecosystem)
if scanner is None:
sys.stderr.write(f"Command scan is not supported for ecosystem {ecosystem}")
log.error(f"Command scan is not supported for ecosystem {ecosystem}")
sys.exit(1)

result = {"package": identifier}
Expand All @@ -228,7 +228,7 @@ def _scan(
log.debug(f"Considering that '{identifier}' is a remote target")
result |= scanner.scan_remote(identifier, version, rule_param)
except Exception as e:
sys.stderr.write(f"Error occurred while scanning target {identifier}: '{e}'\n")
log.error(f"Error occurred while scanning target {identifier}: '{e}'\n")
sys.exit(1)

if output_format == "json":
Expand Down
3 changes: 1 addition & 2 deletions guarddog/scanners/pypi_project_scanner.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import re
import sys
import pkg_resources
import requests
from packaging.specifiers import Specifier, Version
Expand Down Expand Up @@ -117,7 +116,7 @@ def safe_parse_requirements(req):
except StopIteration:
break
except Exception as e:
sys.stderr.write(
log.error(
f"Error when parsing requirements, received error {str(e)}. This entry will be "
"ignored.\n"
)
Expand Down
9 changes: 4 additions & 5 deletions guarddog/scanners/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def _authenticate_by_access_token(self) -> tuple[str, str]:
user = os.getenv("GIT_USERNAME")
personal_access_token = os.getenv("GH_TOKEN")
if not user or not personal_access_token:
print(
log.error(
"""WARNING: Please set GIT_USERNAME (Github handle) and GH_TOKEN
(generate a personal access token in Github settings > developer)
as environment variables before proceeding."""
Expand Down Expand Up @@ -84,10 +84,9 @@ def scan_single_dependency(dependency, version):
dependencies = self.parse_requirements(requirements)
num_workers = PARALLELISM

sys.stderr.write(
log.info(
f"Scanning using at most {num_workers} parallel worker threads\n"
)
sys.stderr.flush()
with ThreadPoolExecutor(max_workers=num_workers) as pool:
try:
futures: typing.List[concurrent.futures.Future] = []
Expand Down Expand Up @@ -155,7 +154,7 @@ def scan_remote(self, url: str, branch: str, requirements_name: str) -> dict:
if resp.status_code == 200:
return self.scan_requirements(resp.content.decode())
else:
sys.stdout.write(
log.error(
f"{req_url} does not exist. Check your link or branch name."
)
sys.exit(255)
Expand Down Expand Up @@ -191,7 +190,7 @@ def scan_local(
with open(path, "r") as f:
return self.scan_requirements(f.read(), rules, callback)
except Exception as e:
sys.stdout.write(f"Received {e}")
log.error(f"Received {e}")
sys.exit(255)

@abstractmethod
Expand Down

0 comments on commit cfeeb6b

Please sign in to comment.