Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typing for logger module #7

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/driutils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import logging
import traceback
from datetime import datetime
from typing import Tuple, Type
from types import TracebackType
from typing import TypeAlias

SysExcInfoType: TypeAlias = tuple[type[BaseException], BaseException, TracebackType | None] | tuple[None, None, None]


class LogFormatter(logging.Formatter):
Expand Down Expand Up @@ -40,7 +43,7 @@ def format(self, record: logging.LogRecord) -> str:

return log_entry

def formatTime(self, record: logging.LogRecord) -> datetime:
def formatTime(self, record: logging.LogRecord, datefmt: str | None = None) -> str:
"""
Format the creation time of the specified LogRecord.

Expand All @@ -50,9 +53,9 @@ def formatTime(self, record: logging.LogRecord) -> datetime:
Returns:
datetime: A datetime object representing the record's creation time.
"""
return datetime.fromtimestamp(record.created)
return str(datetime.fromtimestamp(record.created))

def formatException(self, exc_info: Tuple[Type[BaseException], BaseException]) -> str:
def formatException(self, ei: SysExcInfoType) -> str:
"""
Format the specified exception information as a string.

Expand All @@ -62,7 +65,7 @@ def formatException(self, exc_info: Tuple[Type[BaseException], BaseException]) -
Returns:
str: A string representation of the exception traceback.
"""
return "".join(traceback.format_exception(*exc_info)).strip()
return "".join(traceback.format_exception(*ei)).strip()


def setup_logging(level: int = logging.INFO) -> None:
Expand Down
Loading