diff --git a/pygrype/grype.py b/pygrype/grype.py index bf3dae0..962897a 100644 --- a/pygrype/grype.py +++ b/pygrype/grype.py @@ -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. @@ -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. @@ -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, diff --git a/pygrype/logging.py b/pygrype/logging.py new file mode 100644 index 0000000..ed5df71 --- /dev/null +++ b/pygrype/logging.py @@ -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