【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 patterns
    error_indices = [i for i, line in enumerate(lines) if error_patterns.search(line)]
    
    # Collect the context lines for each error
    error_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 file
    filtered_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}')

相关推荐

  1. 【linux】log 保存过滤

    2024-07-11 18:44:02       20 阅读
  2. spark分布式预测保存过程中遇到的问题记录

    2024-07-11 18:44:02       29 阅读
  3. MySQL 保姆级教程(四):过滤数据

    2024-07-11 18:44:02       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 18:44:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 18:44:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 18:44:02       58 阅读
  4. Python语言-面向对象

    2024-07-11 18:44:02       69 阅读

热门阅读

  1. OpenCV和CUDA匹配,使用源码构建OpenCV

    2024-07-11 18:44:02       21 阅读
  2. Oracle左连接过滤条件注意事项

    2024-07-11 18:44:02       20 阅读
  3. Spring-Data-ES-template工具类使用

    2024-07-11 18:44:02       23 阅读
  4. 四种常见的Http请求方式

    2024-07-11 18:44:02       21 阅读
  5. [USACO5.3] 巨大的牛棚Big Barn

    2024-07-11 18:44:02       26 阅读
  6. python杨辉三角的两种书写方式

    2024-07-11 18:44:02       21 阅读
  7. 【Go - 常见的5类循环】

    2024-07-11 18:44:02       26 阅读
  8. 二叉搜索树的最近公共祖先

    2024-07-11 18:44:02       23 阅读