Skip to content

Commit

Permalink
fix: add types to custom logger
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelrince committed Dec 13, 2024
1 parent 1bf31bd commit 555a18d
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions ecologits/log.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
import logging
from typing import Any, Union, cast


class EcoLogitsLogger(logging.Logger):

def __init__(self, name, level=logging.NOTSET):
def __init__(self, name: str, level: Union[int, str] = logging.NOTSET) -> None:
super().__init__(name, level)
self.__once_messages = set()
self.__once_messages: set[str] = set()

def _log_once(self, level, msg, *args, **kwargs):
def _log_once(self, level: int, msg: str, *args: Any, **kwargs: Any) -> None:
if msg not in self.__once_messages:
self.__once_messages.add(msg)
self.log(level, msg, *args, **kwargs)

def debug_once(self, msg, *args, **kwargs):
def debug_once(self, msg: str, *args: Any, **kwargs: Any) -> None:
self._log_once(logging.DEBUG, msg, *args, **kwargs)

def info_once(self, msg, *args, **kwargs):
def info_once(self, msg: str, *args: Any, **kwargs: Any) -> None:
self._log_once(logging.INFO, msg, *args, **kwargs)

def warning_once(self, msg, *args, **kwargs):
def warning_once(self, msg: str, *args: Any, **kwargs: Any) -> None:
self._log_once(logging.WARNING, msg, *args, **kwargs)

def error_once(self, msg, *args, **kwargs):
def error_once(self, msg: str, *args: Any, **kwargs: Any) -> None:
self._log_once(logging.ERROR, msg, *args, **kwargs)

def critical_once(self, msg, *args, **kwargs):
def critical_once(self, msg: str, *args: Any, **kwargs: Any) -> None:
self._log_once(logging.CRITICAL, msg, *args, **kwargs)


logging.setLoggerClass(EcoLogitsLogger)
logger = logging.getLogger(__name__)
logger: EcoLogitsLogger = cast(EcoLogitsLogger, logging.getLogger(__name__))

0 comments on commit 555a18d

Please sign in to comment.