- Removed trailing whitespace and ensured files end with a single newline in cleanup_unused_files.py. - Added fix_trailing_whitespace.py to batch remove trailing whitespace from Python files. - Updated install_dependencies.py to improve dependency installation prompts. - Cleaned up pick_coords.py by removing unnecessary whitespace and improving code readability. - Enhanced region_selector.py by disabling specific pylint checks for better compatibility with third-party libraries. - Improved WPS流程图生成工具.py by cleaning up whitespace and enhancing readability. - Updated __init__.py in typings to remove unnecessary lines. - Modified app.js to adjust default configuration values and clean up unused code.
252 lines
8.1 KiB
Python
252 lines
8.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
日志管理工具
|
|
============
|
|
|
|
功能:
|
|
1. 查看日志统计信息
|
|
2. 手动清理旧日志
|
|
3. 手动压缩日志文件
|
|
4. 显示日志轮转状态
|
|
|
|
使用方法:
|
|
python manage_logs.py [command] [options]
|
|
|
|
命令:
|
|
stats - 显示日志统计信息
|
|
cleanup - 清理旧日志
|
|
compress - 压缩旧日志
|
|
monitor - 显示监控状态
|
|
help - 显示帮助信息
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import glob
|
|
import argparse
|
|
from datetime import datetime, timedelta
|
|
from typing import Dict, Any
|
|
|
|
# 添加项目路径
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
from douyin.logging.douyin_logging import (
|
|
cleanup_old_logs,
|
|
compress_log_file,
|
|
get_log_stats,
|
|
path_config
|
|
)
|
|
|
|
|
|
def get_log_base_names(logs_dir: str) -> list[str]:
|
|
"""获取日志基础名称列表"""
|
|
if not os.path.exists(logs_dir):
|
|
return []
|
|
|
|
# 查找所有日志文件
|
|
pattern = os.path.join(logs_dir, "*.log*")
|
|
log_files = glob.glob(pattern)
|
|
|
|
base_names = set()
|
|
for log_file in log_files:
|
|
filename = os.path.basename(log_file)
|
|
# 处理压缩文件
|
|
if filename.endswith('.gz'):
|
|
filename = filename[:-3]
|
|
|
|
# 提取基础名称(去掉日期和序号)
|
|
# 例如: unified_2024-01-15.log -> unified
|
|
# 例如: unified_2024-01-15.1.log -> unified
|
|
parts = filename.split('_')
|
|
if len(parts) >= 2:
|
|
base_name = parts[0]
|
|
base_names.add(base_name)
|
|
|
|
return sorted(list(base_names))
|
|
|
|
|
|
def format_size(size_mb: float) -> str:
|
|
"""格式化文件大小"""
|
|
if size_mb < 1024:
|
|
return f"{size_mb:.1f}MB"
|
|
else:
|
|
return f"{size_mb/1024:.1f}GB"
|
|
|
|
|
|
def show_stats(logs_dir: str, base_name: str) -> None:
|
|
"""显示日志统计信息"""
|
|
print(f"\n=== 日志统计信息 ({base_name}) ===")
|
|
|
|
stats = get_log_stats(logs_dir, base_name)
|
|
|
|
if stats['total_files'] == 0:
|
|
print("未找到日志文件")
|
|
return
|
|
|
|
print(f"总文件数: {stats['total_files']}")
|
|
print(f"总大小: {format_size(stats['total_size_mb'])}")
|
|
print(f"压缩文件数: {stats['compressed_files']}")
|
|
if stats['compressed_files'] > 0:
|
|
print(f"压缩率: {stats['compression_ratio']*100:.1f}%")
|
|
print(f"最旧文件: {stats['oldest_file'] or 'N/A'}")
|
|
print(f"最新文件: {stats['newest_file'] or 'N/A'}")
|
|
|
|
# 显示最近的文件
|
|
pattern = os.path.join(logs_dir, f"{base_name}_*.log*")
|
|
log_files = glob.glob(pattern)
|
|
if log_files:
|
|
# 按修改时间排序
|
|
log_files.sort(key=lambda x: os.path.getmtime(x), reverse=True)
|
|
print(f"\n最近的文件 (前5个):")
|
|
for i, log_file in enumerate(log_files[:5]):
|
|
mtime = os.path.getmtime(log_file)
|
|
size = os.path.getsize(log_file) / 1024 / 1024
|
|
date_str = datetime.fromtimestamp(mtime).strftime("%Y-%m-%d %H:%M:%S")
|
|
compressed = log_file.endswith('.gz')
|
|
print(f" {i+1}. {os.path.basename(log_file)} - {format_size(size)} - {date_str} {'[压缩]' if compressed else ''}")
|
|
|
|
|
|
def manual_cleanup(logs_dir: str, base_name: str, keep_days: int = 30, dry_run: bool = True) -> None:
|
|
"""手动清理旧日志"""
|
|
print(f"\n=== 手动清理日志 ({base_name}) ===")
|
|
print(f"保留天数: {keep_days}")
|
|
print(f"预览模式: {'是' if dry_run else '否'}")
|
|
|
|
stats = cleanup_old_logs(
|
|
logs_dir=logs_dir,
|
|
base_name=base_name,
|
|
keep_days=keep_days,
|
|
compress=False, # 清理时不压缩
|
|
)
|
|
|
|
if dry_run:
|
|
print(f"\n预览结果:")
|
|
print(f"将删除文件数: {stats['deleted']}")
|
|
print(f"将释放空间: {format_size(stats['freed_space_mb'])}")
|
|
print(f"当前总大小: {format_size(stats['total_size_mb'])}")
|
|
|
|
|
|
def manual_compress(logs_dir: str, base_name: str, older_than_days: int = 7, dry_run: bool = True) -> None:
|
|
"""手动压缩旧日志"""
|
|
print(f"\n=== 手动压缩日志 ({base_name}) ===")
|
|
print(f"压缩天数阈值: {older_than_days}")
|
|
print(f"预览模式: {'是' if dry_run else '否'}")
|
|
|
|
cutoff_date = datetime.now() - timedelta(days=older_than_days)
|
|
|
|
# 查找需要压缩的文件
|
|
pattern = os.path.join(logs_dir, f"{base_name}_*.log")
|
|
log_files = glob.glob(pattern)
|
|
|
|
compress_candidates = []
|
|
for log_file in log_files:
|
|
try:
|
|
filename = os.path.basename(log_file)
|
|
date_part = filename.replace(f"{base_name}_", "").split('.')[0]
|
|
file_date = datetime.strptime(date_part, "%Y-%m-%d")
|
|
|
|
if file_date < cutoff_date:
|
|
compress_candidates.append(log_file)
|
|
except Exception:
|
|
continue
|
|
|
|
print(f"\n找到 {len(compress_candidates)} 个待压缩文件")
|
|
|
|
total_original_size = 0
|
|
compressed_count = 0
|
|
|
|
for log_file in compress_candidates:
|
|
size_mb = os.path.getsize(log_file) / 1024 / 1024
|
|
total_original_size += size_mb
|
|
|
|
if dry_run:
|
|
print(f"将压缩: {os.path.basename(log_file)} ({format_size(size_mb)})")
|
|
else:
|
|
if compress_log_file(log_file):
|
|
compressed_count += 1
|
|
|
|
if dry_run:
|
|
print(f"\n预览结果:")
|
|
print(f"将压缩文件数: {len(compress_candidates)}")
|
|
print(f"预计节省空间: {format_size(total_original_size * 0.7)} (估算压缩率70%)")
|
|
else:
|
|
print(f"\n执行结果:")
|
|
print(f"成功压缩文件数: {compressed_count}")
|
|
|
|
|
|
def show_monitor_status() -> None:
|
|
"""显示监控器状态"""
|
|
print("\n=== 日志监控状态 ===")
|
|
|
|
from douyin.logging.douyin_logging import _log_monitor
|
|
|
|
if _log_monitor and _log_monitor._running:
|
|
print("状态: 运行中")
|
|
print(f"监控目录: {_log_monitor.logs_dir}")
|
|
print(f"基础名称: {_log_monitor.base_name}")
|
|
print(f"警告阈值: {format_size(_log_monitor.warning_size_mb)}")
|
|
print(f"严重阈值: {format_size(_log_monitor.critical_size_mb)}")
|
|
print(f"检查间隔: {_log_monitor.check_interval}秒")
|
|
else:
|
|
print("状态: 未运行")
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="日志管理工具")
|
|
parser.add_argument("command", choices=["stats", "cleanup", "compress", "monitor", "help"],
|
|
help="执行的命令")
|
|
parser.add_argument("--logs-dir", help="日志目录,默认自动检测")
|
|
parser.add_argument("--base-name", help="日志基础名称,默认显示所有")
|
|
parser.add_argument("--keep-days", type=int, default=30, help="保留天数,默认30")
|
|
parser.add_argument("--older-than", type=int, default=7, help="压缩天数阈值,默认7")
|
|
parser.add_argument("--dry-run", action="store_true", help="预览模式,不执行实际操作")
|
|
|
|
args = parser.parse_args()
|
|
|
|
if args.command == "help":
|
|
parser.print_help()
|
|
return
|
|
|
|
# 获取日志目录
|
|
logs_dir = args.logs_dir or path_config.get_logs_dir()
|
|
|
|
if not os.path.exists(logs_dir):
|
|
print(f"日志目录不存在: {logs_dir}")
|
|
return
|
|
|
|
print(f"日志目录: {logs_dir}")
|
|
|
|
# 获取基础名称
|
|
base_names = get_log_base_names(logs_dir)
|
|
|
|
if not base_names:
|
|
print("未找到日志文件")
|
|
return
|
|
|
|
# 如果未指定base_name,显示所有或让用户选择
|
|
if not args.base_name:
|
|
if len(base_names) == 1:
|
|
args.base_name = base_names[0]
|
|
else:
|
|
print(f"找到多个日志类型: {', '.join(base_names)}")
|
|
print("请使用 --base-name 指定日志类型")
|
|
return
|
|
|
|
if args.base_name not in base_names:
|
|
print(f"日志类型 '{args.base_name}' 不存在,可用类型: {', '.join(base_names)}")
|
|
return
|
|
|
|
# 执行命令
|
|
if args.command == "stats":
|
|
show_stats(logs_dir, args.base_name)
|
|
elif args.command == "cleanup":
|
|
manual_cleanup(logs_dir, args.base_name, args.keep_days, args.dry_run)
|
|
elif args.command == "compress":
|
|
manual_compress(logs_dir, args.base_name, args.older_than, args.dry_run)
|
|
elif args.command == "monitor":
|
|
show_monitor_status()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|