-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log_Monitor_V2
133 lines (103 loc) · 5.28 KB
/
Log_Monitor_V2
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os
import tkinter as tk
from tkinter import filedialog, messagebox
def browse_directory():
directory = filedialog.askdirectory()
log_directory_entry.delete(0, tk.END)
log_directory_entry.insert(0, directory)
def monitor_logs():
log_directory = log_directory_entry.get().strip()
search_term = search_term_entry.get().strip()
if not log_directory:
messagebox.showwarning("Input Error", "Please provide the log directory.")
return
if not search_term:
messagebox.showwarning("Input Error", "Please provide a search term.")
return
errors_found = False
log_text.delete(1.0, tk.END) # Clear previous results
# Search for all .log files in the directory
for file_name in os.listdir(log_directory):
if file_name.endswith('.log'):
file_path = os.path.join(log_directory, file_name)
try:
# Open the file with a specified encoding and handle errors
with open(file_path, 'r', encoding='utf-8', errors='replace') as file:
for line in file:
if search_term in line:
# Determine color based on log type
if "ERROR" in line:
color = 'error'
elif "DEBUG" in line:
color = 'debug'
elif "INFO" in line:
color = 'info'
else:
color = None
# Insert the highlighted file name in bold yellow
log_text.insert(tk.END, f"{file_name}: ", ('filename',))
# Insert the log content with corresponding color
log_text.insert(tk.END, f"{line.strip()}\n", (color,))
errors_found = True
except FileNotFoundError:
messagebox.showerror("File Not Found", f"Log file {file_path} not found.")
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {e}")
if not errors_found:
messagebox.showinfo("No Results", f"No occurrences of '{search_term}' found in the log files.")
def close_application():
root.quit() # Closes the application
def minimize_window():
root.iconify() # Minimizes the window
def show_about():
messagebox.showinfo("About","Prepared By:\n Md.Shamiul Huda Saurov\n Version: 0.2")
# Create the main window
root = tk.Tk()
root.title("Log Monitor")
# Set full-screen mode
root.attributes('-fullscreen', True)
# Add a menu for minimize, close, and about options
menu_bar = tk.Menu(root)
window_menu = tk.Menu(menu_bar, tearoff=0)
window_menu.add_command(label="Minimize", command=minimize_window)
window_menu.add_separator()
window_menu.add_command(label="About", command=show_about)
window_menu.add_separator()
window_menu.add_command(label="Close", command=close_application)
menu_bar.add_cascade(label="Window", menu=window_menu)
# Add the menu to the window
root.config(menu=menu_bar)
# Set a more visible dark theme (dark gray background, light text)
root.configure(bg='#2b2b2b') # Slightly lighter dark grey background
# Configure grid layout to expand widgets
root.grid_rowconfigure(3, weight=1)
root.grid_columnconfigure(1, weight=1)
# Create a label and entry for log directory
log_directory_label = tk.Label(root, text="Log Directory:", bg="white", fg="black")
log_directory_label.grid(row=0, column=0, padx=10, pady=5, sticky="w")
log_directory_entry = tk.Entry(root, width=70, bg="white", fg="black", insertbackground="black")
log_directory_entry.grid(row=0, column=1, padx=10, pady=5, sticky="ew")
browse_directory_button = tk.Button(root, text="Browse", command=browse_directory, bg="white", fg="black")
browse_directory_button.grid(row=0, column=2, padx=10, pady=5)
# Create a label and entry for search term
search_term_label = tk.Label(root, text="Search Term:", bg="white", fg="black")
search_term_label.grid(row=1, column=0, padx=10, pady=5, sticky="w")
search_term_entry = tk.Entry(root, width=70, bg="white", fg="black", insertbackground="black")
search_term_entry.grid(row=1, column=1, padx=10, pady=5, sticky="ew")
# Create a button to start monitoring the logs
monitor_button = tk.Button(root, text="Monitor Logs", command=monitor_logs, bg="white", fg="black")
monitor_button.grid(row=2, columnspan=3, padx=10, pady=10)
# Create a Text widget to display errors found
log_text = tk.Text(root, width=100, height=25, bg="#1e1e1e", fg="white", wrap="word", insertbackground="white")
log_text.grid(row=3, columnspan=3, padx=10, pady=10, sticky="nsew")
# Create a scrollbar for the Text widget
scrollbar = tk.Scrollbar(root, command=log_text.yview, bg="#3a3a3a")
scrollbar.grid(row=3, column=3, sticky="ns")
log_text.config(yscrollcommand=scrollbar.set)
# Configure text tags for coloring
log_text.tag_configure('error', foreground='red')
log_text.tag_configure('debug', foreground='yellow')
log_text.tag_configure('info', foreground='light blue')
log_text.tag_configure('filename', foreground='light goldenrod', font=('Helvetica', 10, 'bold')) # File name styling
# Run the application
root.mainloop()