-
-
Notifications
You must be signed in to change notification settings - Fork 61
feat: make rich logger optional #182
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import logging | ||
import sys | ||
from typing import Union | ||
|
||
from rich.console import Console | ||
|
@@ -9,17 +10,21 @@ def setup_logging( | |
terminal_width: Union[int, None] = None, level: int = logging.INFO | ||
) -> None: | ||
logger = logging.getLogger("fastapi_cli") | ||
console = Console(width=terminal_width) if terminal_width else None | ||
rich_handler = RichHandler( | ||
show_time=False, | ||
rich_tracebacks=True, | ||
tracebacks_show_locals=True, | ||
markup=True, | ||
show_path=False, | ||
console=console, | ||
) | ||
rich_handler.setFormatter(logging.Formatter("%(message)s")) | ||
logger.addHandler(rich_handler) | ||
|
||
if sys.stdout.isatty(): | ||
# This is a real terminal, use ANSI escape sequences for colored output | ||
console = Console(width=terminal_width) if terminal_width else None | ||
rich_handler = RichHandler( | ||
show_time=False, | ||
rich_tracebacks=True, | ||
tracebacks_show_locals=True, | ||
markup=True, | ||
show_path=False, | ||
console=console, | ||
) | ||
rich_handler.setFormatter(logging.Formatter("%(message)s")) | ||
logger.propagate = False | ||
else: | ||
# You're being piped or redirected - pass it to the root logger | ||
pass | ||
|
||
logger.setLevel(level) | ||
logger.propagate = False |