feat:logging setup and cleanup

This commit is contained in:
guorong.zheng 2024-09-05 11:49:26 +08:00
parent 172dd181d6
commit 81ae8e8f88
2 changed files with 36 additions and 12 deletions

@ -5,6 +5,8 @@ from utils.channel import (
append_total_data,
process_sort_channel_list,
write_channel_to_file,
setup_logging,
cleanup_logging,
)
from utils.tools import (
update_file,
@ -25,9 +27,12 @@ from time import time
from flask import Flask, render_template_string
import sys
import shutil
import atexit
app = Flask(__name__)
atexit.register(cleanup_logging)
@app.route("/")
def show_index():
@ -194,6 +199,8 @@ class UpdateSource:
)
except asyncio.exceptions.CancelledError:
print("Update cancelled!")
finally:
cleanup_logging()
async def start(self, callback=None):
def default_callback(self, *args, **kwargs):
@ -202,6 +209,7 @@ class UpdateSource:
self.update_progress = callback or default_callback
self.run_ui = True if callback else False
if config.getboolean("Settings", "open_update"):
setup_logging()
await self.main()
if self.run_ui and config.getboolean("Settings", "open_update") == False:
self.update_progress(

@ -15,16 +15,35 @@ from rapidfuzz import process
log_dir = "output"
log_file = "result_new.log"
log_path = os.path.join(log_dir, log_file)
handler = None
if not os.path.exists(log_dir):
os.makedirs(log_dir)
handler = RotatingFileHandler(log_path, encoding="utf-8")
logging.basicConfig(
handlers=[handler],
format="%(message)s",
level=logging.INFO,
)
def setup_logging():
"""
Setup logging
"""
global handler
if not os.path.exists(log_dir):
os.makedirs(log_dir)
handler = RotatingFileHandler(log_path, encoding="utf-8")
logging.basicConfig(
handlers=[handler],
format="%(message)s",
level=logging.INFO,
)
def cleanup_logging():
"""
Cleanup logging
"""
global handler
if handler:
for handler in logging.root.handlers[:]:
handler.close()
logging.root.removeHandler(handler)
if os.path.exists(log_path):
os.remove(log_path)
def get_channel_data_from_file(channels=None, file=None, from_result=False):
@ -632,9 +651,6 @@ def write_channel_to_file(items, data, callback=None):
channel_urls = get_total_urls_from_info_list(info_list)
print("write:", cate, name, "num:", len(channel_urls))
update_channel_urls_txt(cate, name, channel_urls, callback=callback)
for handler in logging.root.handlers[:]:
handler.close()
logging.root.removeHandler(handler)
def get_multicast_fofa_search_org(region, type):
@ -659,7 +675,7 @@ def get_multicast_fofa_search_urls():
"""
config_region_list = config.get("Settings", "multicast_region_list").split(",")
rtp_file_names = []
for filename in os.listdir(resource_path("updates/multicast/rtp")):
for filename in os.listdir(resource_path("config/rtp")):
if filename.endswith(".txt") and "_" in filename:
filename = filename.replace(".txt", "")
rtp_file_names.append(filename)