From 294aa410361889dc66b50aa9cde69d02da697e46 Mon Sep 17 00:00:00 2001 From: Jacob Hayes Date: Wed, 15 Jan 2025 21:50:50 +0800 Subject: [PATCH] Add logfire.warning to mirror logging.warning (#800) --- logfire-api/logfire_api/__init__.py | 5 ++++- logfire-api/logfire_api/__init__.pyi | 3 ++- logfire-api/logfire_api/_internal/main.pyi | 7 +++++-- logfire/__init__.py | 2 ++ logfire/_internal/main.py | 8 ++++++-- tests/test_logfire.py | 6 +++--- tests/test_logfire_api.py | 2 +- 7 files changed, 23 insertions(+), 10 deletions(-) diff --git a/logfire-api/logfire_api/__init__.py b/logfire-api/logfire_api/__init__.py index 391b5d37..b7b8fc72 100644 --- a/logfire-api/logfire_api/__init__.py +++ b/logfire-api/logfire_api/__init__.py @@ -67,7 +67,9 @@ def notice(self, *args, **kwargs) -> None: ... def info(self, *args, **kwargs) -> None: ... - def warn(self, *args, **kwargs) -> None: ... + def warning(self, *args, **kwargs) -> None: ... + + warn = warning def error(self, *args, **kwargs) -> None: ... @@ -151,6 +153,7 @@ def shutdown(self, *args, **kwargs) -> None: ... notice = DEFAULT_LOGFIRE_INSTANCE.notice info = DEFAULT_LOGFIRE_INSTANCE.info warn = DEFAULT_LOGFIRE_INSTANCE.warn + warning = DEFAULT_LOGFIRE_INSTANCE.warning error = DEFAULT_LOGFIRE_INSTANCE.error exception = DEFAULT_LOGFIRE_INSTANCE.exception fatal = DEFAULT_LOGFIRE_INSTANCE.fatal diff --git a/logfire-api/logfire_api/__init__.pyi b/logfire-api/logfire_api/__init__.pyi index 94cc17c1..af849874 100644 --- a/logfire-api/logfire_api/__init__.pyi +++ b/logfire-api/logfire_api/__init__.pyi @@ -11,7 +11,7 @@ from .version import VERSION as VERSION from logfire.sampling import SamplingOptions as SamplingOptions from typing import Any -__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'] +__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'] DEFAULT_LOGFIRE_INSTANCE = Logfire() span = DEFAULT_LOGFIRE_INSTANCE.span @@ -51,6 +51,7 @@ debug = DEFAULT_LOGFIRE_INSTANCE.debug info = DEFAULT_LOGFIRE_INSTANCE.info notice = DEFAULT_LOGFIRE_INSTANCE.notice warn = DEFAULT_LOGFIRE_INSTANCE.warn +warning = DEFAULT_LOGFIRE_INSTANCE.warning error = DEFAULT_LOGFIRE_INSTANCE.error fatal = DEFAULT_LOGFIRE_INSTANCE.fatal exception = DEFAULT_LOGFIRE_INSTANCE.exception diff --git a/logfire-api/logfire_api/_internal/main.pyi b/logfire-api/logfire_api/_internal/main.pyi index 199b5031..fb12956b 100644 --- a/logfire-api/logfire_api/_internal/main.pyi +++ b/logfire-api/logfire_api/_internal/main.pyi @@ -136,7 +136,7 @@ class Logfire: Set to `True` to use the currently handled exception. """ - def warn(self, msg_template: str, /, *, _tags: Sequence[str] | None = None, _exc_info: ExcInfo = False, **attributes: Any) -> None: + def warning(self, msg_template: str, /, *, _tags: Sequence[str] | None = None, _exc_info: ExcInfo = False, **attributes: Any) -> None: """Log a warning message. ```py @@ -144,9 +144,11 @@ class Logfire: logfire.configure() - logfire.warn('This is a warning log') + logfire.warning('This is a warning log') ``` + `logfire.warn` is an alias of `logfire.warning`. + Args: msg_template: The message to log. attributes: The attributes to bind to the log. @@ -156,6 +158,7 @@ class Logfire: Set to `True` to use the currently handled exception. """ + warn = warning def error(self, msg_template: str, /, *, _tags: Sequence[str] | None = None, _exc_info: ExcInfo = False, **attributes: Any) -> None: """Log an error message. diff --git a/logfire/__init__.py b/logfire/__init__.py index 6d3dd138..6e991c83 100644 --- a/logfire/__init__.py +++ b/logfire/__init__.py @@ -58,6 +58,7 @@ info = DEFAULT_LOGFIRE_INSTANCE.info notice = DEFAULT_LOGFIRE_INSTANCE.notice warn = DEFAULT_LOGFIRE_INSTANCE.warn +warning = DEFAULT_LOGFIRE_INSTANCE.warning error = DEFAULT_LOGFIRE_INSTANCE.error fatal = DEFAULT_LOGFIRE_INSTANCE.fatal exception = DEFAULT_LOGFIRE_INSTANCE.exception @@ -102,6 +103,7 @@ def loguru_handler() -> Any: 'notice', 'info', 'warn', + 'warning', 'error', 'exception', 'fatal', diff --git a/logfire/_internal/main.py b/logfire/_internal/main.py index 7069a24c..a8703efb 100644 --- a/logfire/_internal/main.py +++ b/logfire/_internal/main.py @@ -388,7 +388,7 @@ def notice( raise ValueError('Attribute keys cannot start with an underscore.') self.log('notice', msg_template, attributes, tags=_tags, exc_info=_exc_info) - def warn( + def warning( self, msg_template: str, /, @@ -404,9 +404,11 @@ def warn( logfire.configure() - logfire.warn('This is a warning log') + logfire.warning('This is a warning log') ``` + `logfire.warn` is an alias of `logfire.warning`. + Args: msg_template: The message to log. attributes: The attributes to bind to the log. @@ -420,6 +422,8 @@ def warn( raise ValueError('Attribute keys cannot start with an underscore.') self.log('warn', msg_template, attributes, tags=_tags, exc_info=_exc_info) + warn = warning + def error( self, msg_template: str, diff --git a/tests/test_logfire.py b/tests/test_logfire.py index d3a7c761..47aef5c5 100644 --- a/tests/test_logfire.py +++ b/tests/test_logfire.py @@ -44,7 +44,7 @@ from tests.test_metrics import get_collected_metrics -@pytest.mark.parametrize('method', ['trace', 'info', 'debug', 'warn', 'error', 'fatal']) +@pytest.mark.parametrize('method', ['trace', 'info', 'debug', 'warn', 'warning', 'error', 'fatal']) def test_log_methods_without_kwargs(method: str): with pytest.warns(FormattingFailedWarning) as warnings: getattr(logfire, method)('{foo}', bar=2) @@ -537,7 +537,7 @@ def test_span_without_span_name(exporter: TestExporter) -> None: ) -@pytest.mark.parametrize('level', ('fatal', 'debug', 'error', 'info', 'notice', 'warn', 'trace')) +@pytest.mark.parametrize('level', ('fatal', 'debug', 'error', 'info', 'notice', 'warn', 'warning', 'trace')) def test_log(exporter: TestExporter, level: LevelName): getattr(logfire, level)('test {name} {number} {none}', name='foo', number=2, none=None) @@ -1775,7 +1775,7 @@ def test_kwarg_with_dot_in_name(exporter: TestExporter) -> None: ) -@pytest.mark.parametrize('method', ('trace', 'debug', 'info', 'notice', 'warn', 'error', 'fatal', 'span')) +@pytest.mark.parametrize('method', ('trace', 'debug', 'info', 'notice', 'warn', 'warning', 'error', 'fatal', 'span')) def test_forbid_methods_with_leading_underscore_on_attributes(method: str) -> None: with pytest.raises(ValueError, match='Attribute keys cannot start with an underscore.'): getattr(logfire, method)('test {_foo=}', _foo='bar') diff --git a/tests/test_logfire_api.py b/tests/test_logfire_api.py index 815f6fd2..2f630666 100644 --- a/tests/test_logfire_api.py +++ b/tests/test_logfire_api.py @@ -73,7 +73,7 @@ def test_runtime(logfire_api_factory: Callable[[], ModuleType], module_name: str logfire_api.log('info', 'test log') logfire__all__.remove('log') - for log_method in ['trace', 'debug', 'info', 'notice', 'warn', 'error', 'exception', 'fatal']: + for log_method in ['trace', 'debug', 'info', 'notice', 'warn', 'warning', 'error', 'exception', 'fatal']: assert hasattr(logfire_api, log_method) getattr(logfire_api, log_method)('test log') logfire__all__.remove(log_method)