Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
chore: use logger enum
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinNitroG committed Jan 11, 2025
1 parent 24e3b74 commit 3ecd7e5
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 30 deletions.
24 changes: 14 additions & 10 deletions schemas/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
"title": "ApiEnum",
"type": "string"
},
"LogLevelEnum": {
"enum": [
"NOTSET",
"DEBUG",
"INFO",
"WARNING",
"ERROR",
"CRITICAL"
],
"title": "LogLevelEnum",
"type": "string"
},
"PlateInfo": {
"properties": {
"plate": {
Expand Down Expand Up @@ -208,18 +220,10 @@
"type": "boolean"
},
"log_level": {
"$ref": "#/$defs/LogLevelEnum",
"default": "WARNING",
"description": "Mức độ log",
"enum": [
"NOTSET",
"DEBUG",
"INFO",
"WARNING",
"ERROR",
"CRITICAL"
],
"title": "Mức độ log",
"type": "string"
"title": "Mức độ log"
}
},
"required": [
Expand Down
8 changes: 4 additions & 4 deletions src/check_phat_nguoi/config/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from pydantic import BaseModel, ConfigDict, Field

from check_phat_nguoi.types import ApiEnum, LogLevelType
from check_phat_nguoi.types import ApiEnum
from check_phat_nguoi.types.log_level import LogLevelEnum

from .notifications.telegram_notification import (
TelegramNotificationConfig,
Expand All @@ -13,7 +14,6 @@
class Config(BaseModel):
model_config = ConfigDict(
title="Config",
use_enum_values=True,
frozen=True,
)

Expand Down Expand Up @@ -60,10 +60,10 @@ class Config(BaseModel):
description="Log chi tiết",
default=False,
)
log_level: LogLevelType = Field(
log_level: LogLevelEnum = Field(
title="Mức độ log",
description="Mức độ log",
default="WARNING",
default=LogLevelEnum.warning,
)


Expand Down
2 changes: 1 addition & 1 deletion src/check_phat_nguoi/get_data/engines/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async def __aenter__(self) -> Self:

async def __aexit__(self, exc_type, exc_value, exc_traceback) -> None:
await self.session.close()
logger.debug(f"Closed data engine session: {type(self).__name__}")
logger.debug(f"Closed get data engine session: {type(self).__name__}")

@abstractmethod
async def get_data(self, plate_info: PlateInfo) -> PlateDetail | None: ...
2 changes: 2 additions & 0 deletions src/check_phat_nguoi/notify/engines/telegram.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import asyncio
from asyncio import TimeoutError
from logging import getLogger
Expand Down
6 changes: 3 additions & 3 deletions src/check_phat_nguoi/notify/send_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
class SendNotifications:
def __init__(self) -> None:
self._md_messages: tuple[MarkdownMessageDetail, ...]
self._tele: TelegramNotificationEngine
self._telegram_engine: TelegramNotificationEngine

async def _send_messages(self, notification: BaseNotificationConfig) -> None:
if isinstance(notification, TelegramNotificationConfig):
await self._tele.send(notification.telegram, self._md_messages)
await self._telegram_engine.send(notification.telegram, self._md_messages)

async def send(self) -> None:
if config.notifications is None:
Expand All @@ -38,7 +38,7 @@ async def send(self) -> None:
MarkdownMessage(plate_detail).generate_message()
for plate_detail in plates_context.plates
)
async with TelegramNotificationEngine() as self._tele:
async with TelegramNotificationEngine() as self._telegram_engine:
await gather(
*(
self._send_messages(notification)
Expand Down
4 changes: 2 additions & 2 deletions src/check_phat_nguoi/types/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .api import ApiEnum
from .log_level import LogLevelType
from .log_level import LogLevelEnum
from .vehicle_type import (
VehicleIntType,
VehicleStrType,
Expand All @@ -10,10 +10,10 @@

__all__ = [
"ApiEnum",
"LogLevelType",
"VehicleIntType",
"VehicleStrType",
"VehicleType",
"VehicleTypeEnum",
"get_vehicle_enum",
"LogLevelEnum",
]
18 changes: 9 additions & 9 deletions src/check_phat_nguoi/types/log_level.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import Literal, TypeAlias
from enum import Enum

LogLevelType: TypeAlias = Literal[
"NOTSET",
"DEBUG",
"INFO",
"WARNING",
"ERROR",
"CRITICAL",
]

class LogLevelEnum(str, Enum):
notset = "NOTSET"
debug = "DEBUG"
info = "INFO"
warning = "WARNING"
error = "ERROR"
critical = "CRITICAL"
2 changes: 1 addition & 1 deletion src/check_phat_nguoi/utils/setup_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

def setup_logger() -> None:
basicConfig(
level=config.log_level,
level=config.log_level.value,
format=DETAIL_LOG_MESSAGE if config.detail_log else SIMPLE_LOG_MESSAGE,
)

Expand Down

0 comments on commit 3ecd7e5

Please sign in to comment.