Skip to content

Commit

Permalink
logging: configure root logger to use RichHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
fariss committed Sep 26, 2024
1 parent 596a5bb commit 33249f5
Showing 1 changed file with 10 additions and 34 deletions.
44 changes: 10 additions & 34 deletions capa/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,23 +125,6 @@
E_INVALID_INPUT_FORMAT = 25
E_INVALID_FEATURE_EXTRACTOR = 26

root_logger = logging.getLogger()
logging.basicConfig(level=logging.NOTSET)

# 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"

if root_logger.hasHandlers():
root_logger.handlers.clear()

# markup=True, to allow the use of Rich's markup syntax in log messages
rich_handler = RichHandler(
level=logging.NOTSET, markup=True, show_time=False, show_path=True, console=capa.helpers.log_console
)
rich_handler.setFormatter(logging.Formatter(LOGFORMAT))
root_logger.addHandler(rich_handler)

logger = logging.getLogger("capa")


Expand Down Expand Up @@ -422,33 +405,26 @@ def handle_common_args(args):
raises:
ShouldExitError: if the program is invoked incorrectly and should exit.
"""

if args.quiet:
logging.basicConfig(level=logging.WARNING)
logging.getLogger().setLevel(logging.WARNING)
elif args.debug:
logging.basicConfig(level=logging.DEBUG)
logging.getLogger().setLevel(logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
logging.getLogger().setLevel(logging.INFO)

# disable vivisect-related logging, it's verbose and not relevant for capa users
set_vivisect_log_level(logging.CRITICAL)

# logger = logging.getLogger()
# 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"

# # 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"
# set markup=True to allow the use of Rich's markup syntax in log messages
rich_handler = RichHandler(markup=True, show_time=False, show_path=True, console=capa.helpers.log_console)
rich_handler.setFormatter(logging.Formatter(logformat))

# if logger.hasHandlers():
# logger.handlers.clear()
# use RichHandler for root logger
logging.getLogger().addHandler(rich_handler)

# # markup=True, to allow the use of Rich's markup syntax in log messages
# rich_handler = RichHandler(level=logging.NOTSET, markup=True, show_time=False, show_path=False, console=Console(stderr=True))
# rich_handler.setFormatter(logging.Formatter(LOGFORMAT))
# logger.addHandler(rich_handler)
# disable vivisect-related logging, it's verbose and not relevant for capa users
set_vivisect_log_level(logging.CRITICAL)

if isinstance(sys.stdout, io.TextIOWrapper) or hasattr(sys.stdout, "reconfigure"):
# from sys.stdout type hint:
Expand Down

0 comments on commit 33249f5

Please sign in to comment.