diff --git a/packageship/packageship/application/__init__.py b/packageship/packageship/application/__init__.py index 7356b541ba24cc2a79da61cca6cdcf6aa1760aad..136105832b15c9825b6173001e935d668f14ed19 100644 --- a/packageship/packageship/application/__init__.py +++ b/packageship/packageship/application/__init__.py @@ -17,14 +17,14 @@ OPERATION = None def _timed_task(app): """ - + Timed task function """ from .apps.lifecycle.function.download_yaml import update_pkg_info # pylint: disable=import-outside-toplevel _readconfig = ReadConfig(system_config.SYS_CONFIG_PATH) try: - _hour = int(_readconfig.get_config('TIMEDTASK', 'hour')) - _minute = int(_readconfig.get_config('TIMEDTASK', 'minute')) + _hour = int(_readconfig.get_config('TIMEDTASK', 'hour') or 3) + _minute = int(_readconfig.get_config('TIMEDTASK', 'minute') or 0) except ValueError: _hour = 3 _minute = 0 diff --git a/packageship/packageship/libs/log/loghelper.py b/packageship/packageship/libs/log/loghelper.py index ee557d7e52c8103260e163035b5ef6f021e6309e..362175c123369b7afecb0ee44da19c5414ddcb9c 100644 --- a/packageship/packageship/libs/log/loghelper.py +++ b/packageship/packageship/libs/log/loghelper.py @@ -3,9 +3,10 @@ Logging related """ import os +import threading import pathlib import logging -from logging.handlers import RotatingFileHandler +from concurrent_log_handler import ConcurrentRotatingFileHandler from packageship import system_config from packageship.libs.configutils.readconfig import ReadConfig @@ -44,7 +45,7 @@ def setup_log(config=None): except FileExistsError: pathlib.Path(path).touch() - file_log_handler = RotatingFileHandler( + file_log_handler = ConcurrentRotatingFileHandler( path, maxBytes=max_bytes, backupCount=backup_count) formatter = logging.Formatter( @@ -53,12 +54,13 @@ def setup_log(config=None): file_log_handler.setFormatter(formatter) return file_log_handler - + class Log(): """ General log operations """ + _instance_lock = threading.Lock() def __init__(self, name=__name__, path=None): self.__name = name @@ -88,15 +90,30 @@ class Log(): self.__level = 'INFO' self.__logger = logging.getLogger(self.__name) self.__logger.setLevel(self.__level) - self.backup_count = READCONFIG.get_config('LOG', 'backup_count') - if not self.backup_count or not isinstance(self.backup_count, int): + self.backup_count = READCONFIG.get_config('LOG', 'backup_count') or 10 + self.max_bytes = READCONFIG.get_config('LOG', 'max_bytes') or 314572800 + try: + self.backup_count = int(self.backup_count) + self.max_bytes = int(self.max_bytes) + except ValueError: self.backup_count = 10 - self.max_bytes = READCONFIG.get_config('LOG', 'max_bytes') - if not self.max_bytes or not isinstance(self.max_bytes, int): self.max_bytes = 314572800 + self.__init_handler() + self.__set_handler() + self.__set_formatter() + + def __new__(cls, *args, **kwargs): # pylint: disable=unused-argument + """ + Use the singleton pattern to create a thread-safe producer pattern + """ + if not hasattr(cls, "_instance"): + with cls._instance_lock: + if not hasattr(cls, "_instance"): + cls._instance = object.__new__(cls) + return cls._instance def __init_handler(self): - self.__file_handler = RotatingFileHandler( + self.__file_handler = ConcurrentRotatingFileHandler( self.__path, maxBytes=self.max_bytes, backupCount=self.backup_count, encoding="utf-8") def __set_handler(self): @@ -109,19 +126,25 @@ class Log(): datefmt='%a, %d %b %Y %H:%M:%S') self.__file_handler.setFormatter(formatter) - def close_handler(self): - """ - Turn off log processing - """ - self.__file_handler.close() + def info(self, message): + """General information printing of the log""" + self.__logger.info(message) + + def debug(self, message): + """Log debugging information printing""" + self.__logger.debug(message) + + def warning(self, message): + """Log warning message printing""" + self.__logger.warning(message) + + def error(self, message): + """Log error message printing""" + self.__logger.error(message) @property def logger(self): """ Get logs """ - self.__init_handler() - self.__set_handler() - self.__set_formatter() - self.close_handler() return self.__logger diff --git a/packageship/pkgship.spec b/packageship/pkgship.spec index 8a7adbc523ec4041af8e2dd89dbd3f67079bb783..65f3d75ac5aa2c0eff5ba343d330618fe2453d1e 100644 --- a/packageship/pkgship.spec +++ b/packageship/pkgship.spec @@ -11,8 +11,9 @@ BuildArch: noarch BuildRequires: python3-flask-restful python3-flask python3 python3-pyyaml python3-sqlalchemy BuildRequires: python3-prettytable python3-requests python3-flask-session python3-flask-script python3-marshmallow BuildRequires: python3-Flask-APScheduler python3-pandas python3-retrying python3-xlrd python3-XlsxWriter +BuildRequires: python3-concurrent-log-handler Requires: python3-pip python3-flask-restful python3-flask python3 python3-pyyaml -Requires: python3-sqlalchemy python3-prettytable python3-requests +Requires: python3-sqlalchemy python3-prettytable python3-requests python3-concurrent-log-handler Requires: python3-flask-session python3-flask-script python3-marshmallow python3-uWSGI Requires: python3-pandas python3-dateutil python3-XlsxWriter python3-xlrd python3-Flask-APScheduler python3-retrying