Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: colorize logger format with rich handler #5505

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions app/logger.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
from collections import deque
from datetime import datetime
import io
import logging
import sys
import logging
import threading
from collections import deque
from datetime import datetime
from rich import pretty
from rich.theme import Theme
from rich.console import Console
from rich.logging import RichHandler


logs = None
stdout_interceptor = None
stderr_interceptor = None
LOGFMT = "%(asctime)s %(levelname)s [%(filename)s:%(lineno)s] %(message)s"
formatter = logging.Formatter(LOGFMT)


class LogInterceptor(io.TextIOWrapper):
def __init__(self, stream, *args, **kwargs):
def __init__(self, stream, *args, **kwargs):
buffer = stream.buffer
encoding = stream.encoding
super().__init__(buffer, *args, **kwargs, encoding=encoding, line_buffering=stream.line_buffering)
Expand Down Expand Up @@ -51,6 +58,7 @@ def on_flush(callback):
if stderr_interceptor is not None:
stderr_interceptor.on_flush(callback)


def setup_logger(log_level: str = 'INFO', capacity: int = 300):
global logs
if logs:
Expand All @@ -68,6 +76,33 @@ def setup_logger(log_level: str = 'INFO', capacity: int = 300):
logger = logging.getLogger()
logger.setLevel(log_level)

stream_handler = logging.StreamHandler()
stream_handler.setFormatter(logging.Formatter("%(message)s"))
stream_handler = get_rich_hander(log_level)
logger.addHandler(stream_handler)


def get_rich_hander(log_level: str = "INFO") -> RichHandler:
console = Console(
log_time=True,
log_time_format="%Y-%m-%d %H:%M:%S",
theme=Theme({
"log.time": "green",
"inspect.value.border": "black",
"traceback.border": "black",
"traceback.border.syntax_error": "black",
}),
)

pretty.install(console=console)
handler = RichHandler(
console=console,
level=log_level,
markup=False,
show_time=True,
show_level=True,
show_path=True,
rich_tracebacks=True,
enable_link_path=False,
omit_repeated_times=False,
log_time_format="%Y-%m-%d %H:%M:%S",
)
return handler
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ psutil
kornia>=0.7.1
spandrel
soundfile
rich>=13.9.3