Skip to content

Commit

Permalink
feat(logging): using async structlog methods
Browse files Browse the repository at this point in the history
  • Loading branch information
HitaloM committed Jul 15, 2024
1 parent 8cc21df commit b78e87b
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 32 deletions.
2 changes: 1 addition & 1 deletion locales/bot.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PyKorone 1.0\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-07-15 19:42-0300\n"
"POT-Creation-Date: 2024-07-15 19:58-0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down
2 changes: 1 addition & 1 deletion src/korone/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async def main() -> None:
config = ConfigManager()

if sentry_dsn := config.get("korone", "SENTRY_DSN"):
logger.info("Initializing Sentry integration")
await logger.ainfo("Initializing Sentry integration")
sentry_sdk.init(dsn=sentry_dsn, release=__version__)

params = AppParameters(
Expand Down
4 changes: 2 additions & 2 deletions src/korone/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async def start(self) -> None:

await load_all_modules(self)

logger.info(
await logger.ainfo(
"Korone v%s running with Hydrogram v%s (Layer %s) started on @%s. Hi!",
__version__,
hydrogram.__version__,
Expand All @@ -85,4 +85,4 @@ async def start(self) -> None:

async def stop(self) -> None:
await super().stop()
logger.info("Korone stopped.")
await logger.ainfo("Korone stopped.")
6 changes: 3 additions & 3 deletions src/korone/database/sqlite/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def _execute(
msg = "Connection is not open."
raise RuntimeError(msg)

logger.debug("Executing SQL: %s with parameters: %s", sql, parameters)
await logger.adebug("Executing SQL: %s with parameters: %s", sql, parameters)

return await (
self._conn.executescript(sql) if script else self._conn.execute(sql, parameters)
Expand All @@ -57,7 +57,7 @@ async def connect(self) -> None:
raise RuntimeError(msg)

if not Path(self._path).parent.exists():
logger.info("Creating database directory")
await logger.ainfo("Creating database directory")
Path(self._path).parent.mkdir(parents=True, exist_ok=True)

self._conn = await aiosqlite.connect(self._path, *self._args, **self._kwargs)
Expand Down Expand Up @@ -89,7 +89,7 @@ async def vacuum(self) -> None:
msg = "Connection is not yet open."
raise RuntimeError(msg)

logger.debug("Running VACUUM on the database.")
await logger.adebug("Running VACUUM on the database.")
if self._conn:
await self._conn.execute("VACUUM;")
await self._conn.commit()
12 changes: 8 additions & 4 deletions src/korone/database/sqlite/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async def insert(self, fields: Document) -> None:

sql = f"INSERT INTO {self._table} ({keys}) VALUES ({placeholders})"

logger.debug("Inserting into table %s: %s", self._table, fields)
await logger.adebug("Inserting into table %s: %s", self._table, fields)

await self._conn.execute(sql, values)
await self._conn.commit()
Expand All @@ -38,7 +38,9 @@ async def query(self, query: Query) -> Documents:
clause, data = query.compile()
sql = f"SELECT * FROM {self._table} WHERE {clause}"

logger.debug("Querying table %s with clause: %s and data: %s", self._table, clause, data)
await logger.adebug(
"Querying table %s with clause: %s and data: %s", self._table, clause, data
)

cursor: aiosqlite.Cursor = await self._conn.execute(sql, data)
rows = await cursor.fetchall()
Expand All @@ -60,7 +62,9 @@ async def update(self, fields: Document, query: Query) -> None:
clause, data = query.compile()
sql = f"UPDATE {self._table} SET {assignments} WHERE {clause}"

logger.debug("Updating table %s with fields: %s and query: %s", self._table, fields, query)
await logger.adebug(
"Updating table %s with fields: %s and query: %s", self._table, fields, query
)

await self._conn.execute(sql, (*values, *data))
await self._conn.commit()
Expand All @@ -69,7 +73,7 @@ async def delete(self, query: Query) -> None:
clause, data = query.compile()
sql = f"DELETE FROM {self._table} WHERE {clause}"

logger.debug("Deleting from table %s with query: %s", self._table, query)
await logger.adebug("Deleting from table %s with query: %s", self._table, query)

await self._conn.execute(sql, data)
await self._conn.commit()
10 changes: 5 additions & 5 deletions src/korone/modules/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ async def update_commands(commands: list[str]) -> None:

if command_state:
for each in command_state:
logger.debug(
await logger.adebug(
"Fetched chat state from the database: %s => %s",
each["chat_id"],
bool(each["state"]),
)
COMMANDS[parent]["chat"][each["chat_id"]] = bool(each["state"])

logger.debug("New command node for '%s'", parent, node=COMMANDS[parent])
await logger.adebug("New command node for '%s'", parent, node=COMMANDS[parent])


async def register_handler(client: Client, module: ModuleType) -> bool:
Expand All @@ -131,7 +131,7 @@ async def register_handler(client: Client, module: ModuleType) -> bool:
async def load_module(client: Client, module: tuple) -> bool:
module_name: str = module[0]
try:
logger.debug("Loading module: %s", module_name)
await logger.adebug("Loading module: %s", module_name)
for handler in module[1]["handlers"]:
component = import_module(f".{handler}", "korone.modules")
if not await register_handler(client, component):
Expand All @@ -150,5 +150,5 @@ async def load_all_modules(client: Client) -> None:
if await load_module(client, module):
count += 1
except (TypeError, ModuleNotFoundError):
logger.exception("Could not load module: %s", module[0])
logger.info("Loaded %d modules", count)
await logger.aexception("Could not load module: %s", module[0])
await logger.ainfo("Loaded %d modules", count)
2 changes: 1 addition & 1 deletion src/korone/modules/lastfm/utils/collage_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async def fetch_image(url: str) -> Image.Image | None:
response.raise_for_status()
return Image.open(io.BytesIO(response.content))
except Exception:
logger.exception("Failed to fetch LastFM image")
await logger.aexception("Failed to fetch LastFM image")
return None


Expand Down
4 changes: 2 additions & 2 deletions src/korone/modules/medias/utils/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async def get(self) -> dict | None:
if cache_data:
return pickle.loads(cache_data)
except CacheError as e:
logger.exception("Failed to get data from cache: %s", e)
await logger.aexception("Failed to get data from cache: %s", e)
return None

async def set(self, value: Message | list[Message], expire: int) -> None:
Expand All @@ -59,4 +59,4 @@ async def set(self, value: Message | list[Message], expire: int) -> None:
serialized_cache = pickle.dumps(serialized_data)
await cache.set(self.key, serialized_cache, expire=expire)
except CacheError as e:
logger.exception("Failed to set data to cache: %s", e)
await logger.aexception("Failed to set data to cache: %s", e)
2 changes: 1 addition & 1 deletion src/korone/modules/medias/utils/tiktok.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async def _url_to_binary_io(url: str, video: bool = True) -> BinaryIO:
response.raise_for_status()
content = await response.aread()
except httpx.HTTPError as err:
logger.error(f"Error fetching media data: {err}")
await logger.aexception(f"Error fetching media data: {err}")
raise TikTokError from err

file = io.BytesIO(content)
Expand Down
4 changes: 2 additions & 2 deletions src/korone/modules/medias/utils/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async def _fetch(self, url: str) -> dict:
response.raise_for_status()
return response.json()
except httpx.HTTPError as err:
logger.error(f"Error fetching tweet data: {err}")
await logger.aexception(f"Error fetching tweet data: {err}")
raise TwitterError from err

async def _parse_data(self, data: dict) -> TweetData:
Expand Down Expand Up @@ -148,7 +148,7 @@ async def _url_to_binary_io(url: str) -> BinaryIO:
response.raise_for_status()
content = await response.aread()
except httpx.HTTPError as err:
logger.error(f"Error fetching media data: {err}")
await logger.aexception(f"Error fetching media data: {err}")
raise TwitterError from err

file = io.BytesIO(content)
Expand Down
6 changes: 3 additions & 3 deletions src/korone/modules/medias/utils/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async def process_url(self, url: str) -> dict[str, Any] | None:
self.file_path = Path(ydl.prepare_filename(info)).as_posix()
return info
except yt_dlp.DownloadError as err:
logger.exception("Error downloading content from '%s'", url)
await logger.aexception("Error downloading content from '%s'", url)
raise DownloadError(err.msg) from err

async def _download(self, url: str, options: dict[str, Any]) -> VideoInfo:
Expand All @@ -67,7 +67,7 @@ async def _download(self, url: str, options: dict[str, Any]) -> VideoInfo:
msg = "Failed to download content!"
raise DownloadError(msg)

logger.debug("Download completed for %s, saved to %s", url, self.file_path)
await logger.adebug("Download completed for %s, saved to %s", url, self.file_path)

return await self.generate_videoinfo(info)

Expand All @@ -83,7 +83,7 @@ async def get_info(self, url: str, options: dict[str, Any]) -> VideoInfo:
msg = "Failed to extract video info!"
raise InfoExtractionError(msg)

logger.debug("Information extracted for %s", url)
await logger.adebug("Information extracted for %s", url)
return await self.generate_videoinfo(info)

async def generate_videoinfo(self, info: dict[str, Any]) -> VideoInfo:
Expand Down
14 changes: 7 additions & 7 deletions src/korone/modules/translator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,38 +67,38 @@ async def translate_text(
except httpx.HTTPStatusError as e:
status_code = e.response.status_code
if status_code == 429:
logger.debug(
await logger.adebug(
"[DeepL] Rate limit exceeded, retrying in %s seconds...", backoff
)
await asyncio.sleep(backoff)
retries += 1
backoff *= 2
elif status_code == 456:
msg = "Quota exceeded."
logger.error(msg)
await logger.aexception(msg)
raise QuotaExceededError(msg) from e
elif status_code == 500:
logger.debug(
await logger.adebug(
"[DeepL] Internal server error, retrying in %s seconds...", backoff
)
await asyncio.sleep(backoff)
retries += 1
backoff *= 2
else:
msg = f"HTTP error occurred: {status_code}"
logger.error(msg)
await logger.aexception(msg)
raise TranslationError(msg) from e

except (KeyError, IndexError) as e:
msg = "Failed to parse translation response."
logger.error(msg)
await logger.aexception(msg)
raise TranslationError(msg) from e

except Exception as e:
msg = f"Unexpected error: {e!s}"
logger.error(msg)
await logger.aexception(msg)
raise TranslationError(msg) from e

msg = "Maximum retries exceeded."
logger.error(msg)
await logger.aexception(msg)
raise TranslationError(msg)

0 comments on commit b78e87b

Please sign in to comment.