-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
51 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,66 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
import sys | ||
from typing import Any | ||
|
||
import click | ||
|
||
logging.basicConfig() | ||
_logger = logging.getLogger("pybuild-deps") | ||
|
||
|
||
class Logger: | ||
stream = sys.stderr | ||
"""Custom logger for pybuild-deps.""" | ||
|
||
def __init__(self, verbosity: int = 0): | ||
self.verbosity = self._initial_verbosity = verbosity | ||
self._verbosity = verbosity | ||
self.as_library = True | ||
|
||
def log(self, message: str, *args: Any, **kwargs: Any) -> None: | ||
@property | ||
def verbosity(self): | ||
return self._verbosity | ||
|
||
@verbosity.setter | ||
def verbosity(self, value): | ||
self._verbosity = value | ||
if self._verbosity < 0: | ||
_logger.setLevel(logging.WARNING) | ||
if self._verbosity == 0: | ||
_logger.setLevel(logging.INFO) | ||
if self._verbosity >= 1: | ||
_logger.setLevel(logging.DEBUG) | ||
|
||
def log(self, level: int, message: str, *args: Any, **kwargs: Any) -> None: | ||
if self.as_library: | ||
_logger.log(level, message, *args, **kwargs) | ||
else: | ||
self._cli_log(level, message, args, kwargs) | ||
|
||
def _cli_log(self, level, message, args, kwargs): | ||
kwargs.setdefault("err", True) | ||
if level >= logging.ERROR: | ||
kwargs.setdefault("fg", "red") | ||
elif level >= logging.WARNING: | ||
kwargs.setdefault("fg", "yellow") | ||
elif level >= logging.INFO and self.verbosity < 0: | ||
return | ||
elif level >= logging.DEBUG and self.verbosity < 1: | ||
return | ||
elif level <= logging.DEBUG and self.verbosity >= 1: | ||
kwargs.setdefault("fg", "blue") | ||
click.secho(message, *args, **kwargs) | ||
|
||
def debug(self, message: str, *args: Any, **kwargs: Any) -> None: | ||
if self.verbosity >= 1: | ||
self.log(message, *args, **kwargs) | ||
self.log(logging.DEBUG, message, *args, **kwargs) | ||
|
||
def info(self, message: str, *args: Any, **kwargs: Any) -> None: | ||
if self.verbosity >= 0: | ||
self.log(message, *args, **kwargs) | ||
self.log(logging.INFO, message, *args, **kwargs) | ||
|
||
def warning(self, message: str, *args: Any, **kwargs: Any) -> None: | ||
kwargs.setdefault("fg", "yellow") | ||
self.log(message, *args, **kwargs) | ||
self.log(logging.WARNING, message, *args, **kwargs) | ||
|
||
def error(self, message: str, *args: Any, **kwargs: Any) -> None: | ||
kwargs.setdefault("fg", "red") | ||
self.log(message, *args, **kwargs) | ||
self.log(logging.ERROR, message, *args, **kwargs) | ||
|
||
|
||
log = Logger() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters