Skip to content

Commit

Permalink
Merge pull request #5 from willyw0nka/logging
Browse files Browse the repository at this point in the history
fix: update logging
  • Loading branch information
willyw0nka authored Apr 16, 2024
2 parents f48bebf + 641ceb9 commit 8faa647
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
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

0 comments on commit 8faa647

Please sign in to comment.