-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use colorama for colorful logging
- Loading branch information
1 parent
2028bcb
commit 850a66c
Showing
5 changed files
with
86 additions
and
116 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 |
---|---|---|
|
@@ -14,4 +14,4 @@ repos: | |
hooks: | ||
- id: isort | ||
exclude: '^(env)' | ||
args: ["profile", "--black"] | ||
args: ["--profile", "black"] |
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import atexit | ||
import json | ||
import logging.config | ||
import logging.handlers | ||
from pathlib import Path | ||
|
||
import colorama | ||
|
||
from mr_robot.constants import Client | ||
|
||
|
||
class LogFormatter(logging.Formatter): | ||
colormap = { | ||
logging.DEBUG: colorama.Fore.MAGENTA, | ||
logging.INFO: colorama.Fore.BLUE, | ||
logging.WARNING: colorama.Fore.YELLOW, | ||
logging.ERROR: colorama.Fore.RED, | ||
logging.CRITICAL: colorama.Fore.WHITE, | ||
} | ||
|
||
def __init__(self, format: str) -> None: | ||
super().__init__(format) | ||
|
||
def format(self, record: logging.LogRecord) -> str: | ||
record.levelname = f"{self.colormap.get(record.levelno)}{record.levelname}{colorama.Fore.RESET}" | ||
return super().format(record) | ||
|
||
|
||
def setup_logging_modern() -> None: | ||
with open(Client.logging_config_file, "r") as file: | ||
config = json.load(file) | ||
logging.config.dictConfig(config) | ||
queue_handler = logging.getHandlerByName("queue_handler") | ||
if queue_handler is not None: | ||
queue_handler.listener.start() # type: ignore[reportAttributeAccessIssue] | ||
atexit.register(queue_handler.listener.stop) # type: ignore[reportAttributeAccessIssue] | ||
|
||
|
||
def setup_logging() -> None: | ||
root_logger = logging.getLogger() | ||
|
||
log_file = Path(Client.log_file_name) | ||
log_file.parent.mkdir(exist_ok=True) | ||
|
||
formatter = LogFormatter( | ||
"[ %(levelname)s | %(name)s | %(module)s | L%(lineno)d ] %(asctime)s: %(message)s" | ||
) | ||
file_formatter = logging.Formatter( | ||
"[ %(levelname)s | %(name)s | %(module)s | %(funcName)s | %(filename)s | L%(lineno)d ] %(asctime)s: %(message)s" | ||
) | ||
|
||
file_handler = logging.handlers.RotatingFileHandler( | ||
log_file, | ||
mode="a", | ||
# File handler rotates log every 5 MB | ||
maxBytes=5 * (2**20), | ||
backupCount=10, | ||
) | ||
file_handler.setFormatter(file_formatter) | ||
root_logger.addHandler(file_handler) | ||
|
||
console_handler = logging.StreamHandler() | ||
console_handler.setFormatter(formatter) | ||
root_logger.addHandler(console_handler) | ||
|
||
file_handler.setLevel(logging.DEBUG) | ||
console_handler.setLevel(logging.INFO) | ||
|
||
root_logger.setLevel(logging.DEBUG) | ||
logging.getLogger("httpx").setLevel(logging.WARNING) | ||
logging.getLogger("mafic").setLevel(logging.WARNING) | ||
logging.getLogger("httpcore").setLevel(logging.WARNING) | ||
logging.getLogger("disnake").setLevel(logging.INFO) | ||
logging.getLogger("sqlalchemy.engine").setLevel(logging.WARNING) | ||
logging.getLogger("core").setLevel(logging.WARNING) | ||
logging.getLogger("sqlalchemy.engine.Engine").setLevel(logging.WARNING) | ||
logging.getLogger("sqlalchemy.pool").setLevel(logging.WARNING) | ||
logging.getLogger("sqlalchemy.orm").setLevel(logging.WARNING) | ||
logging.getLogger("aiosqlite").setLevel(logging.WARNING) | ||
logging.getLogger("streamlink").disabled = True | ||
|
||
root_logger.info("Logger Initialized!") |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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