Skip to content

Commit

Permalink
Log instamatic debug if -v, other debug if -vv, paths if -vvv
Browse files Browse the repository at this point in the history
  • Loading branch information
Baharis committed Mar 4, 2025
1 parent 0e784ba commit d3a67fe
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/instamatic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pprint
import sys
from pathlib import Path
from typing import Callable

import instamatic
from instamatic import config
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d3a67fe

Please sign in to comment.