diff --git a/bot/__init__.py b/bot/__init__.py index a0bfa961f..ede988ac4 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -3,6 +3,7 @@ ERROR, INFO, FileHandler, + LogRecord, Formatter, StreamHandler, basicConfig, @@ -31,6 +32,10 @@ from tzlocal import get_localzone from uvloop import install +from datetime import datetime + +from pytz import timezone + # from faulthandler import enable as faulthandler_enable # faulthandler_enable() @@ -51,13 +56,21 @@ class CustomFormatter(Formatter): - def format(self, record): + def formatTime( # noqa: N802 + self: CustomFormatter, record: LogRecord, datefmt: str | None + ) -> str: + dt: datetime = datetime.fromtimestamp( + record.created, tz=timezone("Asia/Dhaka") + ) + return dt.strftime(datefmt) + + def format(self: CustomFormatter, record: LogRecord) -> str: return super().format(record).replace(record.levelname, record.levelname[:1]) formatter = CustomFormatter( - "[%(asctime)s] [%(levelname)s] - %(message)s", - datefmt="%d-%b-%y %I:%M:%S %p", + "[%(asctime)s] %(levelname)s - %(message)s [%(module)s:%(lineno)d]", + datefmt="%d-%b %I:%M:%S %p", ) file_handler = FileHandler("log.txt") diff --git a/update.py b/update.py index 797684439..6076e4e93 100644 --- a/update.py +++ b/update.py @@ -5,6 +5,7 @@ FileHandler, Formatter, StreamHandler, + LogRecord, basicConfig, error, getLogger, @@ -17,22 +18,28 @@ from pymongo.mongo_client import MongoClient from pymongo.server_api import ServerApi from pytz import timezone +from datetime import datetime class CustomFormatter(Formatter): - def formatTime(self, record, datefmt): - dt = datetime.fromtimestamp(record.created, tz=timezone("Asia/Dhaka")) + def formatTime( # noqa: N802 + self: CustomFormatter, record: LogRecord, datefmt: str | None + ) -> str: + dt: datetime = datetime.fromtimestamp( + record.created, tz=timezone("Asia/Dhaka") + ) return dt.strftime(datefmt) - def format(self, record): + def format(self: CustomFormatter, record: LogRecord) -> str: return super().format(record).replace(record.levelname, record.levelname[:1]) formatter = CustomFormatter( - "[%(asctime)s] [%(levelname)s] %(message)s | [%(module)s:%(lineno)d]", + "[%(asctime)s] %(levelname)s - %(message)s [%(module)s:%(lineno)d]", datefmt="%d-%b %I:%M:%S %p", ) + file_handler = FileHandler("log.txt") file_handler.setFormatter(formatter)