import logging
from typing import Any
from banhxeo.utils import RuntimeEnv, get_runtime
[docs]
class PrintLogger: # fix Colab issues
template: str = "[{level}]: {msg}"
[docs]
def info(self, msg: str):
print(self.template.format(level="INFO", msg=msg))
[docs]
def debug(self, msg: str):
print(self.template.format(level="DEBUG", msg=msg))
[docs]
def exception(self, msg: str):
print(self.template.format(level="EXCEPTION", msg=msg))
[docs]
def error(self, msg: str):
print(self.template.format(level="ERROR", msg=msg))
[docs]
def warning(self, msg: str):
print(self.template.format(level="WARNING", msg=msg))
# Set up Logger
[docs]
class Logger:
[docs]
def __init__(self):
if get_runtime() == RuntimeEnv.COLAB:
self.base = PrintLogger()
else:
self.base = logging.getLogger("banhxeo")
logging_handler = []
logFormatter = logging.Formatter("%(asctime)s [%(levelname)s]: %(message)s")
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
logging_handler.append(consoleHandler)
# set up config
logging.basicConfig(
level="NOTSET",
format="%(message)s",
datefmt="[%Y-%m-%d-%H:%M:%S%z]",
handlers=logging_handler,
)
[docs]
def info(self, msg: str):
self.base.info(msg)
[docs]
def debug(self, msg: str):
self.base.debug(msg)
[docs]
def exception(self, msg: str):
self.base.exception(msg)
[docs]
def error(self, msg: str):
self.base.error(msg)
[docs]
def warning(self, msg: str):
self.base.warning(msg)
[docs]
def check_and_raise(self, msg: str, error_type: Exception, condition: Any):
if not condition:
self.error(msg)
raise error_type
DEFAULT_LOGGER = Logger()