Skip to content

Commit

Permalink
more resiliant console logging, fix #830
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Jan 29, 2025
1 parent 52dc65f commit 123c7e4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion logfire/_internal/exporters/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def _details_parts(self, span: ReadableSpan, indent_str: str) -> TextParts:
if not self._verbose or not span.attributes:
return []

file_location: str = span.attributes.get('code.filepath') # type: ignore
file_location = str(span.attributes.get('code.filepath'))
if file_location:
lineno = span.attributes.get('code.lineno')
if lineno: # pragma: no branch
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 @@ -796,3 +796,23 @@ def test_exception(exporter: TestExporter) -> None:
'\x1b[0m\x1b[97;49mby\x1b[0m\x1b[97;49m \x1b[0m\x1b[97;49mzero\x1b[0m',
'',
]


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() == snapshot(['hi', '\x1b[34m│\x1b[0m \x1b[36m3\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() == snapshot(['hi', '│ 3 info'])

0 comments on commit 123c7e4

Please sign in to comment.