Skip to content

Commit

Permalink
feat: Provide function to enable/configure logger
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Oct 3, 2019
1 parent 9e411d2 commit 8620a09
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
21 changes: 20 additions & 1 deletion src/aria2p/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
please refer to the README.md included in this package to get the link to the official documentation.
"""

import sys

from loguru import logger

from .api import API
Expand All @@ -18,4 +20,21 @@

logger.disable("aria2p")

__all__ = ["API", "ClientException", "Client", "Download", "BitTorrent", "File", "Options", "Stats"]

def enable_logger(sink=sys.stderr, level="WARNING"):
"""
Enable the logging of messages.
Configure the ``logger`` variable imported from ``loguru``.
Args:
sink (file): An opened file pointer, or stream handler. Default to standard error.
level (str): The log level to use. Possible values are TRACE, DEBUG, INFO, WARNING, ERROR, CRITICAL.
Default to WARNING.
"""
logger.remove()
logger.configure(handlers=[{"sink": sink, "level": level}])
logger.enable("aria2p")


__all__ = ["API", "ClientException", "Client", "Download", "BitTorrent", "File", "Options", "Stats", "enable_logger"]
6 changes: 2 additions & 4 deletions src/aria2p/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import requests
from loguru import logger

from aria2p import Download
from aria2p import Download, enable_logger

from .api import API
from .client import DEFAULT_HOST, DEFAULT_PORT, Client, ClientException
Expand All @@ -36,9 +36,7 @@ def main(args=None):
args = parser.parse_args(args=args)
kwargs = args.__dict__

logger.remove()
logger.configure(handlers=[{"sink": sys.stderr, "level": kwargs.pop("log_level")}])
logger.enable("aria2p")
enable_logger(kwargs.pop("log_level"))

check_args(parser, args)

Expand Down

0 comments on commit 8620a09

Please sign in to comment.