提交 63d8115f authored 作者: 陈泽健's avatar 陈泽健

处理优化

上级 a0c135c1
...@@ -19,4 +19,4 @@ ...@@ -19,4 +19,4 @@
- 补充监测服务的前置ngrok映射以及端口开放的使用指令注释,处理注释错误。 - 补充监测服务的前置ngrok映射以及端口开放的使用指令注释,处理注释错误。
- 补充_process_line函数对日志去重的IP过滤,parse_log_line函数增加IP过滤,处理相同错误日志因IP不同识别为不同错误问题。 - 补充_process_line函数对日志去重的IP过滤,parse_log_line函数增加IP过滤,处理相同错误日志因IP不同识别为不同错误问题。
- 移除控制台的详细错误日志输出,保留关键状态日志(连接状态、监控启停等),强异常处理和日志记录,在JSON文件名中加入服务器IP标识,添加了主程序入口的详细日志 - 移除控制台的详细错误日志输出,保留关键状态日志(连接状态、监控启停等),强异常处理和日志记录,在JSON文件名中加入服务器IP标识,添加了主程序入口的详细日志
- 修改_process_line方法中的去重逻辑,确保检测到冷却期内的错误时立即返回,处理存储路径问题。 - 修改_process_line方法中的去重逻辑,确保检测到冷却期内的错误时立即返回,处理存储路径问题,处理优化。
\ No newline at end of file \ No newline at end of file
...@@ -236,12 +236,12 @@ class LogMonitor: ...@@ -236,12 +236,12 @@ class LogMonitor:
1. 维护行缓冲区 1. 维护行缓冲区
2. 解析日志行 2. 解析日志行
3. 检查是否ERROR日志 3. 检查是否ERROR日志
4. 生成去重key并检查冷却期 4. 特殊过滤(JWT验证类错误直接忽略)
5. 获取上下文并保存到文件 5. 生成去重key并检查冷却期
6. 发送钉钉通知(非冷却期内) 6. 获取上下文并保存到文件
7. 发送钉钉通知(非冷却期内)
""" """
with self.lock: with self.lock:
# 维护行缓冲区(保留最近500行)
self.line_buffer.append(line) self.line_buffer.append(line)
if len(self.line_buffer) > self.buffer_size: if len(self.line_buffer) > self.buffer_size:
self.line_buffer.pop(0) self.line_buffer.pop(0)
...@@ -252,31 +252,31 @@ class LogMonitor: ...@@ -252,31 +252,31 @@ class LogMonitor:
if not parsed or parsed['level'] != 'ERROR': if not parsed or parsed['level'] != 'ERROR':
return return
# ----- 新增过滤规则 -----
if parsed['module'] == 'JwtTokenUtiljwt工具类' and parsed['action'] == 'JWT格式验证':
logging.info(f"忽略JWT验证类错误: {parsed['module']}/{parsed['action']}")
return
# ----------------------
# 生成去重key(过滤动态内容) # 生成去重key(过滤动态内容)
clean_message = re.sub( clean_message = re.sub(
r'\d{1,3}\\.\d{1,3}\\.\d{1,3}\\.\d{1,3}', r'\d{1,3}\\.\d{1,3}\\.\d{1,3}\\.\d{1,3}',
'[IP]', '[IP]',
parsed['message'] parsed['message']
) )
clean_message = re.sub(r'\d+', '[NUM]', clean_message) # 过滤所有数字 clean_message = re.sub(r'\d+', '[NUM]', clean_message)
key = f"{parsed['module']}|{parsed['action']}|{clean_message}" key = f"{parsed['module']}|{parsed['action']}|{clean_message}"
error_hash = hash(key) error_hash = hash(key)
current_time = time.time() current_time = time.time()
# 检查冷却期(必须先于获取上下文) # 检查冷却期
if error_hash in self.sent_errors: if error_hash in self.sent_errors:
time_diff = current_time - self.sent_errors[error_hash] time_diff = current_time - self.sent_errors[error_hash]
if time_diff < self.resend_interval: if time_diff < self.resend_interval:
logging.info( logging.info(f"冷却期内相同错误 [{parsed['module']}/{parsed['action']}]")
f"冷却期内相同错误 [{parsed['module']}/{parsed['action']}] " return
f"上次发送: {time_diff:.1f}/{self.resend_interval}秒前"
)
return # 关键点:立即返回不再处理
logging.info( logging.info(f"发现新ERROR日志 [{parsed['module']}/{parsed['action']}]")
f"发现新ERROR日志 [{parsed['module']}/{parsed['action']}] "
f"消息: {clean_message[:100]}..."
)
# 获取日志上下文(只对新错误执行) # 获取日志上下文(只对新错误执行)
full_log = self.get_remote_log_with_paramiko( full_log = self.get_remote_log_with_paramiko(
...@@ -305,7 +305,7 @@ class LogMonitor: ...@@ -305,7 +305,7 @@ class LogMonitor:
'log_path': self.log_path, 'log_path': self.log_path,
'error_line': line, 'error_line': line,
'context': context, 'context': context,
'fingerprint': key, # 记录错误指纹 'fingerprint': key,
'structured': parsed 'structured': parsed
}) })
...@@ -324,7 +324,7 @@ class LogMonitor: ...@@ -324,7 +324,7 @@ class LogMonitor:
except Exception as e: except Exception as e:
logging.error(f"钉钉发送失败: {str(e)}") logging.error(f"钉钉发送失败: {str(e)}")
break # 找到错误行后退出循环 break
except Exception as e: except Exception as e:
logging.error(f"处理日志行异常: {str(e)}", exc_info=True) logging.error(f"处理日志行异常: {str(e)}", exc_info=True)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论