Skip to content

Commit

Permalink
Fix: all logs going to stderr (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes11833 authored Jan 6, 2025
1 parent 5576333 commit ce6c776
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion rclone_python/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = "0.1.19"
VERSION = "0.1.20"
7 changes: 7 additions & 0 deletions rclone_python/logs.py
Original file line number Diff line number Diff line change
@@ -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__)
24 changes: 14 additions & 10 deletions rclone_python/rclone.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import json
import re
import logging
from functools import wraps
from shutil import which
from typing import Optional, Tuple, Union, List, Dict, Callable

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):
Expand All @@ -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.
Expand Down Expand Up @@ -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."
)

Expand Down Expand Up @@ -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}"
)

Expand Down Expand Up @@ -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",
Expand Down
9 changes: 3 additions & 6 deletions rclone_python/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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", "<Error message missing>")
errors.append((obj + ": " if obj else "") + msg)
logger.warning(f"Rclone omitted an error: {update_dict}")

if show_progress:
if process.wait() == 0:
Expand Down

0 comments on commit ce6c776

Please sign in to comment.