Skip to content

Commit e2d6099

Browse files
authored
Only skip logging to console after updating span stack and indentation (#844)
1 parent 5c251b9 commit e2d6099

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

logfire/_internal/exporters/console.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ def __init__(
8989
def export(self, spans: Sequence[ReadableSpan]) -> SpanExportResult:
9090
"""Export the spans to the console."""
9191
for span in spans:
92-
if span.attributes: # pragma: no branch
93-
log_level: int = span.attributes.get(ATTRIBUTES_LOG_LEVEL_NUM_KEY, _INFO_LEVEL) # type: ignore
94-
if log_level < self._min_log_level_num:
95-
continue
9692
self._log_span(span)
9793

9894
return SpanExportResult.SUCCESS
@@ -102,18 +98,21 @@ def _log_span(self, span: ReadableSpan) -> None:
10298
10399
In this simple case we just print the span if its type is not "span" - e.g. the message at the end of a span.
104100
"""
105-
if span.attributes: # pragma: no branch
106-
span_type = span.attributes.get(ATTRIBUTES_SPAN_TYPE_KEY, 'span')
107-
# only print for "pending_span" (received at the start of a span) and "log" (spans with no duration)
108-
if span_type == 'span' or span.attributes.get(DISABLE_CONSOLE_KEY):
109-
return
110-
111101
self._print_span(span)
112102

113103
def _print_span(self, span: ReadableSpan, indent: int = 0):
114104
"""Build up a summary of the span, including formatting for rich, then print it."""
115105
_msg, parts = self._span_text_parts(span, indent)
116106

107+
if span.attributes: # pragma: no branch
108+
span_type = span.attributes.get(ATTRIBUTES_SPAN_TYPE_KEY, 'span')
109+
# only print for "pending_span" (received at the start of a span) and "log" (spans with no duration)
110+
if span_type == 'span' or span.attributes.get(DISABLE_CONSOLE_KEY):
111+
return
112+
log_level: int = span.attributes.get(ATTRIBUTES_LOG_LEVEL_NUM_KEY, _INFO_LEVEL) # type: ignore
113+
if log_level < self._min_log_level_num:
114+
return
115+
117116
indent_str = (self._timestamp_indent + indent * 2) * ' '
118117
details_parts = self._details_parts(span, indent_str)
119118
if details_parts:
@@ -318,9 +317,6 @@ def _log_span(self, span: ReadableSpan) -> None:
318317
self._indent_level.pop(span.context.span_id, None)
319318
return
320319

321-
if attributes.get(DISABLE_CONSOLE_KEY): # pragma: no cover
322-
return
323-
324320
if span_type == 'pending_span':
325321
parent_id = _pending_span_parent(attributes)
326322
indent = self._indent_level.get(parent_id, 0) if parent_id else 0
@@ -372,9 +368,6 @@ def _log_span(self, span: ReadableSpan) -> None:
372368
self._span_stack.pop()
373369
return
374370

375-
if attributes.get(DISABLE_CONSOLE_KEY): # pragma: no cover
376-
return
377-
378371
self._print_span(span)
379372

380373
def _span_text_parts(self, span: ReadableSpan, indent: int) -> tuple[str, TextParts]:

tests/test_console_exporter.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import io
55
import sys
6+
from typing import Any
67

78
import pytest
89
from dirty_equals import IsStr
@@ -834,6 +835,25 @@ def test_console_exporter_invalid_text_no_color(capsys: pytest.CaptureFixture[st
834835
)
835836

836837

838+
def test_console_exporter_hidden_debug_span(capsys: pytest.CaptureFixture[str], config_kwargs: dict[str, Any]) -> None:
839+
config_kwargs.update(console=None)
840+
logfire.configure(**config_kwargs)
841+
842+
with logfire.span('1'):
843+
# TODO this span doesn't show, but it still adds to the indentation level
844+
with logfire.span('2', _level='debug'):
845+
logfire.info('3')
846+
logfire.info('4')
847+
848+
assert capsys.readouterr().out.splitlines() == snapshot(
849+
[
850+
'00:00:01.000 1',
851+
'00:00:03.000 3',
852+
'00:00:05.000 4',
853+
]
854+
)
855+
856+
837857
def test_console_exporter_include_tags(capsys: pytest.CaptureFixture[str]) -> None:
838858
logfire.configure(
839859
send_to_logfire=False,

0 commit comments

Comments
 (0)