2023-03-27 19:40:52 -07:00
|
|
|
|
|
|
|
import logging
|
2023-03-27 20:12:37 -07:00
|
|
|
from logging.handlers import SysLogHandler
|
|
|
|
|
2023-03-27 19:59:45 -07:00
|
|
|
import sys
|
2023-03-27 19:40:52 -07:00
|
|
|
|
2024-06-09 06:42:28 -07:00
|
|
|
|
2023-03-27 19:40:52 -07:00
|
|
|
class Logger:
|
|
|
|
|
2024-06-09 06:51:51 -07:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
name: str,
|
|
|
|
debug: bool = False,
|
|
|
|
write_to_syslog: bool = False,
|
2024-06-22 15:31:31 -07:00
|
|
|
systemd: bool = False,
|
|
|
|
do_test_logs: bool = True,
|
2024-06-09 06:51:51 -07:00
|
|
|
):
|
2023-03-27 19:40:52 -07:00
|
|
|
|
|
|
|
self.__name = name
|
2024-06-09 06:51:51 -07:00
|
|
|
self.__debug = debug
|
|
|
|
self.__write_to_syslog = write_to_syslog
|
|
|
|
self.__systemd = systemd
|
2024-06-22 15:31:31 -07:00
|
|
|
self.__do_test_logs = do_test_logs
|
2024-06-09 06:51:51 -07:00
|
|
|
|
|
|
|
self._init_logger()
|
|
|
|
|
|
|
|
def _init_logger(self):
|
2023-03-27 19:59:45 -07:00
|
|
|
|
|
|
|
self.__logger = logging.getLogger(self.__name)
|
2024-06-09 06:51:51 -07:00
|
|
|
|
|
|
|
if self.__debug:
|
2023-03-27 19:59:45 -07:00
|
|
|
level = logging.DEBUG
|
|
|
|
else:
|
|
|
|
level = logging.INFO
|
|
|
|
|
|
|
|
self.__logger.setLevel(level)
|
|
|
|
|
2024-06-09 06:42:28 -07:00
|
|
|
formatter = logging.Formatter(
|
|
|
|
fmt="[{name}][{levelname:<7}] {message}",
|
|
|
|
style='{'
|
|
|
|
)
|
|
|
|
formatter_full = logging.Formatter(
|
|
|
|
fmt="[{asctime}][{name}][{levelname:<7}] {message}",
|
|
|
|
style='{'
|
|
|
|
)
|
2023-03-27 20:12:37 -07:00
|
|
|
|
|
|
|
# Console output / stream handler (STDOUT)
|
|
|
|
handler = logging.StreamHandler(
|
|
|
|
stream=sys.stdout
|
|
|
|
)
|
|
|
|
handler.setLevel(level)
|
|
|
|
handler.addFilter(lambda entry: entry.levelno <= logging.INFO)
|
2024-06-09 06:51:51 -07:00
|
|
|
handler.setFormatter(
|
|
|
|
formatter if self.__systemd else formatter_full
|
|
|
|
)
|
2023-03-27 20:12:37 -07:00
|
|
|
self.__logger.addHandler(handler)
|
|
|
|
|
|
|
|
# Console output / stream handler (STDERR)
|
|
|
|
handler = logging.StreamHandler(
|
|
|
|
stream=sys.stderr
|
|
|
|
)
|
|
|
|
handler.setLevel(logging.WARNING)
|
2024-06-09 06:51:51 -07:00
|
|
|
handler.setFormatter(
|
|
|
|
formatter if self.__systemd else formatter_full
|
|
|
|
)
|
2023-03-27 20:12:37 -07:00
|
|
|
self.__logger.addHandler(handler)
|
2023-03-27 19:59:45 -07:00
|
|
|
|
2023-03-27 20:12:37 -07:00
|
|
|
# Syslog handler
|
2024-06-09 06:51:51 -07:00
|
|
|
if self.__write_to_syslog:
|
|
|
|
handler = SysLogHandler(
|
|
|
|
address="/dev/log"
|
|
|
|
)
|
|
|
|
handler.setLevel(level)
|
|
|
|
handler.setFormatter(formatter)
|
|
|
|
self.__logger.addHandler(handler)
|
2023-03-27 20:12:37 -07:00
|
|
|
|
2023-03-27 20:17:45 -07:00
|
|
|
# This is annoying inside cron
|
2024-06-22 15:31:31 -07:00
|
|
|
if self.__do_test_logs:
|
|
|
|
self.debug("Test debug log")
|
|
|
|
self.info("Test info log")
|
|
|
|
self.warn("Test warn log")
|
|
|
|
self.error("Test error log")
|
2023-03-27 19:59:45 -07:00
|
|
|
|
2023-03-27 19:40:52 -07:00
|
|
|
def debug(self, s):
|
2023-03-27 19:59:45 -07:00
|
|
|
self.__logger.debug(s)
|
2024-06-09 06:42:28 -07:00
|
|
|
|
2023-03-27 19:40:52 -07:00
|
|
|
def info(self, s):
|
2023-03-27 19:59:45 -07:00
|
|
|
self.__logger.info(s)
|
2024-06-09 06:42:28 -07:00
|
|
|
|
2023-03-27 19:40:52 -07:00
|
|
|
def warn(self, s):
|
2024-06-09 06:42:28 -07:00
|
|
|
self.__logger.warning(s)
|
|
|
|
|
|
|
|
def warning(self, s):
|
|
|
|
self.__logger.warning(s)
|
|
|
|
|
2023-03-27 19:40:52 -07:00
|
|
|
def error(self, s):
|
2023-03-27 19:59:45 -07:00
|
|
|
self.__logger.error(s)
|