【linux】log 保存和过滤

发布于:2024-07-13 ⋅ 阅读:(151) ⋅ 点赞:(0)

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}')

网站公告

今日签到

点亮在社区的每一天
去签到