Skip to content

Commit 294aa41

Browse files
authored
Add logfire.warning to mirror logging.warning (#800)
1 parent c2806ef commit 294aa41

File tree

7 files changed

+23
-10
lines changed

7 files changed

+23
-10
lines changed

logfire-api/logfire_api/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ def notice(self, *args, **kwargs) -> None: ...
6767

6868
def info(self, *args, **kwargs) -> None: ...
6969

70-
def warn(self, *args, **kwargs) -> None: ...
70+
def warning(self, *args, **kwargs) -> None: ...
71+
72+
warn = warning
7173

7274
def error(self, *args, **kwargs) -> None: ...
7375

@@ -151,6 +153,7 @@ def shutdown(self, *args, **kwargs) -> None: ...
151153
notice = DEFAULT_LOGFIRE_INSTANCE.notice
152154
info = DEFAULT_LOGFIRE_INSTANCE.info
153155
warn = DEFAULT_LOGFIRE_INSTANCE.warn
156+
warning = DEFAULT_LOGFIRE_INSTANCE.warning
154157
error = DEFAULT_LOGFIRE_INSTANCE.error
155158
exception = DEFAULT_LOGFIRE_INSTANCE.exception
156159
fatal = DEFAULT_LOGFIRE_INSTANCE.fatal

logfire-api/logfire_api/__init__.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ from .version import VERSION as VERSION
1111
from logfire.sampling import SamplingOptions as SamplingOptions
1212
from typing import Any
1313

14-
__all__ = ['Logfire', 'LogfireSpan', 'LevelName', 'AdvancedOptions', 'ConsoleOptions', 'CodeSource', 'PydanticPlugin', 'configure', 'span', 'instrument', 'log', 'trace', 'debug', 'notice', 'info', 'warn', 'error', 'exception', 'fatal', 'force_flush', 'log_slow_async_callbacks', 'install_auto_tracing', 'instrument_asgi', 'instrument_wsgi', 'instrument_pydantic', 'instrument_fastapi', 'instrument_openai', 'instrument_anthropic', 'instrument_asyncpg', 'instrument_httpx', 'instrument_celery', 'instrument_requests', 'instrument_psycopg', 'instrument_django', 'instrument_flask', 'instrument_starlette', 'instrument_aiohttp_client', 'instrument_sqlalchemy', 'instrument_sqlite3', 'instrument_aws_lambda', 'instrument_redis', 'instrument_pymongo', 'instrument_mysql', 'instrument_system_metrics', 'AutoTraceModule', 'with_tags', 'with_settings', 'suppress_scopes', 'shutdown', 'no_auto_trace', 'ScrubMatch', 'ScrubbingOptions', 'VERSION', 'suppress_instrumentation', 'StructlogProcessor', 'LogfireLoggingHandler', 'loguru_handler', 'SamplingOptions', 'MetricsOptions']
14+
__all__ = ['Logfire', 'LogfireSpan', 'LevelName', 'AdvancedOptions', 'ConsoleOptions', 'CodeSource', 'PydanticPlugin', 'configure', 'span', 'instrument', 'log', 'trace', 'debug', 'notice', 'info', 'warn', 'warning', 'error', 'exception', 'fatal', 'force_flush', 'log_slow_async_callbacks', 'install_auto_tracing', 'instrument_asgi', 'instrument_wsgi', 'instrument_pydantic', 'instrument_fastapi', 'instrument_openai', 'instrument_anthropic', 'instrument_asyncpg', 'instrument_httpx', 'instrument_celery', 'instrument_requests', 'instrument_psycopg', 'instrument_django', 'instrument_flask', 'instrument_starlette', 'instrument_aiohttp_client', 'instrument_sqlalchemy', 'instrument_sqlite3', 'instrument_aws_lambda', 'instrument_redis', 'instrument_pymongo', 'instrument_mysql', 'instrument_system_metrics', 'AutoTraceModule', 'with_tags', 'with_settings', 'suppress_scopes', 'shutdown', 'no_auto_trace', 'ScrubMatch', 'ScrubbingOptions', 'VERSION', 'suppress_instrumentation', 'StructlogProcessor', 'LogfireLoggingHandler', 'loguru_handler', 'SamplingOptions', 'MetricsOptions']
1515

1616
DEFAULT_LOGFIRE_INSTANCE = Logfire()
1717
span = DEFAULT_LOGFIRE_INSTANCE.span
@@ -51,6 +51,7 @@ debug = DEFAULT_LOGFIRE_INSTANCE.debug
5151
info = DEFAULT_LOGFIRE_INSTANCE.info
5252
notice = DEFAULT_LOGFIRE_INSTANCE.notice
5353
warn = DEFAULT_LOGFIRE_INSTANCE.warn
54+
warning = DEFAULT_LOGFIRE_INSTANCE.warning
5455
error = DEFAULT_LOGFIRE_INSTANCE.error
5556
fatal = DEFAULT_LOGFIRE_INSTANCE.fatal
5657
exception = DEFAULT_LOGFIRE_INSTANCE.exception

logfire-api/logfire_api/_internal/main.pyi

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,19 @@ class Logfire:
136136
137137
Set to `True` to use the currently handled exception.
138138
"""
139-
def warn(self, msg_template: str, /, *, _tags: Sequence[str] | None = None, _exc_info: ExcInfo = False, **attributes: Any) -> None:
139+
def warning(self, msg_template: str, /, *, _tags: Sequence[str] | None = None, _exc_info: ExcInfo = False, **attributes: Any) -> None:
140140
"""Log a warning message.
141141
142142
```py
143143
import logfire
144144
145145
logfire.configure()
146146
147-
logfire.warn('This is a warning log')
147+
logfire.warning('This is a warning log')
148148
```
149149
150+
`logfire.warn` is an alias of `logfire.warning`.
151+
150152
Args:
151153
msg_template: The message to log.
152154
attributes: The attributes to bind to the log.
@@ -156,6 +158,7 @@ class Logfire:
156158
157159
Set to `True` to use the currently handled exception.
158160
"""
161+
warn = warning
159162
def error(self, msg_template: str, /, *, _tags: Sequence[str] | None = None, _exc_info: ExcInfo = False, **attributes: Any) -> None:
160163
"""Log an error message.
161164

logfire/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
info = DEFAULT_LOGFIRE_INSTANCE.info
5959
notice = DEFAULT_LOGFIRE_INSTANCE.notice
6060
warn = DEFAULT_LOGFIRE_INSTANCE.warn
61+
warning = DEFAULT_LOGFIRE_INSTANCE.warning
6162
error = DEFAULT_LOGFIRE_INSTANCE.error
6263
fatal = DEFAULT_LOGFIRE_INSTANCE.fatal
6364
exception = DEFAULT_LOGFIRE_INSTANCE.exception
@@ -102,6 +103,7 @@ def loguru_handler() -> Any:
102103
'notice',
103104
'info',
104105
'warn',
106+
'warning',
105107
'error',
106108
'exception',
107109
'fatal',

logfire/_internal/main.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ def notice(
388388
raise ValueError('Attribute keys cannot start with an underscore.')
389389
self.log('notice', msg_template, attributes, tags=_tags, exc_info=_exc_info)
390390

391-
def warn(
391+
def warning(
392392
self,
393393
msg_template: str,
394394
/,
@@ -404,9 +404,11 @@ def warn(
404404
405405
logfire.configure()
406406
407-
logfire.warn('This is a warning log')
407+
logfire.warning('This is a warning log')
408408
```
409409
410+
`logfire.warn` is an alias of `logfire.warning`.
411+
410412
Args:
411413
msg_template: The message to log.
412414
attributes: The attributes to bind to the log.
@@ -420,6 +422,8 @@ def warn(
420422
raise ValueError('Attribute keys cannot start with an underscore.')
421423
self.log('warn', msg_template, attributes, tags=_tags, exc_info=_exc_info)
422424

425+
warn = warning
426+
423427
def error(
424428
self,
425429
msg_template: str,

tests/test_logfire.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
from tests.test_metrics import get_collected_metrics
4545

4646

47-
@pytest.mark.parametrize('method', ['trace', 'info', 'debug', 'warn', 'error', 'fatal'])
47+
@pytest.mark.parametrize('method', ['trace', 'info', 'debug', 'warn', 'warning', 'error', 'fatal'])
4848
def test_log_methods_without_kwargs(method: str):
4949
with pytest.warns(FormattingFailedWarning) as warnings:
5050
getattr(logfire, method)('{foo}', bar=2)
@@ -537,7 +537,7 @@ def test_span_without_span_name(exporter: TestExporter) -> None:
537537
)
538538

539539

540-
@pytest.mark.parametrize('level', ('fatal', 'debug', 'error', 'info', 'notice', 'warn', 'trace'))
540+
@pytest.mark.parametrize('level', ('fatal', 'debug', 'error', 'info', 'notice', 'warn', 'warning', 'trace'))
541541
def test_log(exporter: TestExporter, level: LevelName):
542542
getattr(logfire, level)('test {name} {number} {none}', name='foo', number=2, none=None)
543543

@@ -1775,7 +1775,7 @@ def test_kwarg_with_dot_in_name(exporter: TestExporter) -> None:
17751775
)
17761776

17771777

1778-
@pytest.mark.parametrize('method', ('trace', 'debug', 'info', 'notice', 'warn', 'error', 'fatal', 'span'))
1778+
@pytest.mark.parametrize('method', ('trace', 'debug', 'info', 'notice', 'warn', 'warning', 'error', 'fatal', 'span'))
17791779
def test_forbid_methods_with_leading_underscore_on_attributes(method: str) -> None:
17801780
with pytest.raises(ValueError, match='Attribute keys cannot start with an underscore.'):
17811781
getattr(logfire, method)('test {_foo=}', _foo='bar')

tests/test_logfire_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_runtime(logfire_api_factory: Callable[[], ModuleType], module_name: str
7373
logfire_api.log('info', 'test log')
7474
logfire__all__.remove('log')
7575

76-
for log_method in ['trace', 'debug', 'info', 'notice', 'warn', 'error', 'exception', 'fatal']:
76+
for log_method in ['trace', 'debug', 'info', 'notice', 'warn', 'warning', 'error', 'exception', 'fatal']:
7777
assert hasattr(logfire_api, log_method)
7878
getattr(logfire_api, log_method)('test log')
7979
logfire__all__.remove(log_method)

0 commit comments

Comments
 (0)