Skip to content

Commit f0efef4

Browse files
samuelcolvindmontagualexmojaki
authored
more resilient console logging (#831)
Co-authored-by: David Montague <[email protected]> Co-authored-by: Alex Hall <[email protected]>
1 parent 52dc65f commit f0efef4

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

logfire/_internal/exporters/console.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,23 +182,23 @@ def _details_parts(self, span: ReadableSpan, indent_str: str) -> TextParts:
182182
if not self._verbose or not span.attributes:
183183
return []
184184

185-
file_location: str = span.attributes.get('code.filepath') # type: ignore
185+
file_location_raw = span.attributes.get('code.filepath')
186+
file_location = None if file_location_raw is None else str(file_location_raw)
186187
if file_location:
187188
lineno = span.attributes.get('code.lineno')
188189
if lineno: # pragma: no branch
189190
file_location += f':{lineno}'
190191

191192
log_level_num: int = span.attributes.get(ATTRIBUTES_LOG_LEVEL_NUM_KEY) # type: ignore
192-
log_level = NUMBER_TO_LEVEL.get(log_level_num, '')
193+
log_level = NUMBER_TO_LEVEL.get(log_level_num)
193194

194195
if file_location or log_level:
195-
return [
196-
(indent_str, ''),
197-
('│', 'blue'),
198-
(' ', ''),
199-
(file_location, 'cyan'),
200-
(f' {log_level}', ''),
201-
]
196+
parts: TextParts = [(indent_str, ''), ('│', 'blue')]
197+
if file_location:
198+
parts.append((f' {file_location}', 'cyan'))
199+
if log_level:
200+
parts.append((f' {log_level}', ''))
201+
return parts
202202
else:
203203
return []
204204

tests/test_console_exporter.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def test_simple_console_exporter_no_colors_verbose(simple_spans: list[ReadableSp
106106
[
107107
'00:00:01.000 rootSpan',
108108
'00:00:02.000 childSpan 1',
109-
' │ testing.py:42 ',
109+
' │ testing.py:42',
110110
]
111111
)
112112

@@ -419,7 +419,7 @@ def test_verbose_attributes(exporter: TestExporter) -> None:
419419
assert out.getvalue().splitlines() == snapshot(
420420
[
421421
'\x1b[32m00:00:01.000\x1b[0m Hello world!',
422-
' \x1b[34m│\x1b[0m \x1b[36mtest_console_exporter.py:123\x1b[0m info',
422+
' \x1b[34m│\x1b[0m\x1b[36m test_console_exporter.py:123\x1b[0m info',
423423
" \x1b[34m│ \x1b[0m\x1b[34mname=\x1b[0m\x1b[93;49m'\x1b[0m\x1b[93;49mworld\x1b[0m\x1b[93;49m'\x1b[0m",
424424
' \x1b[34m│ \x1b[0m\x1b[34md=\x1b[0m\x1b[97;49m{\x1b[0m ',
425425
" \x1b[34m│ \x1b[0m \x1b[97;49m \x1b[0m\x1b[93;49m'\x1b[0m\x1b[93;49ma\x1b[0m\x1b[93;49m'\x1b[0m\x1b[97;49m:\x1b[0m\x1b[97;49m \x1b[0m\x1b[37;49m1\x1b[0m\x1b[97;49m,\x1b[0m",
@@ -796,3 +796,39 @@ def test_exception(exporter: TestExporter) -> None:
796796
'\x1b[0m\x1b[97;49mby\x1b[0m\x1b[97;49m \x1b[0m\x1b[97;49mzero\x1b[0m',
797797
'',
798798
]
799+
800+
801+
def test_console_exporter_invalid_text(capsys: pytest.CaptureFixture[str]) -> None:
802+
logfire.configure(
803+
send_to_logfire=False,
804+
console=ConsoleOptions(colors='always', include_timestamps=False, verbose=True),
805+
)
806+
807+
logfire.info('hi', **{'code.filepath': 3, 'code.lineno': None}) # type: ignore
808+
logfire.info('hi', **{'code.filepath': None, 'code.lineno': 'foo'}) # type: ignore
809+
assert capsys.readouterr().out.splitlines() == snapshot(
810+
[
811+
'hi',
812+
'\x1b[34m│\x1b[0m\x1b[36m 3\x1b[0m info',
813+
'hi',
814+
'\x1b[34m│\x1b[0m info',
815+
]
816+
)
817+
818+
819+
def test_console_exporter_invalid_text_no_color(capsys: pytest.CaptureFixture[str]) -> None:
820+
logfire.configure(
821+
send_to_logfire=False,
822+
console=ConsoleOptions(colors='never', include_timestamps=False, verbose=True),
823+
)
824+
825+
logfire.info('hi', **{'code.filepath': 3, 'code.lineno': None}) # type: ignore
826+
logfire.info('hi', **{'code.filepath': None, 'code.lineno': 'foo'}) # type: ignore
827+
assert capsys.readouterr().out.splitlines() == snapshot(
828+
[
829+
'hi',
830+
'│ 3 info',
831+
'hi',
832+
'│ info',
833+
]
834+
)

0 commit comments

Comments
 (0)