Skip to content

Commit 17220b0

Browse files
committed
feat: make rich logger optional
1 parent 77e6d1f commit 17220b0

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

src/fastapi_cli/logging.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import sys
23
from typing import Union
34

45
from rich.console import Console
@@ -9,17 +10,21 @@ def setup_logging(
910
terminal_width: Union[int, None] = None, level: int = logging.INFO
1011
) -> None:
1112
logger = logging.getLogger("fastapi_cli")
12-
console = Console(width=terminal_width) if terminal_width else None
13-
rich_handler = RichHandler(
14-
show_time=False,
15-
rich_tracebacks=True,
16-
tracebacks_show_locals=True,
17-
markup=True,
18-
show_path=False,
19-
console=console,
20-
)
21-
rich_handler.setFormatter(logging.Formatter("%(message)s"))
22-
logger.addHandler(rich_handler)
13+
if sys.stdout.isatty():
14+
# This is a real terminal, use ANSI escape sequences for colored output
15+
console = Console(width=terminal_width) if terminal_width else None
16+
rich_handler = RichHandler(
17+
show_time=False,
18+
rich_tracebacks=True,
19+
tracebacks_show_locals=True,
20+
markup=True,
21+
show_path=False,
22+
console=console,
23+
)
24+
rich_handler.setFormatter(logging.Formatter("%(message)s"))
25+
logger.propagate = False
26+
else:
27+
# You're being piped or redirected - pass it to the root logger
28+
pass
2329

2430
logger.setLevel(level)
25-
logger.propagate = False

0 commit comments

Comments
 (0)