From 7b402ff642f4a5ecd27f6cd7c974dae792a65f3b Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Wed, 29 Jan 2025 18:01:52 +0330 Subject: [PATCH] Add check for the texts that should be printed in console --- logfire/_internal/exporters/console.py | 4 ++-- tests/test_console_exporter.py | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/logfire/_internal/exporters/console.py b/logfire/_internal/exporters/console.py index 21d431549..c421bc8b4 100644 --- a/logfire/_internal/exporters/console.py +++ b/logfire/_internal/exporters/console.py @@ -118,9 +118,9 @@ def _print_span(self, span: ReadableSpan, indent: int = 0): parts += [('\n', '')] + details_parts if self._console: - self._console.print(Text.assemble(*parts)) + self._console.print(Text.assemble(*[(text, style) for text, style in parts if isinstance(text, str)])) # type: ignore else: - print(''.join(text for text, _style in parts), file=self._output) + print(''.join(text for text, _style in parts if isinstance(text, str)), file=self._output) # type: ignore # This uses a separate system for color vs no-color because it's not simple text: # in the rich case it uses syntax highlighting and columns for layout. diff --git a/tests/test_console_exporter.py b/tests/test_console_exporter.py index 638ac1261..664d650a8 100644 --- a/tests/test_console_exporter.py +++ b/tests/test_console_exporter.py @@ -86,6 +86,26 @@ def test_simple_console_exporter_no_colors_concise(simple_spans: list[ReadableSp ) +def test_console_exporter_invalid_text(capsys: pytest.CaptureFixture[str]) -> None: + logfire.configure( + send_to_logfire=False, + console=ConsoleOptions(colors='always', include_timestamps=False, verbose=True), + ) + + logfire.info('hi', **{'code.filepath': 3, 'code.lineno': None}) + assert capsys.readouterr().out.splitlines() == ['hi', '\x1b[34m│\x1b[0m info'] + + +def test_console_exporter_invalid_text_no_color(capsys: pytest.CaptureFixture[str]) -> None: + logfire.configure( + send_to_logfire=False, + console=ConsoleOptions(colors='never', include_timestamps=False, verbose=True), + ) + + logfire.info('hi', **{'code.filepath': 3, 'code.lineno': None}) + assert capsys.readouterr().out.splitlines() == ['hi', '│ info'] + + def test_simple_console_exporter_colors_concise(simple_spans: list[ReadableSpan]) -> None: out = io.StringIO() SimpleConsoleSpanExporter(output=out, verbose=False, colors='always').export(simple_spans)