Skip to content

Commit d2a8878

Browse files
authored
Fix: check LOGFIRE_IGNORE_NO_CONFIG from environment when needed
1 parent e363731 commit d2a8878

2 files changed

Lines changed: 72 additions & 1 deletion

File tree

logfire/_internal/config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,9 @@ def get_event_logger_provider(self) -> EventLoggerProvider | None:
11971197
return self._event_logger_provider
11981198

11991199
def warn_if_not_initialized(self, message: str):
1200-
if not self._initialized and not self.ignore_no_config:
1200+
ignore_no_config_env = os.getenv('LOGFIRE_IGNORE_NO_CONFIG', '')
1201+
ignore_no_config = ignore_no_config_env.lower() in ('1', 'true', 't') or self.ignore_no_config
1202+
if not self._initialized and not ignore_no_config:
12011203
warn_at_user_stacklevel(
12021204
f'{message} until `logfire.configure()` has been called. '
12031205
f'Set the environment variable LOGFIRE_IGNORE_NO_CONFIG=1 or add ignore_no_config=true in pyproject.toml to suppress this warning.',

tests/test_logfire.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from __future__ import annotations
22

33
import inspect
4+
import os
45
import re
56
import sys
7+
import warnings
68
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
79
from contextlib import asynccontextmanager, contextmanager
810
from dataclasses import dataclass
@@ -3476,3 +3478,70 @@ def test_min_level(exporter: TestExporter, config_kwargs: dict[str, Any]) -> Non
34763478
assert [span['name'] for span in exporter.exported_spans_as_dict()] == snapshot(
34773479
['warning span', 'notice message', 'warn message', 'default span']
34783480
)
3481+
3482+
3483+
def test_warn_if_not_initialized():
3484+
"""Test that warnings are properly issued when logfire is not initialized."""
3485+
3486+
config = LogfireConfig()
3487+
3488+
with pytest.warns(LogfireNotConfiguredWarning) as warnings_list:
3489+
config.warn_if_not_initialized('Test message')
3490+
3491+
assert str(warnings_list[0].message) == (
3492+
'Test message until `logfire.configure()` has been called. '
3493+
'Set the environment variable LOGFIRE_IGNORE_NO_CONFIG=1 or add ignore_no_config=true in pyproject.toml to suppress this warning.'
3494+
)
3495+
3496+
with patch.dict(os.environ, {'LOGFIRE_IGNORE_NO_CONFIG': '1'}):
3497+
config.warn_if_not_initialized('Should not warn with env var')
3498+
3499+
with patch.dict(os.environ, {'LOGFIRE_IGNORE_NO_CONFIG': '0'}):
3500+
with pytest.warns(LogfireNotConfiguredWarning) as warnings_list:
3501+
config.warn_if_not_initialized('Should warn with env var 0')
3502+
3503+
assert str(warnings_list[0].message) == (
3504+
'Should warn with env var 0 until `logfire.configure()` has been called. '
3505+
'Set the environment variable LOGFIRE_IGNORE_NO_CONFIG=1 or add ignore_no_config=true in pyproject.toml to suppress this warning.'
3506+
)
3507+
3508+
with patch.dict(os.environ, {'LOGFIRE_IGNORE_NO_CONFIG': ''}):
3509+
with pytest.warns(LogfireNotConfiguredWarning) as warnings_list:
3510+
config.warn_if_not_initialized('Should warn with empty env var')
3511+
3512+
assert str(warnings_list[0].message) == (
3513+
'Should warn with empty env var until `logfire.configure()` has been called. '
3514+
'Set the environment variable LOGFIRE_IGNORE_NO_CONFIG=1 or add ignore_no_config=true in pyproject.toml to suppress this warning.'
3515+
)
3516+
assert len(warnings_list) == 1
3517+
3518+
3519+
def test_warn_if_not_initialized_with_file_config():
3520+
"""Test that warnings are suppressed when ignore_no_config is set in config file."""
3521+
import tempfile
3522+
from pathlib import Path
3523+
3524+
with tempfile.TemporaryDirectory() as temp_dir:
3525+
config_dir = Path(temp_dir)
3526+
pyproject_toml = config_dir / 'pyproject.toml'
3527+
pyproject_toml.write_text('[tool.logfire]\nignore_no_config = true\n')
3528+
3529+
config = LogfireConfig(config_dir=config_dir)
3530+
3531+
with warnings.catch_warnings(record=True) as warning_list:
3532+
warnings.simplefilter('always')
3533+
config.warn_if_not_initialized('Should not warn due to config file')
3534+
3535+
logfire_warnings = [w for w in warning_list if issubclass(w.category, LogfireNotConfiguredWarning)]
3536+
assert len(logfire_warnings) == 0
3537+
3538+
3539+
def test_warn_if_not_initialized_category():
3540+
"""Test that the warning has the correct category."""
3541+
config = LogfireConfig()
3542+
3543+
with pytest.warns(LogfireNotConfiguredWarning) as warnings_list:
3544+
config.warn_if_not_initialized('Test message')
3545+
3546+
assert warnings_list[0].category == LogfireNotConfiguredWarning
3547+
assert issubclass(LogfireNotConfiguredWarning, UserWarning)

0 commit comments

Comments
 (0)