diff options
| author | Cody Logan <cody@lokken.dev> | 2023-10-24 11:00:56 -0700 |
|---|---|---|
| committer | Cody Logan <cody@lokken.dev> | 2023-10-24 11:04:54 -0700 |
| commit | 93ed73f700e1f6da32418b0188c2a8f8122f1c0d (patch) | |
| tree | 6a179b38aee4fc6ebc41294fedc2d1f91b4fb0d8 /src | |
| parent | 0b700624f79461a0d9f52eeb0370d15f16d49c90 (diff) | |
| download | wikiget-93ed73f700e1f6da32418b0188c2a8f8122f1c0d.tar.gz wikiget-93ed73f700e1f6da32418b0188c2a8f8122f1c0d.zip | |
Use per-module loggers and tweak initial log config
Diffstat (limited to 'src')
| -rw-r--r-- | src/wikiget/dl.py | 31 | ||||
| -rw-r--r-- | src/wikiget/logging.py | 32 | ||||
| -rw-r--r-- | src/wikiget/parse.py | 4 | ||||
| -rw-r--r-- | src/wikiget/wikiget.py | 10 |
4 files changed, 41 insertions, 36 deletions
diff --git a/src/wikiget/dl.py b/src/wikiget/dl.py index 5b5b43b..f569347 100644 --- a/src/wikiget/dl.py +++ b/src/wikiget/dl.py @@ -33,10 +33,12 @@ from wikiget.logging import FileLogAdapter from wikiget.parse import get_dest from wikiget.validations import verify_hash +logger = logging.getLogger(__name__) + def query_api(filename: str, site_name: str, args: Namespace) -> Image: # connect to site and identify ourselves - logging.info(f"Connecting to {site_name}") + logger.info(f"Connecting to {site_name}") try: site = Site(site_name, path=args.path, clients_useragent=wikiget.USER_AGENT) if args.username and args.password: @@ -44,21 +46,21 @@ def query_api(filename: str, site_name: str, args: Namespace) -> Image: except ConnectionError as e: # usually this means there is no such site, or there's no network connection, # though it could be a certificate problem - logging.error("Could not connect to specified site") - logging.debug(e) + logger.error("Could not connect to specified site") + logger.debug(e) raise except HTTPError as e: # most likely a 403 forbidden or 404 not found error for api.php - logging.error( + logger.error( "Could not find the specified wiki's api.php. Check the value of --path." ) - logging.debug(e) + logger.debug(e) raise except (InvalidResponse, LoginError) as e: # InvalidResponse: site exists, but we couldn't communicate with the API # endpoint for some reason other than an HTTP error. # LoginError: missing or invalid credentials - logging.error(e) + logger.error(e) raise # get info about the target file @@ -67,11 +69,11 @@ def query_api(filename: str, site_name: str, args: Namespace) -> Image: except APIError as e: # an API error at this point likely means access is denied, which could happen # with a private wiki - logging.error( + logger.error( "Access denied. Try providing credentials with --username and --password." ) for i in e.args: - logging.debug(i) + logger.debug(i) raise return image @@ -88,13 +90,13 @@ def batch_download(args: Namespace) -> int: dl_list = {} errors = 0 - logging.info(f"Using batch file '{input_file}'.") + logger.info(f"Using batch file '{input_file}'.") try: fd = open(input_file) except OSError as e: - logging.error("File could not be read. The following error was encountered:") - logging.error(e) + logger.error("File could not be read. The following error was encountered:") + logger.error(e) sys.exit(1) else: with fd: @@ -110,15 +112,15 @@ def batch_download(args: Namespace) -> int: futures = [] for line_num, line in dl_list.items(): # keep track of batch file line numbers for debugging/logging purposes - logging.info(f"Processing '{line}' at line {line_num}") + logger.info(f"Processing '{line}' at line {line_num}") try: file = prep_download(line, args) except ParseError as e: - logging.warning(f"{e} (line {line_num})") + logger.warning(f"{e} (line {line_num})") errors += 1 continue except (ConnectionError, HTTPError, InvalidResponse, LoginError, APIError): - logging.warning( + logger.warning( f"Unable to download '{line}' (line {line_num}) due to an error" ) errors += 1 @@ -139,7 +141,6 @@ def download(f: File, args: Namespace) -> int: errors = 0 - logger = logging.getLogger("") adapter = FileLogAdapter(logger, {"filename": filename}) if file.exists: diff --git a/src/wikiget/logging.py b/src/wikiget/logging.py index 290fd1d..425b045 100644 --- a/src/wikiget/logging.py +++ b/src/wikiget/logging.py @@ -38,20 +38,20 @@ def configure_logging(verbosity: int, logfile: str, *, quiet: bool) -> None: # configure logging: # console log level is set via -v, -vv, and -q options; # file log level is always debug (TODO: make this user configurable) - base_format = "%(message)s" - log_format = "[%(levelname)s] " + base_format + console_log_format = "[%(levelname)s] %(message)s" + file_log_format = "%(asctime)s [%(levelname)-7s] %(message)s" + + logger = logging.getLogger("") # root logger + + # set up console logging + ch = logging.StreamHandler() + ch.setLevel(loglevel) + ch.setFormatter(logging.Formatter(console_log_format)) + logger.addHandler(ch) + if logfile: - # log to console and file - logging.basicConfig( - level=logging.DEBUG, - format="%(asctime)s [%(levelname)-7s] " + base_format, - filename=logfile, - ) - - console = logging.StreamHandler() - console.setLevel(loglevel) - console.setFormatter(logging.Formatter(log_format)) - logging.getLogger("").addHandler(console) - else: - # log only to console - logging.basicConfig(level=loglevel, format=log_format) + # also log to file + fh = logging.FileHandler(logfile) + fh.setLevel(logging.DEBUG) + fh.setFormatter(logging.Formatter(file_log_format)) + logger.addHandler(fh) diff --git a/src/wikiget/parse.py b/src/wikiget/parse.py index fe3fe43..52cc262 100644 --- a/src/wikiget/parse.py +++ b/src/wikiget/parse.py @@ -24,6 +24,8 @@ from wikiget.exceptions import ParseError from wikiget.file import File from wikiget.validations import valid_file +logger = logging.getLogger(__name__) + def get_dest(dl: str, args: Namespace) -> File: url = urlparse(dl) @@ -34,7 +36,7 @@ def get_dest(dl: str, args: Namespace) -> File: if args.site is not wikiget.DEFAULT_SITE: # this will work even if the user specifies 'commons.wikimedia.org' since # we're comparing objects instead of values (is not vs. !=) - logging.warning("Target is a URL, ignoring site specified with --site") + logger.warning("Target is a URL, ignoring site specified with --site") else: filename = dl site_name = args.site diff --git a/src/wikiget/wikiget.py b/src/wikiget/wikiget.py index aee493f..cf877cf 100644 --- a/src/wikiget/wikiget.py +++ b/src/wikiget/wikiget.py @@ -27,6 +27,8 @@ from wikiget.dl import batch_download, download, prep_download from wikiget.exceptions import ParseError from wikiget.logging import configure_logging +logger = logging.getLogger(__name__) + def construct_parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser( @@ -123,8 +125,8 @@ def main() -> None: # log events are appended to the file if it already exists, so note the start of a # new download session - logging.info(f"Starting download session using wikiget {wikiget.wikiget_version}") - logging.debug(f"User agent: {wikiget.USER_AGENT}") + logger.info(f"Starting download session using wikiget {wikiget.wikiget_version}") + logger.debug(f"User agent: {wikiget.USER_AGENT}") if args.batch: # batch download mode @@ -132,7 +134,7 @@ def main() -> None: if errors: # return non-zero exit code if any problems were encountered, even if some # downloads completed successfully - logging.warning( + logger.warning( f"{errors} problem{'s'[:errors^1]} encountered during batch processing" ) sys.exit(1) @@ -141,7 +143,7 @@ def main() -> None: try: file = prep_download(args.FILE, args) except ParseError as e: - logging.error(e) + logger.error(e) sys.exit(1) except (ConnectionError, HTTPError, InvalidResponse, LoginError, APIError): sys.exit(1) |
