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

more resilient console logging #831

Merged
merged 4 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 9 additions & 9 deletions logfire/_internal/exporters/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,23 +182,23 @@ 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_raw = span.attributes.get('code.filepath')
file_location = None if file_location_raw is None else str(file_location_raw)
if file_location:
lineno = span.attributes.get('code.lineno')
if lineno: # pragma: no branch
file_location += f':{lineno}'

log_level_num: int = span.attributes.get(ATTRIBUTES_LOG_LEVEL_NUM_KEY) # type: ignore
log_level = NUMBER_TO_LEVEL.get(log_level_num, '')
log_level = NUMBER_TO_LEVEL.get(log_level_num)

if file_location or log_level:
return [
(indent_str, ''),
('│', 'blue'),
(' ', ''),
(file_location, 'cyan'),
(f' {log_level}', ''),
]
parts: TextParts = [(indent_str, ''), ('│', 'blue'), (' ', '')]
if file_location:
parts.append((file_location, 'cyan'))
if log_level:
parts.append((f' {log_level}', ''))
dmontagu marked this conversation as resolved.
Show resolved Hide resolved
return parts
else:
return []

Expand Down
22 changes: 21 additions & 1 deletion tests/test_console_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_simple_console_exporter_no_colors_verbose(simple_spans: list[ReadableSp
[
'00:00:01.000 rootSpan',
'00:00:02.000 childSpan 1',
' │ testing.py:42 ',
' │ testing.py:42',
]
)

Expand Down 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'])
Loading