当前位置: 首页 > news >正文

【linux】log 保存和过滤

log 保存

./run.sh 2>&1 | tee -a /home/name/log.txt

log 过滤

import os
import re# Expanded regular expression to match a wider range of error patterns
error_patterns = re.compile(# r'(error|exception|traceback|fail|failed|fatal|critical|warn|warning)', r'(error|exception|traceback|fail|failed|fatal|critical)', re.IGNORECASE
)# List of log files to process
log_files = ['log_1.txt', 'log_2.txt', 'log_3.txt', 'log_4.txt']# Create a directory to save filtered logs
filtered_dir = 'filtered_logs'
os.makedirs(filtered_dir, exist_ok=True)# Number of context lines to show before and after the match
context_lines = 20# Process each log file
for log_file in log_files:with open(log_file, 'r') as infile:lines = infile.readlines()# Find the indices of lines that match error patternserror_indices = [i for i, line in enumerate(lines) if error_patterns.search(line)]# Collect the context lines for each errorerror_contexts = []for idx in error_indices:start = max(0, idx - context_lines)end = min(len(lines), idx + context_lines + 1)error_contexts.extend(lines[start:end])error_contexts.append("\n" + "="*80 + "\n")  # Separator for readability# Write the context lines to a new filefiltered_log_file = os.path.join(filtered_dir, f'filtered_{log_file}')with open(filtered_log_file, 'w') as outfile:outfile.writelines(error_contexts)print(f'Filtered errors with context written to {filtered_log_file}')
http://www.lryc.cn/news/398550.html

相关文章:

  • GeoTrust ——适合企业使用的SSL证书!
  • Kubelet 认证
  • aws slb
  • 【AI大模型】ChatGPT-4 对比 ChatGPT-3.5:有哪些优势
  • 详解yolov5的网络结构
  • 汽车零配件行业看板管理系统应用
  • 【Go】函数的使用
  • 宝塔面板运行Admin.net框架
  • Javaweb11-Filter过滤器
  • 【AI-7】CUDA
  • ctfshow-web入门-文件上传(web164、web165)图片二次渲染绕过
  • 基于实现Runnable接口的java多线程
  • 如何在uniapp中使用websocket?
  • PCL 点云PFH特征描述子
  • linux程序安装-编译-rpm-yum
  • 【网络协议】PIM
  • Redis 中的跳跃表(Skiplist)基本介绍
  • C语言编译和编译预处理
  • ahb 总线的一些思考
  • spark shuffle写操作——SortShuffleWriter
  • ESP32CAM物联网教学12
  • 【C++精华铺】12.STL list模拟实现
  • ChatGPT Mac App 发布!
  • ACE之ACE_Time_Value
  • [论文笔记] 自对齐指令反翻译:SELF-ALIGNMENT WITH INSTRUCTION BACKTRANSLATION
  • 算术运算符. 二
  • 代码优化方法记录
  • qt 图形、图像、3D相关知识
  • 【逆向基础】十、工具分享之DIE(Detect It Easy)
  • Netcat:——网络瑞士军刀