Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update logging #5

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pygrype/grype.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
from pygrype.core.grype_version import GrypeVersion
from pygrype.core.scan.scan import Scan
from pygrype.grype_db import _GrypeDB

from pygrype.logging import get_logger

class Grype:
"""A class representing the Grype vulnerability scanner."""

path: str
db: _GrypeDB
logger: logging.Logger = get_logger()

def __init__(self, path: str = 'grype') -> None:
"""Initialize the Grype object.
Expand All @@ -27,12 +28,12 @@ def __init__(self, path: str = 'grype') -> None:
Exception: If Grype is not found at the specified path.
"""
if not shutil.which(path):
logging.error(f'Grype was not found at: {path}')
self.logger.error(f'Grype was not found at: {path}')
raise Exception(f'Grype was not found at: {path}')
self.path = path
self.db = _GrypeDB(self.path)

logging.info(f'Using Grype {self.version().version}')
self.logger.info(f'Using Grype {self.version().version}')

def version(self) -> GrypeVersion:
"""Get the version of Grype.
Expand Down Expand Up @@ -111,7 +112,7 @@ def scan(self, target: str, add_cpes_if_none: bool = False, by_cve: bool = False
if show_supressed:
args.append('--show-supressed')

logging.debug(f'Running: {args}')
self.logger.debug(f'Running: {args}')

process = subprocess.run(
args=args,
Expand Down
14 changes: 14 additions & 0 deletions pygrype/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import logging
import sys

def get_logger() -> logging.Logger:
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)

return logger
Loading