From 9a8c286edf71911de141254b638f6f23afecf5c5 Mon Sep 17 00:00:00 2001 From: e Date: Fri, 11 Sep 2020 10:24:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=8D=A2log=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E7=9A=84=E8=AE=B0=E5=BD=95=E6=A8=A1=E5=9D=97=EF=BC=8C=E5=9C=A8?= =?UTF-8?q?spec=E6=96=87=E4=BB=B6=E4=B8=AD=E6=B7=BB=E5=8A=A0concurrent-log?= =?UTF-8?q?-handler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../packageship/application/__init__.py | 6 +- packageship/packageship/libs/log/loghelper.py | 57 +++++++++++++------ packageship/pkgship.spec | 3 +- 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/packageship/packageship/application/__init__.py b/packageship/packageship/application/__init__.py index 7356b541..13610583 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 ee557d7e..362175c1 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 8a7adbc5..65f3d75a 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 -- Gitee