Skip to content

Commit

Permalink
added log format option
Browse files Browse the repository at this point in the history
  • Loading branch information
xnetcat authored Oct 27, 2023
1 parent 83a10b4 commit b04a864
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion spotdl/console/entry_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def console_entry_point():
# Create settings dicts
spotify_settings, downloader_settings, web_settings = create_settings(arguments)

init_logging(downloader_settings["log_level"])
init_logging(downloader_settings["log_level"], downloader_settings["log_format"])

# If the application is frozen, we check for ffmpeg
# if it's not present download it create config file
Expand Down
2 changes: 2 additions & 0 deletions spotdl/download/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,14 @@ def __init__(
raise DownloaderError(f"Invalid proxy server: {proxy}")
proxies = {"http": proxy, "https": proxy}
logger.info("Setting proxy server: %s", proxy)

GlobalConfig.set_parameter("proxies", proxies)

# Initialize archive
self.url_archive = Archive()
if self.settings["archive"]:
self.url_archive.load(self.settings["archive"])

logger.debug("Archive: %d urls", len(self.url_archive))

logger.debug("Downloader initialized")
Expand Down
2 changes: 2 additions & 0 deletions spotdl/types/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class DownloaderOptions(TypedDict):
ignore_albums: Optional[List[str]]
proxy: Optional[str]
skip_explicit: Optional[bool]
log_format: Optional[str]


class WebOptions(TypedDict):
Expand Down Expand Up @@ -160,6 +161,7 @@ class DownloaderOptionalOptions(TypedDict, total=False):
save_errors: Optional[str]
proxy: Optional[str]
skip_explicit: Optional[bool]
log_format: Optional[str]


class WebOptionalOptions(TypedDict, total=False):
Expand Down
6 changes: 6 additions & 0 deletions spotdl/utils/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,12 @@ def parse_misc_options(parser: _ArgumentGroup):
help="Use a simple tui.",
)

# Add log format argument
parser.add_argument(
"--log-format",
help="Logging format to use. Defaults to `%(message)s`. https://docs.python.org/3/library/logging.html#logrecord-attributes",
)


def parse_other_options(parser: _ArgumentGroup):
"""
Expand Down
1 change: 1 addition & 0 deletions spotdl/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ def get_parameter(cls, key):
"ignore_albums": None,
"proxy": None,
"skip_explicit": False,
"log_format": None,
}

WEB_OPTIONS: WebOptions = {
Expand Down
13 changes: 9 additions & 4 deletions spotdl/utils/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import logging

from typing import Optional

from rich import get_console
from rich.console import ConsoleRenderable
from rich.logging import RichHandler
Expand Down Expand Up @@ -158,7 +160,7 @@ def render_message(
return message_text


def init_logging(log_level: str):
def init_logging(log_level: str, log_format: Optional[str]):
"""
Initialize logging for spotdl.
Expand Down Expand Up @@ -196,9 +198,12 @@ def init_logging(log_level: str):
rich_tracebacks=True,
)

msg_format = "%(message)s"
if log_level == "DEBUG":
msg_format = "%(threadName)s - %(message)s"
if log_format is None:
msg_format = "%(message)s"
if log_level == "DEBUG":
msg_format = "%(threadName)s - %(message)s"
else:
msg_format = log_format

# Add rich handler to spotdl logger
rich_handler.setFormatter(SpotdlFormatter(msg_format))
Expand Down

0 comments on commit b04a864

Please sign in to comment.