From e997236159cb649244663518d3d3f6011389f4b1 Mon Sep 17 00:00:00 2001 From: Bai Date: Fri, 13 Sep 2024 17:47:57 +0800 Subject: [PATCH] perf: Modify gunicorn log file rotate yesterday dir --- apps/jumpserver/rewriting/logging.py | 20 ++++++++++++++++++++ apps/jumpserver/settings/logging.py | 21 +++++++++------------ 2 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 apps/jumpserver/rewriting/logging.py diff --git a/apps/jumpserver/rewriting/logging.py b/apps/jumpserver/rewriting/logging.py new file mode 100644 index 000000000..3a7d1c0ff --- /dev/null +++ b/apps/jumpserver/rewriting/logging.py @@ -0,0 +1,20 @@ +import os +from logging.handlers import TimedRotatingFileHandler +from datetime import datetime, timedelta + + +class DailyTimedRotatingFileHandler(TimedRotatingFileHandler): + def rotator(self, source, dest): + """ Override the original method to rotate the log file daily.""" + dest = self._get_rotate_dest_filename(source) + if os.path.exists(source) and not os.path.exists(dest): + # 存在多个服务进程时, 保证只有一个进程成功 rotate + os.rename(source, dest) + + @staticmethod + def _get_rotate_dest_filename(source): + date_yesterday = (datetime.now() - timedelta(days=1)).strftime('%Y-%m-%d') + path = [os.path.dirname(source), date_yesterday, os.path.basename(source)] + filename = os.path.join(*path) + os.makedirs(os.path.dirname(filename), exist_ok=True) + return filename diff --git a/apps/jumpserver/settings/logging.py b/apps/jumpserver/settings/logging.py index 4fad5f641..74585d168 100644 --- a/apps/jumpserver/settings/logging.py +++ b/apps/jumpserver/settings/logging.py @@ -50,37 +50,33 @@ LOGGING = { 'file': { 'encoding': 'utf8', 'level': 'DEBUG', - 'class': 'logging.handlers.RotatingFileHandler', - 'maxBytes': 1024 * 1024 * 100, - 'backupCount': 7, + 'class': 'jumpserver.rewriting.logging.DailyTimedRotatingFileHandler', + 'when': 'midnight', 'formatter': 'main', 'filename': JUMPSERVER_LOG_FILE, }, 'ansible_logs': { 'encoding': 'utf8', 'level': 'DEBUG', - 'class': 'logging.handlers.RotatingFileHandler', + 'class': 'jumpserver.rewriting.logging.DailyTimedRotatingFileHandler', + 'when': 'midnight', 'formatter': 'main', - 'maxBytes': 1024 * 1024 * 100, - 'backupCount': 7, 'filename': ANSIBLE_LOG_FILE, }, 'drf_exception': { 'encoding': 'utf8', 'level': 'DEBUG', - 'class': 'logging.handlers.RotatingFileHandler', + 'class': 'jumpserver.rewriting.logging.DailyTimedRotatingFileHandler', + 'when': 'midnight', 'formatter': 'exception', - 'maxBytes': 1024 * 1024 * 100, - 'backupCount': 7, 'filename': DRF_EXCEPTION_LOG_FILE, }, 'unexpected_exception': { 'encoding': 'utf8', 'level': 'DEBUG', - 'class': 'logging.handlers.RotatingFileHandler', + 'class': 'jumpserver.rewriting.logging.DailyTimedRotatingFileHandler', + 'when': 'midnight', 'formatter': 'exception', - 'maxBytes': 1024 * 1024 * 100, - 'backupCount': 7, 'filename': UNEXPECTED_EXCEPTION_LOG_FILE, }, 'syslog': { @@ -155,3 +151,4 @@ if CONFIG.SYSLOG_ADDR != '' and len(CONFIG.SYSLOG_ADDR.split(':')) == 2: if not os.path.isdir(LOG_DIR): os.makedirs(LOG_DIR, mode=0o755) +