Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
dycw committed Sep 13, 2024
1 parent 255c1f9 commit 2bf19ee
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
21 changes: 21 additions & 0 deletions src/tests/test_loguru.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from re import search
from typing import TYPE_CHECKING, Any, ClassVar, cast

from hypothesis import given
from loguru import logger
from loguru._defaults import LOGURU_FORMAT
from loguru._recattrs import RecordFile, RecordLevel, RecordProcess, RecordThread
Expand All @@ -27,6 +28,7 @@
func_test_exit_predicate,
func_test_exit_sync,
)
from utilities.hypothesis import text_ascii
from utilities.loguru import (
LEVEL_CONFIGS,
GetLoggingLevelNumberError,
Expand All @@ -41,6 +43,8 @@
logged_sleep_sync,
make_except_hook,
make_filter,
make_slack_sink,
make_slack_sink_async,
)
from utilities.text import ensure_str, strip_and_dedent

Expand Down Expand Up @@ -781,3 +785,20 @@ def _record(self) -> Record:
),
}
return cast(Any, record)


class TestMakeSlackSink:
@given(url=text_ascii())
def test_sync(self, *, url: str) -> None:
sink = make_slack_sink(url)
handler: HandlerConfiguration = {"sink": sink, "level": LogLevel.TRACE}
_ = logger.configure(handlers=[cast(dict[str, Any], handler)])
logger.trace("message")

@given(url=text_ascii())
async def test_async(self, *, url: str) -> None:
sink = make_slack_sink_async(url)
handler: HandlerConfiguration = {"sink": sink, "level": LogLevel.TRACE}
_ = logger.configure(handlers=[cast(dict[str, Any], handler)])
logger.trace("message")
await logger.complete()
2 changes: 1 addition & 1 deletion src/utilities/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from __future__ import annotations

__version__ = "0.54.1"
__version__ = "0.54.2"
32 changes: 30 additions & 2 deletions src/utilities/loguru.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from functools import partial, wraps
from inspect import iscoroutinefunction
from logging import Handler, LogRecord
from sys import __excepthook__, _getframe
from sys import __excepthook__, _getframe, stderr
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -53,7 +53,7 @@
Writable,
)

from utilities.asyncio import MaybeCoroutine1
from utilities.asyncio import Coroutine1, MaybeCoroutine1
from utilities.iterables import MaybeIterable
from utilities.types import Duration, PathLike, StrMapping

Expand Down Expand Up @@ -429,6 +429,32 @@ def filter_func(record: Record, /) -> bool:
return filter_func


def make_slack_sink(url: str, /) -> Callable[[Message], None]:
"""Make a `slack` sink."""
from utilities.slack_sdk import SendSlackError, send_slack_sync

def sink_sync(message: Message, /) -> None:
try:
send_slack_sync(message, url=url)
except SendSlackError as error:
_ = stderr.write(f"{error}\n") # pragma: no cover

return sink_sync


def make_slack_sink_async(url: str, /) -> Callable[[Message], Coroutine1[None]]:
"""Make an asynchronous `slack` sink."""
from utilities.slack_sdk import SendSlackError, send_slack_async

async def sink_async(message: Message, /) -> None:
try:
await send_slack_async(message, url=url)
except SendSlackError as error:
_ = stderr.write(f"{error}\n") # pragma: no cover

return sink_async


__all__ = [
"LEVEL_CONFIGS",
"GetLoggingLevelNameError",
Expand All @@ -443,4 +469,6 @@ def filter_func(record: Record, /) -> bool:
"logged_sleep_sync",
"make_except_hook",
"make_filter",
"make_slack_sink",
"make_slack_sink_async",
]

0 comments on commit 2bf19ee

Please sign in to comment.