Skip to content

Commit

Permalink
logging: handle logging in capa.main
Browse files Browse the repository at this point in the history
  • Loading branch information
fariss committed Sep 19, 2024
1 parent 2d7240b commit 607ff79
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions capa/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import colorama
from pefile import PEFormatError
from rich.logging import RichHandler
from elftools.common.exceptions import ELFError

import capa.perf
Expand Down Expand Up @@ -124,6 +125,7 @@
E_INVALID_FEATURE_EXTRACTOR = 26

logger = logging.getLogger("capa")
logger.propagate = False


class FilterConfig(TypedDict, total=False):
Expand Down Expand Up @@ -402,6 +404,15 @@ def handle_common_args(args):
raises:
ShouldExitError: if the program is invoked incorrectly and should exit.
"""
# use [/] after the logger name to reset any styling,
# and prevent the color from carrying over to the message
LOGFORMAT = "[dim]%(name)s[/]: %(message)s"

# markup=True, to allow the use of Rich's markup syntax in log messages
rich_handler = RichHandler(markup=True, show_time=False)
rich_handler.setFormatter(logging.Formatter(LOGFORMAT))
logger.addHandler(rich_handler)

if args.quiet:
logging.basicConfig(level=logging.WARNING)
logging.getLogger().setLevel(logging.WARNING)
Expand Down

0 comments on commit 607ff79

Please sign in to comment.