From 123c7e45e3574f647efe5cdfd47a58be0e822aec Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Wed, 29 Jan 2025 16:54:42 +0000 Subject: [PATCH] more resiliant console logging, fix #830 --- logfire/_internal/exporters/console.py | 2 +- tests/test_console_exporter.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/logfire/_internal/exporters/console.py b/logfire/_internal/exporters/console.py index 21d431549..2fa578224 100644 --- a/logfire/_internal/exporters/console.py +++ b/logfire/_internal/exporters/console.py @@ -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 diff --git a/tests/test_console_exporter.py b/tests/test_console_exporter.py index 638ac1261..b06de0eb9 100644 --- a/tests/test_console_exporter.py +++ b/tests/test_console_exporter.py @@ -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'])