-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclean_logs.py
More file actions
87 lines (69 loc) · 2.73 KB
/
clean_logs.py
File metadata and controls
87 lines (69 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
"""
Utility script to clean up the app.log file.
This script removes log entries from previous days, keeping only the current day's logs.
It can be run manually to clean up the app.log file if it has grown too large.
Usage:
python clean_logs.py
Note:
This application only uses app.log for logging. No other log files should be created.
"""
import os
import sys
import datetime
import argparse
def cleanup_old_logs(log_file_path):
"""
Remove log entries from previous days, keeping only the current day's logs.
Args:
log_file_path: Path to the log file
"""
today = datetime.datetime.now().strftime('%Y-%m-%d')
if not os.path.exists(log_file_path):
print(f"Log file not found: {log_file_path}")
return False
try:
print(f"Cleaning up logs in: {log_file_path}")
with open(log_file_path, 'r') as f:
log_content = f.readlines()
original_size = len(log_content)
# Filter logs to keep only current day's entries
current_day_logs = []
for line in log_content:
# Only check lines that might contain a timestamp
if len(line) > 10 and '-' in line[:10]:
try:
# Extract the date part (YYYY-MM-DD)
log_date = line[:10]
if log_date == today:
current_day_logs.append(line)
except (ValueError, IndexError):
# If the line doesn't have a proper date format, keep it for safety
current_day_logs.append(line)
else:
# Keep lines without timestamps (could be stack traces, etc.)
current_day_logs.append(line)
# Write back only today's logs
with open(log_file_path, 'w') as f:
f.writelines(current_day_logs)
new_size = len(current_day_logs)
lines_removed = original_size - new_size
print(f"Log cleanup complete: {lines_removed} lines removed from {log_file_path}")
return True
except Exception as e:
print(f"Error during log cleanup: {str(e)}")
return False
def main():
# Get the project root directory
project_root = os.path.dirname(os.path.abspath(__file__))
# Create logs directory if it doesn't exist
logs_dir = os.path.join(project_root, 'logs')
os.makedirs(logs_dir, exist_ok=True)
# The only log file used by the application
app_log_path = os.path.join(logs_dir, 'app.log')
if cleanup_old_logs(app_log_path):
print("Successfully cleaned app.log")
else:
print("Failed to clean app.log")
if __name__ == "__main__":
main()