Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
midoks committed Nov 13, 2024
1 parent f685054 commit ee64e55
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 3 deletions.
18 changes: 18 additions & 0 deletions web/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions web/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@
# 日志文件名。这将进入数据目录,服务器模式下的非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数据库的默认路径。
# 此默认设置将文件放置在与此相同的目录中 配置文件,但会生成一个在整个应用程序中使用的绝对路径。
Expand Down
11 changes: 10 additions & 1 deletion web/utils/email.py
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
# ---------------------------------------------------------------------------------


import smtplib

Expand Down
61 changes: 61 additions & 0 deletions web/utils/enhanced_log_rotation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# coding:utf-8

# ---------------------------------------------------------------------------------
# MW-Linux面板
# ---------------------------------------------------------------------------------
# copyright (c) 2018-∞(https://github.com/midoks/mdserver-web) All rights reserved.
# ---------------------------------------------------------------------------------
# Author: midoks <[email protected]>
# ---------------------------------------------------------------------------------


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)

0 comments on commit ee64e55

Please sign in to comment.