Coverage for src/melvonaut/utils.py: 78%

26 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-08 09:36 +0000

1from apprise import NotifyType 

2from loguru import logger 

3from shared import constants as con 

4from melvonaut.settings import settings 

5import sys 

6import apprise 

7from typing import Any 

8 

9file_log_handler_id = None 

10 

11## [Logging] 

12 

13 

14@apprise.decorators.notify(on="melvin") # type: ignore 

15def melvin_notifier( 

16 body: str, title: str, notify_type: NotifyType, *args: Any, **kwargs: dict[str, Any] 

17) -> None: 

18 """Melvin-specific notification handler. 

19 

20 Just prints to stdout. 

21 

22 Args: 

23 body (str): The message body. 

24 title (str): The notification title. 

25 notify_type (NotifyType): The type of notification. 

26 *args (Any): Additional arguments. 

27 **kwargs (dict[str, Any]): Additional keyword arguments. 

28 """ 

29 print("MELVIN HERE!") 

30 

31 

32def setup_logging() -> None: 

33 """Configures the logging system for the application. 

34 

35 This function removes existing log handlers, sets up terminal logging, 

36 and configures Apprise notifications for Discord and Melvin events. 

37 """ 

38 logger.remove() 

39 logger.add( 

40 sink=sys.stdout, 

41 level=settings.TERMINAL_LOGGING_LEVEL, 

42 backtrace=True, 

43 diagnose=True, 

44 enqueue=True, 

45 ) 

46 notifier = apprise.Apprise() 

47 if settings.DISCORD_WEBHOOK_TOKEN and settings.DISCORD_ALERTS_ENABLED: 47 ↛ 48line 47 didn't jump to line 48 because the condition on line 47 was never true

48 notifier.add(f"discord://{settings.DISCORD_WEBHOOK_TOKEN}") 

49 logger.add(notifier.notify, level="ERROR", filter={"apprise": False}) # type: ignore 

50 

51 if settings.NETWORK_SIM_ENABLED: 51 ↛ 52line 51 didn't jump to line 52 because the condition on line 51 was never true

52 notifier.add("melvin://") 

53 logger.add(notifier.notify, level="ERROR", filter={"apprise": False}) # type: ignore 

54 

55 setup_file_logging() 

56 

57 

58def setup_file_logging() -> None: 

59 """Configures file-based logging with rotation at midnight. 

60 

61 If a file log handler already exists, it is removed before adding a new one. 

62 """ 

63 global file_log_handler_id 

64 if file_log_handler_id is not None: 

65 logger.remove(file_log_handler_id) # type: ignore 

66 file_log_handler_id = logger.add( 

67 sink=con.MEL_LOG_LOCATION, 

68 rotation="00:00", 

69 level=settings.FILE_LOGGING_LEVEL, 

70 backtrace=True, 

71 diagnose=True, 

72 enqueue=True, 

73 )