Skip to content

Commit 6ac69e5

Browse files
stevenhalexmojaki
andauthored
feat: console output option (#1477)
Co-authored-by: Alex Hall <[email protected]>
1 parent d3d8820 commit 6ac69e5

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

logfire/_internal/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@
113113
)
114114

115115
if TYPE_CHECKING:
116+
from typing import TextIO
117+
116118
from opentelemetry._events import EventLoggerProvider
117119

118120
from .main import Logfire
@@ -159,6 +161,8 @@ class ConsoleOptions:
159161

160162
show_project_link: bool = True
161163
"""Whether to print the URL of the Logfire project after initialization."""
164+
output: TextIO | None = None
165+
"""The output stream to write console output to (default: stdout)."""
162166

163167

164168
@dataclass
@@ -894,6 +898,7 @@ def add_span_processor(span_processor: SpanProcessor) -> None:
894898
include_tags=self.console.include_tags,
895899
verbose=self.console.verbose,
896900
min_log_level=self.console.min_log_level,
901+
output=self.console.output,
897902
)
898903
add_span_processor(SimpleSpanProcessor(console_span_exporter))
899904
log_record_processors.append(SimpleLogRecordProcessor(ConsoleLogExporter(console_span_exporter)))

tests/test_configure.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import threading
88
from collections.abc import Iterable, Sequence
99
from contextlib import ExitStack
10+
from io import StringIO
1011
from pathlib import Path
1112
from time import sleep, time
1213
from typing import Any
@@ -881,6 +882,19 @@ def test_config_serializable_console_false():
881882
assert GLOBAL_CONFIG.console is False
882883

883884

885+
def test_config_console_output_set():
886+
output = StringIO()
887+
logfire.configure(send_to_logfire=False, console=logfire.ConsoleOptions(output=output))
888+
assert isinstance(GLOBAL_CONFIG.console, logfire.ConsoleOptions)
889+
assert GLOBAL_CONFIG.console.output is output
890+
891+
deserialize_config(serialize_config())
892+
assert isinstance(GLOBAL_CONFIG.console, logfire.ConsoleOptions)
893+
assert GLOBAL_CONFIG.console.output is output
894+
logfire.info('test')
895+
assert 'test' in output.getvalue()
896+
897+
884898
def test_sanitize_project_name():
885899
assert sanitize_project_name('foo') == 'foo'
886900
assert sanitize_project_name('FOO') == 'foo'

0 commit comments

Comments
 (0)