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