forked from jgonggrijp/pip-review
-
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.
Merge pull request #11 from realshouzy/improve-logging
Improve logging
- Loading branch information
Showing
8 changed files
with
427 additions
and
148 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,81 @@ | ||
from __future__ import annotations | ||
|
||
__all__: list[str] = ["setup_logging", "set_logging_level"] | ||
__all__: list[str] = ["setup_logging"] | ||
|
||
import logging | ||
import logging.config | ||
import sys | ||
from typing import TextIO | ||
from typing import ClassVar, Literal | ||
|
||
if sys.version_info >= (3, 12): # pragma: >=3.12 cover | ||
from typing import override | ||
else: # pragma: <3.12 cover | ||
from typing_extensions import override | ||
|
||
|
||
class _StdOutFilter(logging.Filter): | ||
class _NonErrorFilter(logging.Filter): | ||
@override | ||
def filter(self, record: logging.LogRecord) -> bool: | ||
return record.levelno <= logging.INFO | ||
|
||
|
||
def setup_logging(logger_name: str) -> logging.Logger: | ||
logger: logging.Logger = logging.getLogger(logger_name) | ||
class _ColoredFormatter(logging.Formatter): | ||
COLORS: ClassVar[dict[str, str]] = { | ||
"DEBUG": "\x1b[0;37m", | ||
"INFO": "\x1b[0;32m", | ||
"WARNING": "\x1b[0;33m", | ||
"ERROR": "\x1b[0;31m", | ||
"CRITICAL": "\x1b[1;31m", | ||
} | ||
RESET: ClassVar[Literal["\x1b[0m"]] = "\x1b[0m" | ||
|
||
stdout_handler: logging.StreamHandler[TextIO] = logging.StreamHandler(sys.stdout) | ||
stdout_handler.set_name("stdout") | ||
stdout_handler.addFilter(_StdOutFilter()) | ||
stdout_handler.setFormatter(logging.Formatter("%(message)s")) | ||
stdout_handler.setLevel(logging.DEBUG) | ||
|
||
stderr_handler: logging.StreamHandler[TextIO] = logging.StreamHandler(sys.stderr) | ||
stderr_handler.set_name("stderr") | ||
stderr_handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s")) | ||
stderr_handler.setLevel(logging.WARNING) | ||
|
||
logger.addHandler(stderr_handler) | ||
logger.addHandler(stdout_handler) | ||
return logger | ||
@override | ||
def format(self, record: logging.LogRecord) -> str: | ||
log_color: str = self.COLORS.get(record.levelname, self.RESET) | ||
record.msg = f"{log_color}{record.levelname}: {record.msg}{self.RESET}" | ||
return super().format(record) | ||
|
||
|
||
def set_logging_level(logger: logging.Logger, *, verbose: bool) -> None: | ||
logger.setLevel(logging.DEBUG if verbose else logging.INFO) | ||
def setup_logging(logger_name: str, *, verbose: bool) -> None: | ||
level: Literal["DEBUG", "INFO"] = "DEBUG" if verbose else "INFO" | ||
logging.config.dictConfig( | ||
{ | ||
"version": 1, | ||
"disable_existing_loggers": False, | ||
"formatters": { | ||
"simple": { | ||
"format": "%(message)s", | ||
}, | ||
"colored": { | ||
"()": _ColoredFormatter, | ||
}, | ||
}, | ||
"filters": { | ||
"StdOutFilter": { | ||
"()": _NonErrorFilter, | ||
}, | ||
}, | ||
"handlers": { | ||
"stdout": { | ||
"class": "logging.StreamHandler", | ||
"stream": "ext://sys.stdout", | ||
"formatter": "simple", | ||
"filters": ["StdOutFilter"], | ||
"level": "DEBUG", | ||
}, | ||
"stderr": { | ||
"class": "logging.StreamHandler", | ||
"stream": "ext://sys.stderr", | ||
"formatter": "colored", | ||
"level": "WARNING", | ||
}, | ||
}, | ||
"loggers": { | ||
"": { | ||
"level": "DEBUG", | ||
"handlers": ["stdout", "stderr"], | ||
}, | ||
logger_name: {"level": level, "propagate": True}, | ||
}, | ||
}, | ||
) |
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
Oops, something went wrong.