Compare commits

...
11 Commits
Author SHA1 Message Date
teddy 6acdaea9f8 feat(sendmsg, README): 添加微信状态检查和发送结果验证功能
- 在sendmsg.py中实现微信状态检查,确保客户端就绪、已登录及接收者有效性。
- 增强消息和文件发送逻辑,添加发送结果验证,记录详细的发送状态信息。
- 更新README.md,详细描述新增功能和使用方法,包括微信状态检查和多格式图片支持。
2025-08-22 18:02:11 +08:00
teddy 08d302b22c fix(sendmsg): 修复文件路径检查逻辑
- 合并文件路径和文件存在性检查为单一条件,确保在文件路径有效时才进行后续操作。
2025-08-21 15:28:01 +08:00
teddy 462fed6336 Merge branch 'master' of https://git.internal.intelligrow.cn/intelligrow/Send_picture_automatically 2025-08-21 15:24:39 +08:00
teddy f96d1eb08d feat: 更新配置和增强日志记录功能
- 修改config.py中的文件路径和定时发送时间
- 在各个脚本中添加日志记录功能,提升错误追踪和调试能力
- 更新README.md,详细说明程序功能和使用方法
- 重构scheduler.py、sendmsg.py、send_openmsg.py和send_filemsg.py,增强代码可读性和可维护性
2025-08-21 15:22:55 +08:00
teddy a919965831 fix(config): 更新接收者和发送时间配置
将消息接收者从"2期"改为"1期",并调整检查时间和发送时间为早上时段
2025-08-18 17:05:23 +08:00
teddy 33aca60f56 feat: 更新配置并优化消息发送逻辑
- 修改配置中的接收者名称和发送时间
- 在scheduler.py中添加周末不执行任务的逻辑
- 在sendmsg.py中支持多种图片格式并延长重试间隔
2025-08-18 16:58:14 +08:00
teddy aaf5f59b59 feat: 改进定时任务管理并更新配置
- 在service_runner.py中添加进程ID记录功能
- 增强stop_scheduler.py以同时处理scheduler.py和sendmsg.py进程
- 修改sendmsg.py增加重试机制
- 更新配置文件路径和执行时间
2025-08-15 19:45:46 +08:00
teddy 7233700539 feat: 重构定时任务系统并增强配置管理
- 将硬编码的时间配置改为从CONFIG读取
- 支持多接收人配置,修改open_id为数组类型
- 增加发送时间等待功能,确保消息在指定时间发送
- 改进停止定时任务的进程查找逻辑
- 更新配置项,增加checking_time、sending_time和message字段
2025-06-26 17:24:50 +08:00
teddy 546ca9dfe2 feat: 添加定时任务后台运行和停止功能
新增service_runner.py用于后台运行定时任务,使用pythonw.exe实现无窗口运行
新增stop_scheduler.py用于停止后台定时任务进程
调整scheduler.py中的任务执行时间
2025-06-24 17:49:10 +08:00
teddy 84288fc66b refactor: 重构定时任务和消息发送逻辑
- 删除start_scheduler.bat脚本,改为直接运行scheduler.py
- 重构scheduler.py,添加日志记录和主函数封装
- 重构sendmsg.py,将逻辑封装到函数中并添加返回值
- 调整定时任务执行时间为每天17:30
2025-06-24 17:31:49 +08:00
teddy 699bac8008 feat: 更新定时任务时间并添加批量处理脚本
- 将定时任务时间从12:00改为14:39
- 新增start_scheduler.bat脚本用于管理进程
- 在config.py中添加messages_reciever配置项
- 优化sendmsg.py代码结构,使用配置项代替硬编码
2025-06-24 14:50:22 +08:00
11 changed files with 1307 additions and 91 deletions
+1
View File
@@ -0,0 +1 @@
2025-08-14 10:00:11.042 sessionbox.py[249] _click -> Can not move cursor. ListItemControl's BoundingRectangle is (0,0,0,0)[0x0]. SearchProperties: {ControlType: ListItemControl}
+163
View File
@@ -0,0 +1,163 @@
# WXMSG - 微信消息自动发送程序
## 功能概述
这是一个自动发送每日AI新闻图片和消息到微信的程序,具有完整的日志记录、错误处理和微信状态检查功能。
## 主要功能
- **定时发送**: 每天在指定时间自动发送图片和消息
- **容错机制**: 微信失败时自动发送飞书提醒
- **详细日志**: 完整的操作日志记录,便于问题排查
- **进程管理**: 支持后台运行和进程控制
- **微信状态检查**: 自动检查微信客户端状态和接收者有效性
- **智能重试**: 支持多种图片格式,自动重试机制
## 新增功能
### 微信状态检查
程序现在会自动检查:
- 微信客户端是否就绪
- 微信是否已登录
- 接收者是否存在于联系人列表
- 当前会话数量和联系人数量
- 微信窗口状态
### 发送结果验证
使用wxauto的WxResponse对象验证:
- 文件发送是否成功
- 消息发送是否成功
- 详细的发送状态信息
- 部分失败时的智能处理
### 多格式图片支持
支持多种图片格式:
- .jpg
- .jpeg
- .png
## 日志功能
### 日志文件位置
所有日志文件保存在 `wxauto_logs/` 目录下,按日期和模块分类:
- `sendmsg_YYYYMMDD.log` - 主发送模块日志
- `scheduler_YYYYMMDD.log` - 定时任务调度器日志
- `send_openmsg_YYYYMMDD.log` - 飞书提醒消息日志
- `send_filemsg_YYYYMMDD.log` - 文件未找到提醒日志
- `service_runner_YYYYMMDD.log` - 后台服务启动日志
- `stop_scheduler_YYYYMMDD.log` - 进程停止日志
### 日志级别
- **INFO**: 正常操作信息
- **WARNING**: 警告信息
- **ERROR**: 错误信息
- **DEBUG**: 调试信息
### 日志内容
每个日志条目包含:
- 时间戳
- 模块名称
- 日志级别
- 函数名和行号
- 详细消息
## 使用方法
### 1. 直接运行
```bash
python sendmsg.py
```
### 2. 启动定时任务
```bash
python scheduler.py
```
### 3. 后台运行
```bash
python service_runner.py
```
### 4. 停止服务
```bash
python stop_scheduler.py
```
### 5. 测试微信状态
```bash
python test_wechat_status.py
```
## 配置说明
`config.py` 中配置:
- 发送时间
- 文件路径
- 消息内容
- 飞书API配置
## 故障排查
### 查看日志
1. 检查 `wxauto_logs/` 目录下的最新日志文件
2. 根据时间戳找到相关操作的日志
3. 查看ERROR级别的日志了解错误详情
### 常见问题
1. **微信客户端问题**: 查看 `sendmsg_*.log` 中的微信初始化日志
2. **文件未找到**: 检查文件路径配置和文件是否存在
3. **飞书API问题**: 查看 `send_openmsg_*.log``send_filemsg_*.log`
4. **进程管理问题**: 查看 `service_runner_*.log``stop_scheduler_*.log`
### 微信状态检查
运行测试脚本检查微信状态:
```bash
python test_wechat_status.py
```
这将显示:
- 微信客户端状态
- 登录状态
- 接收者有效性
- 可用方法列表
### 日志示例
```
2024-01-15 11:40:00 - sendmsg - INFO - check_wechat_status:45 - ✅ 微信已登录,当前会话数量: 15
2024-01-15 11:40:00 - sendmsg - INFO - check_wechat_status:52 - ✅ 接收者 '文件传输助手' 存在于联系人列表中
2024-01-15 11:40:00 - sendmsg - INFO - send_daily_message:120 - 🎉 所有内容发送完成!
2024-01-15 11:40:00 - sendmsg - INFO - send_daily_message:125 - 发送结果总结:
2024-01-15 11:40:00 - sendmsg - INFO - send_daily_message:126 - 📎 文件发送: ✅ 成功
2024-01-15 11:40:00 - sendmsg - INFO - send_daily_message:127 - 💬 消息发送: ✅ 成功
```
## 依赖要求
- Python 3.13+
- wxauto
- lark-oapi
- schedule
- psutil (可选,用于进程管理)
## 注意事项
1. 确保微信客户端已登录
2. 检查文件路径配置是否正确
3. 验证飞书API配置是否有效
4. 定期检查日志文件大小,避免占用过多磁盘空间
5. 运行前建议先执行 `test_wechat_status.py` 检查微信状态
## 更新日志
### v0.2.0
- 添加微信状态检查功能
- 使用WxResponse验证发送结果
- 支持多种图片格式
- 改进错误处理和日志记录
- 添加微信状态测试脚本
### v0.1.0
- 基础消息发送功能
- 定时任务调度
- 飞书提醒机制
- 基础日志记录
+8 -4
View File
@@ -1,6 +1,10 @@
CONFIG = { CONFIG = {
"app_id": "cli_a8de57f7e95f100c", "app_id": "cli_a8d64a7be63a500e",
"app_secret": "jm2nkFnnSNVS3n076VOPfh8k7q0hEKVp", "app_secret": "mcK8aTiq0CLtkzGs2aTZpcnom5J4o6yB",
"open_id": "ou_bd33a5f75dd2ccb005a21e4dd01f930c", "open_id": ["ou_c6466a45623096cf7a34d94fe30c6c73", "ou_3b94d0caf83dbced8b0e26af4852a281"],
"file_path": "C:\\Users\\22251\\OneDrive\\桌面\\picture\\" "file_path": "F:\\",
"messages_reciever": "宝库学堂:AI全能增长官特训营 1期",
"checking_time": "07:55",
"sending_time": "10:00",
"message": "新的一天,从华智长盈每日AI新闻开始!让我们一起看看今天AI圈有啥新鲜事!"
} }
+50
View File
@@ -0,0 +1,50 @@
import logging
import os
from datetime import datetime
from pathlib import Path
def setup_logger(name, log_file=None, level=logging.INFO):
"""设置日志记录器"""
# 创建logs目录
logs_dir = Path("wxauto_logs")
logs_dir.mkdir(exist_ok=True)
# 如果没有指定日志文件,使用默认命名
if log_file is None:
timestamp = datetime.now().strftime("%Y%m%d")
log_file = logs_dir / f"{name}_{timestamp}.log"
# 创建logger
logger = logging.getLogger(name)
logger.setLevel(level)
# 避免重复添加handler
if logger.handlers:
return logger
# 创建文件处理器
file_handler = logging.FileHandler(log_file, encoding='utf-8')
file_handler.setLevel(level)
# 创建控制台处理器
console_handler = logging.StreamHandler()
console_handler.setLevel(level)
# 创建格式器
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(funcName)s:%(lineno)d - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)
# 添加处理器到logger
logger.addHandler(file_handler)
logger.addHandler(console_handler)
return logger
def get_logger(name):
"""获取已配置的logger"""
return logging.getLogger(name)
+94 -6
View File
@@ -3,17 +3,105 @@ import time
import subprocess import subprocess
import sys import sys
import os import os
from datetime import datetime
import calendar
from config import CONFIG
from logger_config import setup_logger
# 设置日志记录器
logger = setup_logger('scheduler')
def run_sendmsg(): def run_sendmsg():
"""执行sendmsg.py脚本,周末不执行"""
# 获取当前日期是星期几(0是星期一,6是星期日)
current_weekday = datetime.now().weekday()
# 如果是周末(周六或周日),不执行任务
if current_weekday >= 5: # 5是周六,6是周日
print(f"[{datetime.now()}] 今天是周末,不执行任务")
return
script_path = os.path.join(os.path.dirname(__file__), 'sendmsg.py') script_path = os.path.join(os.path.dirname(__file__), 'sendmsg.py')
subprocess.run([sys.executable, script_path]) logger.info("=" * 50)
logger.info("开始执行每日消息发送任务...")
logger.info(f"脚本路径: {script_path}")
logger.info(f"当前工作目录: {os.getcwd()}")
# 设置每天12点运行 try:
schedule.every().day.at("12:00").do(run_sendmsg) logger.info("正在启动sendmsg.py子进程...")
result = subprocess.run([sys.executable, script_path],
capture_output=True, text=True, cwd=os.path.dirname(__file__))
print("定时任务已启动,将在每天12:00运行sendmsg.py") logger.info(f"子进程执行完成,返回码: {result.returncode}")
# 保持程序运行 if result.returncode == 0:
logger.info("任务执行成功")
if result.stdout:
logger.info(f"标准输出: {result.stdout}")
else:
logger.error("任务执行失败")
if result.stderr:
logger.error(f"错误输出: {result.stderr}")
if result.stdout:
logger.info(f"标准输出: {result.stdout}")
except Exception as e:
logger.error(f"执行出错: {str(e)}")
logger.error(f"错误类型: {type(e).__name__}")
import traceback
logger.error(f"异常堆栈: {traceback.format_exc()}")
logger.info("=" * 50)
def main():
"""主函数 - 设置定时任务"""
logger.info("=" * 50)
logger.info("定时任务调度器启动")
logger.info("=" * 50)
# 显示配置信息
logger.info(f"检查时间: {CONFIG['checking_time']}")
logger.info(f"发送时间: {CONFIG['sending_time']}")
logger.info(f"文件路径: {CONFIG['file_path']}")
logger.info(f"消息接收者: {CONFIG['messages_reciever']}")
logger.info(f"飞书接收者数量: {len(CONFIG['open_id'])}")
# 每天在配置的时间执行任务
schedule.every().day.at(CONFIG['checking_time']).do(run_sendmsg)
logger.info(f"定时任务已设置,将在每天 {CONFIG['checking_time']} 执行sendmsg.py")
logger.info("按Ctrl+C停止程序")
# 显示下次执行时间
next_run = schedule.next_run()
if next_run:
logger.info(f"下次执行时间: {next_run.strftime('%Y-%m-%d %H:%M:%S')}")
try:
while True: while True:
schedule.run_pending() schedule.run_pending()
time.sleep(1) time.sleep(60) # 每分钟检查一次
# 每分钟显示一次状态信息
current_time = datetime.now()
logger.debug(f"当前时间: {current_time.strftime('%Y-%m-%d %H:%M:%S')}, 等待下次执行...")
except KeyboardInterrupt:
logger.info(f"\n收到中断信号,正在停止定时任务...")
logger.info(f"定时任务已停止")
except Exception as e:
logger.error(f"定时任务运行过程中出现异常: {str(e)}")
logger.error(f"异常类型: {type(e).__name__}")
import traceback
logger.error(f"异常堆栈: {traceback.format_exc()}")
raise
if __name__ == "__main__":
try:
main()
except Exception as e:
logger.error(f"程序启动失败: {str(e)}")
logger.error(f"异常类型: {type(e).__name__}")
import traceback
logger.error(f"异常堆栈: {traceback.format_exc()}")
sys.exit(1)
+65 -8
View File
@@ -1,37 +1,94 @@
import json import json
import lark_oapi as lark import lark_oapi as lark
from lark_oapi.api.im.v1 import * from lark_oapi.api.im.v1 import *
from config import CONFIG from config import CONFIG
from logger_config import setup_logger
# 设置日志记录器
logger = setup_logger('send_filemsg')
def main(): def main():
logger.info("=" * 50)
logger.info("开始执行文件未找到的飞书提醒消息发送")
logger.info("=" * 50)
Local_openid = CONFIG['open_id']
logger.info(f"目标接收者数量: {len(Local_openid)}")
logger.info(f"接收者open_id列表: {Local_openid}")
# 创建client
logger.info("正在创建飞书客户端...")
try:
client = lark.Client.builder() \ client = lark.Client.builder() \
.app_id(CONFIG['app_id']) \ .app_id(CONFIG['app_id']) \
.app_secret(CONFIG['app_secret']) \ .app_secret(CONFIG['app_secret']) \
.log_level(lark.LogLevel.DEBUG) \ .log_level(lark.LogLevel.DEBUG) \
.build() .build()
logger.info("飞书客户端创建成功")
except Exception as e:
logger.error(f"飞书客户端创建失败: {str(e)}")
logger.error(f"错误类型: {type(e).__name__}")
return
success_count = 0
fail_count = 0
for i, local_openid in enumerate(Local_openid):
logger.info(f"正在处理第 {i+1}/{len(Local_openid)} 个接收者: {local_openid}")
try:
# 构造请求对象 # 构造请求对象
request: CreateMessageRequest = CreateMessageRequest.builder() \ request: CreateMessageRequest = CreateMessageRequest.builder() \
.receive_id_type("open_id") \ .receive_id_type("open_id") \
.request_body(CreateMessageRequestBody.builder() .request_body(CreateMessageRequestBody.builder()
.receive_id(CONFIG['open_id']) .receive_id(local_openid)
.msg_type("text") .msg_type("text")
.content("{\"text\":\"未找到指定图片\"}") .content("{\"text\":\"未找到指定图片,请手动发送\"}")
.build()) \ .build()) \
.build() .build()
logger.info("请求对象构造成功")
logger.info(f"请求内容: 未找到指定图片,请手动发送")
# 发起请求 # 发起请求
logger.info("正在发送飞书消息...")
response: CreateMessageResponse = client.im.v1.message.create(request) response: CreateMessageResponse = client.im.v1.message.create(request)
# 处理失败返回 # 处理失败返回
if not response.success(): if not response.success():
lark.logger.error( logger.error(f"消息发送失败 - 接收者: {local_openid}")
f"client.im.v1.message.create failed, code: {response.code}, msg: {response.msg}, log_id: {response.get_log_id()}, resp: \n{json.dumps(json.loads(response.raw.content), indent=4, ensure_ascii=False)}") logger.error(f"错误代码: {response.code}")
return logger.error(f"错误信息: {response.msg}")
logger.error(f"日志ID: {response.get_log_id()}")
logger.error(f"响应内容: {json.dumps(json.loads(response.raw.content), indent=4, ensure_ascii=False)}")
fail_count += 1
else:
logger.info(f"消息发送成功 - 接收者: {local_openid}")
logger.info(f"响应数据: {lark.JSON.marshal(response.data, indent=4)}")
success_count += 1
# 处理业务结果 except Exception as e:
lark.logger.info(lark.JSON.marshal(response.data, indent=4)) logger.error(f"处理接收者 {local_openid} 时出现异常: {str(e)}")
logger.error(f"异常类型: {type(e).__name__}")
import traceback
logger.error(f"异常堆栈: {traceback.format_exc()}")
fail_count += 1
# 总结报告
logger.info("=" * 50)
logger.info("文件未找到的飞书提醒消息发送完成")
logger.info(f"成功发送: {success_count}")
logger.info(f"发送失败: {fail_count}")
logger.info(f"总计处理: {len(Local_openid)} 个接收者")
logger.info("=" * 50)
if __name__ == "__main__": if __name__ == "__main__":
try:
main() main()
logger.info("程序执行完成")
except Exception as e:
logger.error(f"程序执行过程中出现未捕获的异常: {str(e)}")
logger.error(f"异常类型: {type(e).__name__}")
import traceback
logger.error(f"异常堆栈: {traceback.format_exc()}")
exit(1)
+63 -7
View File
@@ -1,38 +1,94 @@
import json import json
import lark_oapi as lark import lark_oapi as lark
from lark_oapi.api.im.v1 import * from lark_oapi.api.im.v1 import *
from config import CONFIG from config import CONFIG
from logger_config import setup_logger
# 设置日志记录器
logger = setup_logger('send_openmsg')
def main(): def main():
logger.info("=" * 50)
logger.info("开始执行飞书提醒消息发送")
logger.info("=" * 50)
Local_openid = CONFIG['open_id']
logger.info(f"目标接收者数量: {len(Local_openid)}")
logger.info(f"接收者open_id列表: {Local_openid}")
# 创建client # 创建client
logger.info("正在创建飞书客户端...")
try:
client = lark.Client.builder() \ client = lark.Client.builder() \
.app_id(CONFIG['app_id']) \ .app_id(CONFIG['app_id']) \
.app_secret(CONFIG['app_secret']) \ .app_secret(CONFIG['app_secret']) \
.log_level(lark.LogLevel.DEBUG) \ .log_level(lark.LogLevel.DEBUG) \
.build() .build()
logger.info("飞书客户端创建成功")
except Exception as e:
logger.error(f"飞书客户端创建失败: {str(e)}")
logger.error(f"错误类型: {type(e).__name__}")
return
# 构造请求对象 # 构造请求对象
success_count = 0
fail_count = 0
for i, local_openid in enumerate(Local_openid):
logger.info(f"正在处理第 {i+1}/{len(Local_openid)} 个接收者: {local_openid}")
try:
request: CreateMessageRequest = CreateMessageRequest.builder() \ request: CreateMessageRequest = CreateMessageRequest.builder() \
.receive_id_type("open_id") \ .receive_id_type("open_id") \
.request_body(CreateMessageRequestBody.builder() .request_body(CreateMessageRequestBody.builder()
.receive_id(CONFIG['open_id']) .receive_id(local_openid)
.msg_type("text") .msg_type("text")
.content("{\"text\":\"请打开微信或者登录微信\"}") .content("{\"text\":\"请打开微信或者登录微信\"}")
.build()) \ .build()) \
.build() .build()
logger.info("请求对象构造成功")
logger.info(f"请求内容: 请打开微信或者登录微信")
# 发起请求 # 发起请求
logger.info("正在发送飞书消息...")
response: CreateMessageResponse = client.im.v1.message.create(request) response: CreateMessageResponse = client.im.v1.message.create(request)
# 处理失败返回 # 处理失败返回
if not response.success(): if not response.success():
lark.logger.error( logger.error(f"消息发送失败 - 接收者: {local_openid}")
f"client.im.v1.message.create failed, code: {response.code}, msg: {response.msg}, log_id: {response.get_log_id()}, resp: \n{json.dumps(json.loads(response.raw.content), indent=4, ensure_ascii=False)}") logger.error(f"错误代码: {response.code}")
return logger.error(f"错误信息: {response.msg}")
logger.error(f"日志ID: {response.get_log_id()}")
logger.error(f"响应内容: {json.dumps(json.loads(response.raw.content), indent=4, ensure_ascii=False)}")
fail_count += 1
else:
logger.info(f"消息发送成功 - 接收者: {local_openid}")
logger.info(f"响应数据: {lark.JSON.marshal(response.data, indent=4)}")
success_count += 1
# 处理业务结果 except Exception as e:
lark.logger.info(lark.JSON.marshal(response.data, indent=4)) logger.error(f"处理接收者 {local_openid} 时出现异常: {str(e)}")
logger.error(f"异常类型: {type(e).__name__}")
import traceback
logger.error(f"异常堆栈: {traceback.format_exc()}")
fail_count += 1
# 总结报告
logger.info("=" * 50)
logger.info("飞书提醒消息发送完成")
logger.info(f"成功发送: {success_count}")
logger.info(f"发送失败: {fail_count}")
logger.info(f"总计处理: {len(Local_openid)} 个接收者")
logger.info("=" * 50)
if __name__ == "__main__": if __name__ == "__main__":
try:
main() main()
logger.info("程序执行完成")
except Exception as e:
logger.error(f"程序执行过程中出现未捕获的异常: {str(e)}")
logger.error(f"异常类型: {type(e).__name__}")
import traceback
logger.error(f"异常堆栈: {traceback.format_exc()}")
exit(1)
+307 -20
View File
@@ -1,36 +1,323 @@
from wxauto import WeChat from wxauto import WeChat
from datetime import date from datetime import date, datetime, time as dt_time
import sys import sys
import os import os
import subprocess import subprocess
import json
from config import CONFIG from config import CONFIG
import time import time
from logger_config import setup_logger
# 设置日志记录器
logger = setup_logger('sendmsg')
def check_wechat_status(wx, who):
"""检查微信客户端状态"""
logger.info("=" * 30)
logger.info("微信状态检查开始")
logger.info("=" * 30)
status_info = {
'client_ready': False,
'logged_in': False,
'receiver_exists': False,
'sessions_count': 0,
'contacts_count': 0
}
try:
# 检查微信客户端是否就绪
if wx:
status_info['client_ready'] = True
logger.info("✅ 微信客户端对象创建成功")
else:
logger.error("❌ 微信客户端对象创建失败")
return status_info
# 检查微信是否已登录
try:
if hasattr(wx, 'GetSessionList'):
sessions = wx.GetSessionList()
if sessions:
status_info['logged_in'] = True
status_info['sessions_count'] = len(sessions)
logger.info(f"✅ 微信已登录,当前会话数量: {len(sessions)}")
logger.info(f"📱 会话列表: {sessions[:5]}...") # 只显示前5个
else:
logger.warning("⚠️ 微信已登录但会话列表为空")
else:
logger.warning("⚠️ 无法获取会话列表方法")
except Exception as session_e:
logger.warning(f"⚠️ 无法获取会话列表: {str(session_e)}")
# 检查接收者是否存在
try:
if hasattr(wx, 'GetAllContacts'):
contacts = wx.GetAllContacts()
if contacts:
status_info['contacts_count'] = len(contacts)
logger.info(f"📋 联系人总数: {len(contacts)}")
if who in contacts:
status_info['receiver_exists'] = True
logger.info(f"✅ 接收者 '{who}' 存在于联系人列表中")
else:
logger.warning(f"❌ 接收者 '{who}' 不在联系人列表中")
logger.info(f"📝 可用的联系人: {contacts[:10]}...") # 显示前10个
# 尝试模糊匹配
similar_contacts = [c for c in contacts if who.lower() in c.lower()]
if similar_contacts:
logger.info(f"🔍 找到相似的联系人: {similar_contacts}")
else:
logger.warning("⚠️ 联系人列表为空")
else:
logger.warning("⚠️ 无法获取联系人列表方法")
except Exception as contact_e:
logger.warning(f"⚠️ 无法获取联系人列表: {str(contact_e)}")
# 检查微信窗口状态
try:
if hasattr(wx, 'GetWeChatWindow'):
window = wx.GetWeChatWindow()
if window:
logger.info("✅ 微信窗口已找到")
else:
logger.warning("⚠️ 微信窗口未找到")
else:
logger.warning("⚠️ 无法获取微信窗口方法")
except Exception as window_e:
logger.warning(f"⚠️ 无法获取微信窗口: {str(window_e)}")
# 状态总结
logger.info("=" * 30)
logger.info("微信状态检查总结:")
logger.info(f" 🖥️ 客户端就绪: {'' if status_info['client_ready'] else ''}")
logger.info(f" 🔐 登录状态: {'' if status_info['logged_in'] else ''}")
logger.info(f" 👤 接收者存在: {'' if status_info['receiver_exists'] else ''}")
logger.info(f" 📱 会话数量: {status_info['sessions_count']}")
logger.info(f" 📋 联系人数量: {status_info['contacts_count']}")
logger.info("=" * 30)
return status_info
except Exception as e:
logger.error(f"微信状态检查过程中出现错误: {str(e)}")
logger.error(f"错误类型: {type(e).__name__}")
return status_info
def wait_until_time():
"""等待到配置的发送时间"""
now = datetime.now()
# 从配置文件中读取发送时间
send_time_str = CONFIG['sending_time'] # "17:10"
hour, minute = map(int, send_time_str.split(':'))
target_time = now.replace(hour=hour, minute=minute, second=0, microsecond=0)
# 如果当前时间已经过了今天的发送时间,则设置为明天的发送时间
if now >= target_time:
target_time = target_time.replace(day=target_time.day + 1)
wait_seconds = (target_time - now).total_seconds()
logger.info(f"当前时间: {now.strftime('%Y-%m-%d %H:%M:%S')}")
logger.info(f"目标发送时间: {target_time.strftime('%Y-%m-%d %H:%M:%S')}")
logger.info(f"等待时间: {wait_seconds/3600:.2f} 小时 ({wait_seconds:.0f} 秒)")
time.sleep(wait_seconds)
logger.info("等待完成,开始执行发送操作")
def send_daily_message(count):
"""发送每日消息的主函数"""
logger.info(f"开始执行每日消息发送任务,当前尝试次数: {count}")
today = date.today() today = date.today()
formatted_date = today.strftime('%Y-%m-%d') formatted_date = today.strftime('%Y-%m-%d')
file_name = formatted_date+".png"
# 支持多种图片格式:jpg, jpeg, png
file_formats = [".jpg", ".jpeg", ".png"]
# 初始化文件路径变量
file_path = None
file_name = formatted_date + ".jpg"
file_path = CONFIG['file_path'] + file_name file_path = CONFIG['file_path'] + file_name
logger.info(f"目标日期: {formatted_date}")
logger.info(f"目标文件: {file_name}")
logger.info(f"完整文件路径: {file_path}")
num = count
if num >= 4:
logger.error("已尝试4次,程序将退出")
sys.exit(0) # 直接终止程序
try: try:
logger.info("尝试初始化微信客户端...")
wx = WeChat() wx = WeChat()
except: logger.info("微信客户端初始化完成")
#运行send_openmsg.py
except Exception as e:
logger.error(f"微信客户端初始化失败: {str(e)}")
logger.error(f"错误类型: {type(e).__name__}")
logger.error("可能的原因:")
logger.error(" 1. 微信客户端未启动")
logger.error(" 2. 微信客户端未登录")
logger.error(" 3. wxauto库版本不兼容")
logger.error(" 4. 系统权限不足")
logger.info("尝试发送飞书提醒消息...")
subprocess.run([sys.executable, 'send_openmsg.py']) subprocess.run([sys.executable, 'send_openmsg.py'])
sys.exit(1) if num<4:
logger.info(f"等待60秒后重试,当前尝试次数: {num+1}")
time.sleep(60)
msg = 'hello, wxauto!' num += 1
who = 'File Transfer' send_daily_message(num)
if os.path.isfile(file_path):
error_msg = ""
else: else:
error_msg = "没找到指定文件" logger.error("已尝试4次,程序将退出")
#运行send_filemsg.py sys.exit(0) # 直接终止程序
subprocess.run([sys.executable, 'send_filemsg.py'])
sys.exit(1)
print("找到了指定文件!") msg = CONFIG['message']
wx.SendFiles(filepath=file_path, who="File Transfer") who = CONFIG['messages_reciever']
wx.SendMsg(msg=msg, who=who)
logger.info(f"消息内容: {msg}")
logger.info(f"接收者: {who}")
# 使用专门的函数检查微信状态
wechat_status = check_wechat_status(wx, who)
# 如果接收者不存在,给出警告但继续执行
if not wechat_status['receiver_exists']:
logger.warning("⚠️ 接收者不在联系人列表中,但将继续尝试发送")
logger.warning("这可能是因为:")
logger.warning(" 1. 接收者是群聊名称")
logger.warning(" 2. 接收者是特殊联系人(如文件传输助手)")
logger.warning(" 3. 联系人列表获取不完整")
# 依次检查各种格式的文件是否存在
for format in file_formats:
temp_file_name = formatted_date + format
temp_file_path = CONFIG['file_path'] + temp_file_name
if os.path.isfile(temp_file_path):
file_path = temp_file_path
print(f"找到了{format}格式的图片文件!")
break
if file_path and os.path.isfile(file_path):
logger.info("找到了指定文件!")
logger.info(f"文件大小: {os.path.getsize(file_path)} 字节")
# 等待到配置的时间再执行发送操作
wait_until_time()
logger.info("开始发送文件和消息...")
try:
# 发送文件
logger.info(f"正在发送文件: {file_path}")
file_response = wx.SendFiles(filepath=file_path, who=who)
# 检查文件发送结果
if hasattr(file_response, 'success') and file_response.success():
logger.info("文件发送成功")
logger.info(f"文件发送响应: {file_response}")
else:
logger.warning("文件发送可能失败")
logger.warning(f"文件发送响应: {file_response}")
if hasattr(file_response, 'message'):
logger.warning(f"文件发送消息: {file_response.message}")
# 发送消息
logger.info(f"正在发送消息: {msg}")
msg_response = wx.SendMsg(msg=msg, who=who)
# 检查消息发送结果
if hasattr(msg_response, 'success') and msg_response.success():
logger.info("消息发送成功")
logger.info(f"消息发送响应: {msg_response}")
else:
logger.warning("消息发送可能失败")
logger.warning(f"消息发送响应: {msg_response}")
if hasattr(msg_response, 'message'):
logger.warning(f"消息发送消息: {msg_response.message}")
# 综合判断发送结果
file_success = hasattr(file_response, 'success') and file_response.success()
msg_success = hasattr(msg_response, 'success') and msg_response.success()
if file_success and msg_success:
logger.info("🎉 所有内容发送完成!")
logger.info("=" * 50)
logger.info("发送结果总结:")
logger.info(f" 📎 文件发送: {'✅ 成功' if file_success else '❌ 失败'}")
logger.info(f" 💬 消息发送: {'✅ 成功' if msg_success else '❌ 失败'}")
logger.info("=" * 50)
return True
else:
logger.warning("⚠️ 部分内容发送失败")
logger.warning("=" * 50)
logger.warning("发送结果总结:")
logger.warning(f" 📎 文件发送: {'✅ 成功' if file_success else '❌ 失败'}")
logger.warning(f" 💬 消息发送: {'✅ 成功' if msg_success else '❌ 失败'}")
logger.warning("=" * 50)
# 即使部分失败,也记录为成功(因为至少发送了部分内容)
if file_success or msg_success:
logger.info("由于部分内容发送成功,记录为成功")
return True
else:
logger.error("所有内容发送失败,需要重试")
raise Exception("文件发送和消息发送都失败了")
except Exception as e:
logger.error(f"发送过程中出现错误: {str(e)}")
logger.error(f"错误类型: {type(e).__name__}")
# 记录详细的错误信息
if 'file_response' in locals():
logger.error(f"文件发送响应详情: {file_response}")
if 'msg_response' in locals():
logger.error(f"消息发送响应详情: {msg_response}")
# 发送失败时也尝试发送飞书提醒
logger.info("尝试发送飞书提醒消息...")
subprocess.run([sys.executable, 'send_filemsg.py'])
if num<4:
logger.info(f"等待60秒后重试,当前尝试次数: {num+1}")
time.sleep(60)
num += 1
send_daily_message(num)
else:
logger.error("已尝试4次,程序将退出")
sys.exit(0)
else:
print("没找到任何支持格式的图片文件(.jpg, .jpeg, .png)")
logger.warning(f"没找到指定文件: {file_path}")
logger.info("尝试发送飞书提醒消息...")
subprocess.run([sys.executable, 'send_filemsg.py'])
if num<4:
time.sleep(1800)
logger.info(f"等待60秒后重试,当前尝试次数: {num+1}")
time.sleep(60)
num += 1
send_daily_message(num)
else:
logger.error("已尝试4次,程序将退出")
sys.exit(0) # 直接终止程序
# 如果直接运行此脚本,执行发送消息功能
if __name__ == "__main__":
logger.info("=" * 50)
logger.info("开始执行微信消息发送程序")
logger.info("=" * 50)
count = 0
try:
send_daily_message(count)
logger.info("程序执行完成")
except Exception as e:
logger.error(f"程序执行过程中出现未捕获的异常: {str(e)}")
logger.error(f"异常类型: {type(e).__name__}")
import traceback
logger.error(f"异常堆栈: {traceback.format_exc()}")
sys.exit(1)
+100
View File
@@ -0,0 +1,100 @@
import subprocess
import sys
import os
from pathlib import Path
from logger_config import setup_logger
# 设置日志记录器
logger = setup_logger('service_runner')
def run_in_background():
"""在后台运行定时任务"""
logger.info("=" * 50)
logger.info("开始启动后台定时任务")
logger.info("=" * 50)
script_path = Path(__file__).parent / "scheduler.py"
pid_file = Path(__file__).parent / "scheduler_pid.txt"
logger.info(f"脚本路径: {script_path}")
logger.info(f"PID文件路径: {pid_file}")
logger.info(f"当前工作目录: {os.getcwd()}")
# 检查脚本文件是否存在
if not script_path.exists():
logger.error(f"脚本文件不存在: {script_path}")
return False
# 检查是否已经有PID文件
if pid_file.exists():
logger.warning(f"发现已存在的PID文件: {pid_file}")
try:
with open(pid_file, 'r') as f:
old_pid = f.read().strip()
logger.warning(f"已存在的进程ID: {old_pid}")
# 检查进程是否还在运行
try:
import psutil
if psutil.pid_exists(int(old_pid)):
logger.warning(f"进程 {old_pid} 仍在运行,请先停止它")
return False
else:
logger.info(f"进程 {old_pid} 已不存在,删除旧的PID文件")
pid_file.unlink()
except ImportError:
logger.warning("无法检查进程状态,建议手动确认")
return False
except Exception as e:
logger.error(f"读取PID文件失败: {str(e)}")
pid_file.unlink() # 删除损坏的PID文件
# 使用pythonw.exe在后台运行(无窗口)
pythonw_path = sys.executable.replace('python.exe', 'pythonw.exe')
logger.info(f"Python解释器路径: {sys.executable}")
logger.info(f"Pythonw路径: {pythonw_path}")
if not os.path.exists(pythonw_path):
logger.warning(f"pythonw.exe不存在,使用python.exe替代")
pythonw_path = sys.executable
try:
logger.info("正在启动后台进程...")
process = subprocess.Popen([pythonw_path, str(script_path)],
cwd=str(Path(__file__).parent),
creationflags=subprocess.CREATE_NO_WINDOW)
logger.info(f"后台进程启动成功,进程ID: {process.pid}")
# 保存进程ID到文件
with open(pid_file, 'w') as f:
f.write(str(process.pid))
logger.info(f"进程ID已保存到: {pid_file}")
logger.info("=" * 50)
logger.info("后台定时任务启动完成")
logger.info("=" * 50)
return True
except Exception as e:
logger.error(f"启动后台进程失败: {str(e)}")
logger.error(f"错误类型: {type(e).__name__}")
import traceback
logger.error(f"异常堆栈: {traceback.format_exc()}")
return False
if __name__ == "__main__":
try:
success = run_in_background()
if success:
logger.info("程序执行完成")
else:
logger.error("程序执行失败")
exit(1)
except Exception as e:
logger.error(f"程序执行过程中出现未捕获的异常: {str(e)}")
logger.error(f"异常类型: {type(e).__name__}")
import traceback
logger.error(f"异常堆栈: {traceback.format_exc()}")
exit(1)
+179
View File
@@ -0,0 +1,179 @@
import subprocess
import sys
import os
import psutil
from pathlib import Path
from logger_config import setup_logger
# 设置日志记录器
logger = setup_logger('stop_scheduler')
def stop_scheduler():
"""停止定时任务进程和相关的sendmsg.py进程"""
logger.info("=" * 50)
logger.info("开始停止定时任务进程")
logger.info("=" * 50)
stopped_count = 0
current_dir = str(Path(__file__).parent)
logger.info(f"当前工作目录: {current_dir}")
# 方法0: 从PID文件中读取进程ID
pid_file = Path(__file__).parent / "scheduler_pid.txt"
if pid_file.exists():
logger.info(f"发现PID文件: {pid_file}")
try:
with open(pid_file, 'r') as f:
pid = int(f.read().strip())
logger.info(f"从PID文件中读取到进程ID: {pid}")
try:
process = psutil.Process(pid)
logger.info(f"找到进程: PID={pid}, 名称={process.name()}")
logger.info(f"进程命令行: {' '.join(process.cmdline())}")
process.terminate()
logger.info(f"已发送终止信号给进程 PID={pid}")
try:
process.wait(timeout=5) # 等待进程正常结束
logger.info(f"进程 PID={pid} 已正常终止")
except psutil.TimeoutExpired:
logger.warning(f"进程 PID={pid} 未在5秒内终止,强制结束")
process.kill()
logger.info(f"已强制结束进程 PID={pid}")
stopped_count += 1
# 删除PID文件
pid_file.unlink()
logger.info("PID文件已删除")
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess) as e:
logger.warning(f"PID文件中的进程 {pid} 已不存在或无法访问: {str(e)}")
# 删除过期的PID文件
pid_file.unlink()
logger.info("过期的PID文件已删除")
except Exception as e:
logger.error(f"从PID文件读取进程ID失败: {str(e)}")
logger.error(f"错误类型: {type(e).__name__}")
else:
logger.info("未找到PID文件,尝试其他方法查找进程...")
try:
# 方法1: 使用psutil精确查找进程
logger.info("使用psutil查找相关进程...")
for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
try:
if proc.info['name'] in ['python.exe', 'pythonw.exe']:
cmdline = proc.info['cmdline']
if cmdline and len(cmdline) > 1:
# 检查是否是我们的scheduler.py或sendmsg.py进程
cmdline_str = ' '.join(cmdline)
if ('scheduler.py' in cmdline_str or 'sendmsg.py' in cmdline_str) and current_dir in cmdline_str:
logger.info(f"找到目标进程: PID={proc.info['pid']}")
logger.info(f"进程名称: {proc.info['name']}")
logger.info(f"命令行: {cmdline_str}")
try:
proc.terminate()
logger.info(f"已发送终止信号给进程 PID={proc.info['pid']}")
try:
proc.wait(timeout=5) # 等待进程正常结束
logger.info(f"进程 PID={proc.info['pid']} 已正常终止")
except psutil.TimeoutExpired:
logger.warning(f"进程 PID={proc.info['pid']} 未在5秒内终止,强制结束")
proc.kill()
logger.info(f"已强制结束进程 PID={proc.info['pid']}")
stopped_count += 1
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess) as e:
logger.warning(f"进程 {proc.info['pid']} 已不存在或无法访问: {str(e)}")
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess) as e:
logger.debug(f"跳过进程 {proc.info.get('pid', 'unknown')}: {str(e)}")
continue
except ImportError:
logger.warning("psutil模块未安装,使用备用方法...")
# 方法2: 备用方法 - 改进的tasklist方式
try:
logger.info("使用wmic命令查找进程...")
# 获取详细的进程信息
result = subprocess.run(['wmic', 'process', 'where', 'name="pythonw.exe"', 'get', 'ProcessId,CommandLine', '/format:csv'],
capture_output=True, text=True, encoding='gbk')
lines = result.stdout.strip().split('\n')
for line in lines[1:]: # 跳过标题行
if line.strip() and ('scheduler.py' in line or 'sendmsg.py' in line) and current_dir in line:
logger.info(f"找到目标进程: {line}")
# 提取PID
parts = line.split(',')
if len(parts) >= 2:
pid = parts[-1].strip()
if pid.isdigit():
logger.info(f"正在终止进程 PID={pid}")
subprocess.run(['taskkill', '/F', '/PID', pid], check=True)
logger.info(f"已终止进程 PID={pid}")
stopped_count += 1
except subprocess.CalledProcessError as e:
logger.error(f"wmic方法执行失败: {e}")
# 方法3: 最后的备用方案
try:
logger.info("使用tasklist命令查找进程...")
result = subprocess.run(['tasklist', '/FI', 'IMAGENAME eq pythonw.exe', '/FO', 'CSV'],
capture_output=True, text=True)
if 'pythonw.exe' in result.stdout:
logger.warning("警告: 找到pythonw.exe进程,但无法确定是否为目标进程")
logger.warning("建议手动检查进程列表")
logger.info("进程列表:")
logger.info(result.stdout)
response = input("是否强制结束所有pythonw.exe进程?(y/N): ")
if response.lower() == 'y':
logger.info("正在强制结束所有pythonw.exe进程...")
subprocess.run(['taskkill', '/F', '/IM', 'pythonw.exe'], check=True)
logger.info("已强制结束所有pythonw.exe进程")
stopped_count += 1
except subprocess.CalledProcessError as e:
logger.error(f"最后备用方案失败: {e}")
# 总结报告
logger.info("=" * 50)
if stopped_count > 0:
logger.info(f"成功停止了 {stopped_count} 个定时任务相关进程(scheduler.py或sendmsg.py)")
else:
logger.info("未找到运行中的定时任务相关进程(scheduler.py或sendmsg.py)")
logger.info("=" * 50)
# 额外检查:查看是否还有相关进程
logger.info("检查剩余进程...")
try:
result = subprocess.run(['tasklist', '/FI', 'IMAGENAME eq pythonw.exe'],
capture_output=True, text=True)
if 'pythonw.exe' in result.stdout:
logger.info("仍有pythonw.exe进程在运行:")
logger.info(result.stdout)
else:
logger.info("没有发现pythonw.exe进程")
except Exception as e:
logger.warning(f"检查剩余进程失败: {str(e)}")
if __name__ == "__main__":
try:
stop_scheduler()
logger.info("程序执行完成")
except Exception as e:
logger.error(f"程序执行过程中出现未捕获的异常: {str(e)}")
logger.error(f"异常类型: {type(e).__name__}")
import traceback
logger.error(f"异常堆栈: {traceback.format_exc()}")
exit(1)
+231
View File
@@ -0,0 +1,231 @@
#!/usr/bin/env python3
"""
日志查看工具
用于快速查看和搜索wxmsg程序的日志文件
"""
import os
import sys
from pathlib import Path
from datetime import datetime, timedelta
import argparse
def list_log_files():
"""列出所有可用的日志文件"""
logs_dir = Path("wxauto_logs")
if not logs_dir.exists():
print("❌ 日志目录不存在: wxauto_logs/")
return []
log_files = []
for log_file in logs_dir.glob("*.log"):
stat = log_file.stat()
size_mb = stat.st_size / (1024 * 1024)
modified_time = datetime.fromtimestamp(stat.st_mtime)
log_files.append({
'name': log_file.name,
'path': log_file,
'size_mb': size_mb,
'modified': modified_time
})
# 按修改时间排序,最新的在前
log_files.sort(key=lambda x: x['modified'], reverse=True)
return log_files
def show_log_files():
"""显示日志文件列表"""
log_files = list_log_files()
if not log_files:
print("📁 没有找到日志文件")
return
print("📋 可用的日志文件:")
print("=" * 80)
print(f"{'文件名':<30} {'大小(MB)':<10} {'最后修改':<20} {'状态'}")
print("=" * 80)
for log_file in log_files:
status = "🟢 最新" if log_file['modified'].date() == datetime.now().date() else "🟡 旧"
print(f"{log_file['name']:<30} {log_file['size_mb']:<10.2f} {log_file['modified'].strftime('%Y-%m-%d %H:%M:%S'):<20} {status}")
print("=" * 80)
def view_log_file(filename, lines=50, search=None, level=None):
"""查看指定的日志文件"""
logs_dir = Path("wxauto_logs")
log_path = logs_dir / filename
if not log_path.exists():
print(f"❌ 日志文件不存在: {filename}")
return
print(f"📖 查看日志文件: {filename}")
print(f"📁 文件路径: {log_path.absolute()}")
print(f"📊 文件大小: {log_path.stat().st_size / 1024:.2f} KB")
print("=" * 80)
try:
with open(log_path, 'r', encoding='utf-8') as f:
all_lines = f.readlines()
# 过滤行
filtered_lines = []
for line in all_lines:
# 按级别过滤
if level and level.upper() not in line.upper():
continue
# 按搜索词过滤
if search and search.lower() not in line.lower():
continue
filtered_lines.append(line)
# 显示最后N行
if lines > 0:
filtered_lines = filtered_lines[-lines:]
if not filtered_lines:
print("🔍 没有找到匹配的日志条目")
return
print(f"📝 显示 {len(filtered_lines)} 条日志条目:")
print("-" * 80)
for line in filtered_lines:
line = line.strip()
if not line:
continue
# 高亮显示不同级别的日志
if "ERROR" in line:
print(f"🔴 {line}")
elif "WARNING" in line:
print(f"🟡 {line}")
elif "INFO" in line:
print(f"🔵 {line}")
elif "DEBUG" in line:
print(f"{line}")
else:
print(f" {line}")
except Exception as e:
print(f"❌ 读取日志文件失败: {str(e)}")
def search_logs(search_term, level=None, days=1):
"""搜索所有日志文件"""
logs_dir = Path("wxauto_logs")
if not logs_dir.exists():
print("❌ 日志目录不存在: wxauto_logs/")
return
print(f"🔍 在所有日志文件中搜索: '{search_term}'")
if level:
print(f"📊 日志级别: {level}")
print(f"📅 搜索范围: 最近 {days}")
print("=" * 80)
found_count = 0
cutoff_date = datetime.now() - timedelta(days=days)
for log_file in logs_dir.glob("*.log"):
try:
stat = log_file.stat()
modified_time = datetime.fromtimestamp(stat.st_mtime)
# 检查文件是否在时间范围内
if modified_time < cutoff_date:
continue
with open(log_file, 'r', encoding='utf-8') as f:
file_lines = f.readlines()
matching_lines = []
for line in file_lines:
line = line.strip()
if not line:
continue
# 按级别过滤
if level and level.upper() not in line.upper():
continue
# 按搜索词过滤
if search_term.lower() in line.lower():
matching_lines.append(line)
if matching_lines:
print(f"\n📁 文件: {log_file.name}")
print(f"📅 修改时间: {modified_time.strftime('%Y-%m-%d %H:%M:%S')}")
print(f"🔍 找到 {len(matching_lines)} 条匹配记录:")
print("-" * 60)
for line in matching_lines[:20]: # 限制每个文件显示20条
# 高亮显示不同级别的日志
if "ERROR" in line:
print(f"🔴 {line}")
elif "WARNING" in line:
print(f"🟡 {line}")
elif "INFO" in line:
print(f"🔵 {line}")
elif "DEBUG" in line:
print(f"{line}")
else:
print(f" {line}")
if len(matching_lines) > 20:
print(f"... 还有 {len(matching_lines) - 20} 条记录")
found_count += len(matching_lines)
except Exception as e:
print(f"❌ 读取文件 {log_file.name} 失败: {str(e)}")
print("=" * 80)
print(f"🔍 搜索完成,总共找到 {found_count} 条匹配记录")
def main():
parser = argparse.ArgumentParser(description="WXMSG日志查看工具")
parser.add_argument("action", choices=["list", "view", "search"],
help="操作类型: list(列出文件), view(查看文件), search(搜索)")
parser.add_argument("--file", "-f", help="要查看的日志文件名")
parser.add_argument("--lines", "-n", type=int, default=50,
help="显示的行数 (默认: 50, 0表示全部)")
parser.add_argument("--search", "-s", help="搜索关键词")
parser.add_argument("--level", "-l", choices=["INFO", "WARNING", "ERROR", "DEBUG"],
help="按日志级别过滤")
parser.add_argument("--days", "-d", type=int, default=1,
help="搜索最近几天的日志 (默认: 1)")
args = parser.parse_args()
if args.action == "list":
show_log_files()
elif args.action == "view":
if not args.file:
print("❌ 请指定要查看的日志文件名")
print("💡 使用 --file 参数或先运行 'python view_logs.py list' 查看可用文件")
return
view_log_file(args.file, args.lines, args.search, args.level)
elif args.action == "search":
if not args.search:
print("❌ 请指定搜索关键词")
print("💡 使用 --search 参数指定要搜索的内容")
return
search_logs(args.search, args.level, args.days)
if __name__ == "__main__":
if len(sys.argv) == 1:
print("🔧 WXMSG日志查看工具")
print("=" * 40)
print("用法:")
print(" python view_logs.py list # 列出所有日志文件")
print(" python view_logs.py view -f filename.log # 查看指定日志文件")
print(" python view_logs.py search -s keyword # 搜索日志内容")
print("\n更多帮助: python view_logs.py --help")
print("=" * 40)
else:
main()