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

Remove None from text that should be printed in console #827

Closed
wants to merge 1 commit into from
Closed
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
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}) # type: ignore
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}) # type: ignore
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