From ee64e55ecaf6b3d3137d0e61d1596debe41b1b3b Mon Sep 17 00:00:00 2001 From: Mr Chen Date: Wed, 13 Nov 2024 19:28:56 +0800 Subject: [PATCH] update --- web/admin/__init__.py | 18 +++++++++ web/config.py | 7 +++- web/utils/email.py | 11 +++++- web/utils/enhanced_log_rotation.py | 61 ++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 web/utils/enhanced_log_rotation.py diff --git a/web/admin/__init__.py b/web/admin/__init__.py index a33c569ff..dc2770ff0 100644 --- a/web/admin/__init__.py +++ b/web/admin/__init__.py @@ -171,6 +171,24 @@ def webssh(data): shell.run(data) return +# File logging +logger = logging.getLogger('werkzeug') +logger.setLevel(config.CONSOLE_LOG_LEVEL) + +from utils.enhanced_log_rotation import EnhancedRotatingFileHandler +fh = EnhancedRotatingFileHandler(config.LOG_FILE, + config.LOG_ROTATION_SIZE, + config.LOG_ROTATION_AGE, + config.LOG_ROTATION_MAX_LOG_FILES) +fh.setLevel(config.FILE_LOG_LEVEL) +app.logger.addHandler(fh) +logger.addHandler(fh) + +# Console logging +ch = logging.StreamHandler() +ch.setLevel(config.CONSOLE_LOG_LEVEL) +ch.setFormatter(logging.Formatter(config.CONSOLE_LOG_FORMAT)) + # Log the startup app.logger.info('########################################################') app.logger.info('Starting %s v%s...', config.APP_NAME, config.APP_VERSION) diff --git a/web/config.py b/web/config.py index 82aa42513..60d4b98f2 100644 --- a/web/config.py +++ b/web/config.py @@ -46,11 +46,14 @@ DATA_DIR = mw.getPanelDataDir() # 日志文件名。这将进入数据目录,服务器模式下的非Windows平台除外。 LOG_FILE = os.path.join(mw.getMWLogs(), APP_LOG_NAME + '.log') +CONSOLE_LOG_FORMAT = '%(asctime)s: %(levelname)s\t%(name)s:\t%(message)s' +FILE_LOG_FORMAT = '%(asctime)s: %(levelname)s\t%(name)s:\t%(message)s' + # 日志旋转设置日志文件将根据LOG_ROTATION_SIZE和LOG_ROTATION_AGE的值进行切换。 # 旋转的文件将以格式命名Y-m-d_H-M-S -LOG_ROTATION_SIZE = 10 # MBs +LOG_ROTATION_SIZE = 1 # MBs LOG_ROTATION_AGE = 1440 # minutes -LOG_ROTATION_MAX_LOG_FILES = 90 # 要保留的最大备份数 +LOG_ROTATION_MAX_LOG_FILES = 5 # 要保留的最大备份数 # 用于存储用户帐户和设置的SQLite数据库的默认路径。 # 此默认设置将文件放置在与此相同的目录中 配置文件,但会生成一个在整个应用程序中使用的绝对路径。 diff --git a/web/utils/email.py b/web/utils/email.py index 6ba7b62d5..9112622cb 100644 --- a/web/utils/email.py +++ b/web/utils/email.py @@ -1,4 +1,13 @@ -# coding: utf-8 +# coding:utf-8 + +# --------------------------------------------------------------------------------- +# MW-Linux面板 +# --------------------------------------------------------------------------------- +# copyright (c) 2018-∞(https://github.com/midoks/mdserver-web) All rights reserved. +# --------------------------------------------------------------------------------- +# Author: midoks +# --------------------------------------------------------------------------------- + import smtplib diff --git a/web/utils/enhanced_log_rotation.py b/web/utils/enhanced_log_rotation.py new file mode 100644 index 000000000..1dfa449c0 --- /dev/null +++ b/web/utils/enhanced_log_rotation.py @@ -0,0 +1,61 @@ +# coding:utf-8 + +# --------------------------------------------------------------------------------- +# MW-Linux面板 +# --------------------------------------------------------------------------------- +# copyright (c) 2018-∞(https://github.com/midoks/mdserver-web) All rights reserved. +# --------------------------------------------------------------------------------- +# Author: midoks +# --------------------------------------------------------------------------------- + + +import re +from logging import handlers + + +class EnhancedRotatingFileHandler(handlers.TimedRotatingFileHandler, + handlers.RotatingFileHandler): + """ + Handler for logging to a set of files, which switches from one file + to the next when the current file reaches a certain size, or at certain + timed intervals + @filename - log file name + @max_bytes - file size in bytes to rotate file + @interval - Duration to rotate file + @backup_count - Maximum number of files to retain + @encoding - file encoding + @when - 'when' events supported: + # S - Seconds + # M - Minutes + # H - Hours + # D - Days + # midnight - roll over at midnight + # W{0-6} - roll over on a certain day; 0 - Monday + Here we are defaulting rotation with minutes interval + """ + def __init__(self, filename, max_bytes=1, interval=60, backup_count=0, + encoding=None, when='M'): + max_bytes = max_bytes * 1024 * 1024 + handlers.TimedRotatingFileHandler.__init__(self, filename=filename, + when=when, + interval=interval, + backupCount=backup_count, + encoding=encoding) + + handlers.RotatingFileHandler.__init__(self, filename=filename, + mode='a', + maxBytes=max_bytes, + backupCount=backup_count, + encoding=encoding) + + # Time & Size combined rollover + def shouldRollover(self, record): + return handlers.TimedRotatingFileHandler.shouldRollover(self, record) \ + or handlers.RotatingFileHandler.shouldRollover(self, record) + + # Roll overs current file + def doRollover(self): + self.suffix = "%Y-%m-%d_%H-%M-%S" + self.extMatch = r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}(\.\w+)?$" + self.extMatch = re.compile(self.extMatch, re.ASCII) + handlers.TimedRotatingFileHandler.doRollover(self) \ No newline at end of file