diff --git a/README.md b/README.md index 9e5d5de..89ce394 100644 --- a/README.md +++ b/README.md @@ -150,10 +150,10 @@ Copying data to data1 ━━━━━━╸━━━━━━━━━━━━ └─another.mp4 ━╸━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4% 1.4 MB/s ``` -## Debug -For debugging progress related functionality, set the DEBUG flag to true: +## Set the log level +The log level can be set using: ```python -rclone.DEBUG = True +rclone.set_log_level(logging.DEBUG) ``` This will make the wrapper print the raw rclone progress. diff --git a/rclone_python/__init__.py b/rclone_python/__init__.py index 775065b..c635a98 100644 --- a/rclone_python/__init__.py +++ b/rclone_python/__init__.py @@ -1 +1 @@ -VERSION = "0.1.19" +VERSION = "0.1.20" diff --git a/rclone_python/logs.py b/rclone_python/logs.py new file mode 100644 index 0000000..a6e112b --- /dev/null +++ b/rclone_python/logs.py @@ -0,0 +1,7 @@ +import logging +from rich.logging import RichHandler + + +FORMAT = "%(message)s" +logging.basicConfig(format=FORMAT, datefmt="[%X]", handlers=[RichHandler()]) +logger = logging.getLogger(__name__) diff --git a/rclone_python/rclone.py b/rclone_python/rclone.py index 43a9538..8fe1689 100644 --- a/rclone_python/rclone.py +++ b/rclone_python/rclone.py @@ -1,6 +1,5 @@ import json import re -import logging from functools import wraps from shutil import which from typing import Optional, Tuple, Union, List, Dict, Callable @@ -8,10 +7,7 @@ from rclone_python import utils from rclone_python.hash_types import HashTypes from rclone_python.remote_types import RemoteTypes - - -# debug flag enables/disables raw output of rclone progresses in the terminal -DEBUG = False +from rclone_python.logs import logger def __check_installed(func): @@ -27,6 +23,15 @@ def wrapper(*args, **kwargs): return wrapper +def set_log_level(level: int): + """Change the log level of this wrapper. + + Args: + level (int): The log level to use. + """ + logger.setLevel(level) + + def is_installed() -> bool: """ :return: True if rclone is correctly installed on the system. @@ -92,12 +97,12 @@ def create_remote( command = f'config create "{remote_name}" "{remote_type}"' if client_id and client_secret: - logging.info("Using the provided client id and client secret.") + logger.info("Using the provided client id and client secret.") kwargs["client_id"] = client_id kwargs["client_secret"] = client_secret else: - logging.warning( + logger.warning( "The drive client id and the client secret have not been set. Using defaults." ) @@ -614,7 +619,7 @@ def version( beta = beta[0] if beta else None if not latest or not beta: - logging.warning( + logger.warning( f"Failed to get latest rclone versions. The following error was output by rclone:\n{stderr}" ) @@ -671,12 +676,11 @@ def _rclone_transfer_operation( prog_title, listener=listener, show_progress=show_progress, - debug=DEBUG, pbar=pbar, ) if process.wait() == 0: - logging.info("Cloud upload completed.") + logger.info("Cloud upload completed.") else: raise utils.RcloneException( description=f"{command_descr} from {in_path} to {out_path} failed", diff --git a/rclone_python/utils.py b/rclone_python/utils.py index 6e5447c..782b28f 100644 --- a/rclone_python/utils.py +++ b/rclone_python/utils.py @@ -3,6 +3,7 @@ from typing import Any, Callable, Dict, List, Optional, Tuple, Union from rich.progress import Progress, TaskID, Task from pathlib import Path +from rclone_python.logs import logger from rich.progress import ( Progress, @@ -92,7 +93,6 @@ def rclone_progress( pbar_title: str, show_progress=True, listener: Callable[[Dict], None] = None, - debug=False, pbar: Optional[Progress] = None, ) -> Tuple[subprocess.Popen, List[str]]: total_progress_id = None @@ -123,17 +123,14 @@ def rclone_progress( if listener: listener(update_dict) - if debug: - if show_progress: - pbar.log(line) - else: - print(line) + logger.debug(line) else: if update_dict is not None: obj = update_dict.get("object", "") msg = update_dict.get("msg", "") errors.append((obj + ": " if obj else "") + msg) + logger.warning(f"Rclone omitted an error: {update_dict}") if show_progress: if process.wait() == 0: