From ba2323d4a2f8c602556f5c60d00ce8a15bfb58f3 Mon Sep 17 00:00:00 2001 From: yshtcn Date: Wed, 6 Sep 2023 11:37:34 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BD=BF=E6=97=A5=E5=BF=97=E5=92=8C?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E4=BF=A1=E6=81=AF=E4=BD=BF=E7=94=A8=E5=BD=93?= =?UTF-8?q?=E5=89=8D=E6=97=A5=E6=9C=9F=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ServerChanPush2TelegramBot.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/ServerChanPush2TelegramBot.py b/ServerChanPush2TelegramBot.py index 1f90943..879c0f0 100644 --- a/ServerChanPush2TelegramBot.py +++ b/ServerChanPush2TelegramBot.py @@ -4,9 +4,23 @@ import logging import json import re from urllib.parse import unquote +from datetime import datetime +from logging.handlers import TimedRotatingFileHandler -# 设置日志级别和日志文件 -logging.basicConfig(level=logging.INFO,filename="received_requests.log", filemode="a", encoding='utf-8') +# 获取当前日期 +current_date = datetime.now().strftime("%Y-%m-%d") + +# 创建一个处理器,该处理器每天午夜都会创建一个新的日志文件 +handler = TimedRotatingFileHandler(f"received_requests_{current_date}.log", when="midnight", interval=1, backupCount=10) +handler.suffix = "%Y-%m-%d" + +# 配置日志 +logging.basicConfig( + level=logging.INFO, + handlers=[handler], + format="%(asctime)s - %(levelname)s - %(message)s", + datefmt="%Y-%m-%d %H:%M:%S" +) # 初始化 Flask 应用 app = Flask(__name__) @@ -21,7 +35,8 @@ def load_config(): # 保存接收到的请求数据 def save_received_data(received_url, received_data): try: - with open("received_data.json", "a", encoding='utf-8') as f: + current_date = datetime.now().strftime("%Y-%m-%d") + with open(f"received_data_{current_date}.json", "a", encoding='utf-8') as f: json.dump({"received_url": received_url, "received_data": received_data}, f, ensure_ascii=False) f.write("\n") except Exception as e: @@ -30,7 +45,8 @@ def save_received_data(received_url, received_data): # 保存发送的请求数据 def save_sent_data(api_url, payload): try: - with open("sent_data.json", "a", encoding='utf-8') as f: + current_date = datetime.now().strftime("%Y-%m-%d") + with open(f"sent_data_{current_date}.json", "a", encoding='utf-8') as f: json.dump({"sent_url": api_url, "sent_data": payload}, f, ensure_ascii=False) f.write("\n") except Exception as e: