From d3a67fe7971d47a5feba963ca64c6460b2831090 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Tcho=C5=84?= Date: Tue, 4 Mar 2025 13:06:59 +0100 Subject: [PATCH] Log instamatic debug if -v, other debug if -vv, paths if -vvv --- src/instamatic/main.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/instamatic/main.py b/src/instamatic/main.py index 5802cd79..d37998df 100644 --- a/src/instamatic/main.py +++ b/src/instamatic/main.py @@ -6,6 +6,7 @@ import pprint import sys from pathlib import Path +from typing import Callable import instamatic from instamatic import config @@ -120,9 +121,10 @@ def main(): parser.add_argument( '-v', '--verbose', - action='store_true', - dest='verbose', - help="Show imported packages' DEBUG records in log", + action='count', + dest='verbosity', + help='Write debug messages of instamatic (-v) and other imported packages (-vv) to the log', + default=0, ) parser.set_defaults( @@ -163,20 +165,25 @@ def main(): date = datetime.datetime.now().strftime('%Y-%m-%d') logfile = config.locations['logs'] / f'instamatic_{date}.log' - def filter_out_imported_package_debug_records(r: logging.LogRecord) -> bool: - return ( - r.name.startswith('instamatic') - or r.name == '__main__' - or r.levelno > logging.DEBUG - or options.verbose - ) + def log_filter_factory(verbosity: int) -> Callable[[logging.LogRecord], bool]: + instamatic_logging_level = logging.DEBUG if verbosity >= 1 else logging.INFO + imported_logging_level = logging.DEBUG if verbosity >= 2 else logging.INFO + + def log_filter_function(r: logging.LogRecord) -> bool: + if r.name.startswith('instamatic') or r.name == '__main__': + return r.levelno >= instamatic_logging_level + else: + return r.levelno >= imported_logging_level + + return log_filter_function log_main = logging.getLogger() log_main.setLevel(logging.DEBUG) - log_format = '%(asctime)s | %(module)s:%(lineno)s | %(levelname)s | %(message)s' + log_detail = 'module' if options.verbose <= 2 else 'pathname' + log_format = f'%(asctime)s | %({log_detail})s:%(lineno)s | %(levelname)s | %(message)s' log_handler = logging.FileHandler(logfile) log_handler.setFormatter(logging.Formatter(log_format)) - log_handler.addFilter(filter_out_imported_package_debug_records) + log_handler.addFilter(log_filter_factory(verbosity=options.verbosity)) log_main.addHandler(log_handler) logging.captureWarnings(True)