Skip to content

Commit

Permalink
Better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
5hojib committed Dec 7, 2024
1 parent 11ace94 commit 486f7f5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
19 changes: 16 additions & 3 deletions bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
ERROR,
INFO,
FileHandler,
LogRecord,
Formatter,
StreamHandler,
basicConfig,
Expand Down Expand Up @@ -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()

Expand All @@ -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")
Expand Down
15 changes: 11 additions & 4 deletions update.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
FileHandler,
Formatter,
StreamHandler,
LogRecord,
basicConfig,
error,
getLogger,
Expand All @@ -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)

Expand Down

0 comments on commit 486f7f5

Please sign in to comment.