Skip to content

Commit

Permalink
Add logfire.warning
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobHayes committed Jan 15, 2025
1 parent c2806ef commit 62f4d55
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 5 deletions.
3 changes: 3 additions & 0 deletions logfire-api/logfire_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def info(self, *args, **kwargs) -> None: ...

def warn(self, *args, **kwargs) -> None: ...

def warning(self, *args, **kwargs) -> None: ...

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

def exception(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
22 changes: 22 additions & 0 deletions logfire-api/logfire_api/_internal/main.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,28 @@ class Logfire:
logfire.warn('This is a warning log')
```
This is an alias of `logfire.warning`.
Args:
msg_template: The message to log.
attributes: The attributes to bind to the log.
_tags: An optional sequence of tags to include in the log.
_exc_info: Set to an exception or a tuple as returned by [`sys.exc_info()`][sys.exc_info]
to record a traceback with the log message.
Set to `True` to use the currently handled exception.
"""
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.warning('This is a warning log')
```
Args:
msg_template: The message to log.
attributes: The attributes to bind to the log.
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
32 changes: 32 additions & 0 deletions logfire/_internal/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,38 @@ def warn(
logfire.warn('This is a warning log')
```
This is an alias of `logfire.warning`.
Args:
msg_template: The message to log.
attributes: The attributes to bind to the log.
_tags: An optional sequence of tags to include in the log.
_exc_info: Set to an exception or a tuple as returned by [`sys.exc_info()`][sys.exc_info]
to record a traceback with the log message.
Set to `True` to use the currently handled exception.
"""
self.warning(msg_template, _tags=_tags, _exc_info=_exc_info, **attributes)

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.warning('This is a warning log')
```
Args:
msg_template: The message to log.
attributes: The attributes to bind to the log.
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

0 comments on commit 62f4d55

Please sign in to comment.