49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
import pymysql
|
|
import re
|
|
from collections import Counter
|
|
|
|
def get_db_connection():
|
|
return pymysql.connect(
|
|
host='localhost',
|
|
user='root',
|
|
password='45278434',
|
|
database='pm_proto_test',
|
|
charset='utf8mb4',
|
|
cursorclass=pymysql.cursors.DictCursor
|
|
)
|
|
|
|
def analyze_logs():
|
|
conn = get_db_connection()
|
|
try:
|
|
with conn.cursor() as cursor:
|
|
cursor.execute("SELECT DISTINCT recent_log FROM projects_history WHERE recent_log IS NOT NULL AND recent_log != ''")
|
|
rows = cursor.fetchall()
|
|
|
|
logs = [r['recent_log'] for r in rows]
|
|
|
|
output = []
|
|
output.append("[Raw Log Samples]")
|
|
for log in logs[:20]:
|
|
output.append(f"- {log}")
|
|
|
|
patterns = []
|
|
for log in logs:
|
|
p = re.sub(r'\d{2,4}\.\d{2}\.\d{2}', '[DATE]', log)
|
|
p = re.sub(r'\d+', '[NUM]', p)
|
|
patterns.append(p)
|
|
|
|
output.append("\n[Log Patterns Frequency]")
|
|
pattern_counts = Counter(patterns).most_common(20)
|
|
for p, count in pattern_counts:
|
|
output.append(f"({count}) {p}")
|
|
|
|
with open("log_analysis_result.txt", "w", encoding="utf-8") as f:
|
|
f.write("\n".join(output))
|
|
print("Analysis complete. Result saved to log_analysis_result.txt")
|
|
|
|
finally:
|
|
conn.close()
|
|
|
|
if __name__ == "__main__":
|
|
analyze_logs()
|