proper logger
This commit is contained in:
parent
1e2cf14cdf
commit
ef8f46ac30
4 changed files with 68 additions and 5 deletions
|
|
@ -69,6 +69,15 @@ class SubscriptionValidator(StrictDictValidator):
|
|||
preset_options=self.preset,
|
||||
)
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""
|
||||
Returns
|
||||
-------
|
||||
Name of the subscription
|
||||
"""
|
||||
return self._name
|
||||
|
||||
@classmethod
|
||||
def from_dict(
|
||||
cls, config: ConfigFile, subscription_name: str, subscription_dict: Dict
|
||||
|
|
|
|||
47
src/ytdl_sub/logging/__init__.py
Normal file
47
src/ytdl_sub/logging/__init__.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import logging
|
||||
import sys
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class YtdlSubLogger:
|
||||
@classmethod
|
||||
def _get_formatter(cls) -> logging.Formatter:
|
||||
"""
|
||||
Returns
|
||||
-------
|
||||
Formatter for all ytdl-sub loggers
|
||||
"""
|
||||
return logging.Formatter("[%(name)s] %(message)s")
|
||||
|
||||
@classmethod
|
||||
def _get_handler(cls) -> logging.StreamHandler:
|
||||
"""
|
||||
Returns
|
||||
-------
|
||||
Logger handler
|
||||
"""
|
||||
handler = logging.StreamHandler(sys.stdout)
|
||||
handler.setLevel(logging.INFO)
|
||||
handler.setFormatter(cls._get_formatter())
|
||||
return handler
|
||||
|
||||
@classmethod
|
||||
def logger(cls, name: Optional[str] = None) -> logging.Logger:
|
||||
"""
|
||||
Parameters
|
||||
----------
|
||||
name
|
||||
Optional. Name of the logger which is included in the prefix like [ytdl-sub:<name>].
|
||||
If None, the prefix is just [ytdl-sub]
|
||||
|
||||
Returns
|
||||
-------
|
||||
A configured logger
|
||||
"""
|
||||
logger_name = "ytdl-sub"
|
||||
if name:
|
||||
logger_name += f":{name}"
|
||||
|
||||
logger = logging.Logger(name=logger_name)
|
||||
logger.addHandler(cls._get_handler())
|
||||
return logger
|
||||
|
|
@ -7,10 +7,13 @@ from ytdl_sub.cli.download_args_parser import DownloadArgsParser
|
|||
from ytdl_sub.cli.main_args_parser import parser
|
||||
from ytdl_sub.config.config_file import ConfigFile
|
||||
from ytdl_sub.config.subscription import SubscriptionValidator
|
||||
from ytdl_sub.logging import YtdlSubLogger
|
||||
from ytdl_sub.utils.exceptions import ValidationException
|
||||
|
||||
DEBUGGER_MODE = True
|
||||
|
||||
logger = YtdlSubLogger.logger()
|
||||
|
||||
|
||||
def _download_subscriptions_from_yaml_files(config: ConfigFile, args: argparse.Namespace) -> None:
|
||||
"""
|
||||
|
|
@ -28,6 +31,7 @@ def _download_subscriptions_from_yaml_files(config: ConfigFile, args: argparse.N
|
|||
)
|
||||
|
||||
for subscription in subscriptions:
|
||||
logger.info("Beginning subscription download for %s", subscription.name)
|
||||
subscription.to_subscription().download()
|
||||
|
||||
|
||||
|
|
@ -56,12 +60,12 @@ def main():
|
|||
config: ConfigFile = ConfigFile.from_file_path(args.config)
|
||||
if args.subparser == "sub":
|
||||
_download_subscriptions_from_yaml_files(config=config, args=args)
|
||||
print("Subscription download complete!")
|
||||
logger.info("Subscription download complete!")
|
||||
|
||||
# One-off download
|
||||
if args.subparser == "dl":
|
||||
_download_subscription_from_cli(config=config, extra_args=extra_args)
|
||||
print("Download complete!")
|
||||
logger.info("Download complete!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
@ -70,7 +74,7 @@ if __name__ == "__main__":
|
|||
except ValidationException as validation_exception:
|
||||
if DEBUGGER_MODE:
|
||||
raise
|
||||
print(validation_exception)
|
||||
logger.error(validation_exception)
|
||||
sys.exit(1)
|
||||
except Exception as exc: # pylint: disable=broad-except
|
||||
if DEBUGGER_MODE:
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ from typing import Set
|
|||
from yt_dlp import DateRange
|
||||
|
||||
from ytdl_sub.entries.entry import Entry
|
||||
from ytdl_sub.logging import YtdlSubLogger
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
@ -336,6 +337,8 @@ class EnhancedDownloadArchive:
|
|||
self._download_archive: Optional[DownloadArchive] = None
|
||||
self._download_mapping: Optional[DownloadMappings] = None
|
||||
|
||||
self.logger = YtdlSubLogger.logger(name=subscription_name)
|
||||
|
||||
@property
|
||||
def archive_file_name(self) -> str:
|
||||
"""
|
||||
|
|
@ -460,10 +463,10 @@ class EnhancedDownloadArchive:
|
|||
)
|
||||
|
||||
for uid, mapping in stale_mappings.items():
|
||||
print(f"[{uid}] Removing the following stale file(s):")
|
||||
self.logger.info("[%s] Removing the following stale file(s):", uid)
|
||||
for file_name in mapping.file_names:
|
||||
file_path = Path(self.output_directory) / Path(file_name)
|
||||
print(f" - {file_path}")
|
||||
self.logger.info(" - %s", file_path)
|
||||
if os.path.exists(file_path):
|
||||
os.remove(file_path)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue