Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logfire.warning to mirror logging.warning #800

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion logfire-api/logfire_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion logfire-api/logfire_api/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions logfire-api/logfire_api/_internal/main.pyi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think i'd prefer

def warning(...):
    ...

warn = warning

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me - are you ok without the note that "warn is an alias of warning"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can still have a note in the warning docstring

Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,19 @@ 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
import 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.
Expand All @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions logfire/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -102,6 +103,7 @@ def loguru_handler() -> Any:
'notice',
'info',
'warn',
'warning',
'error',
'exception',
'fatal',
Expand Down
8 changes: 6 additions & 2 deletions logfire/_internal/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
/,
Expand All @@ -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.
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_logfire.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion tests/test_logfire_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading