Skip to content

Commit

Permalink
Add check for the texts that should be printed in console
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani committed Jan 29, 2025
1 parent 52dc65f commit 7b402ff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions logfire/_internal/exporters/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions tests/test_console_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7b402ff

Please sign in to comment.