1+ import tkinter as tk
2+ from tkinter import ttk , filedialog , scrolledtext
3+ import matplotlib .pyplot as plt
4+
5+ def read_log_file (file_path ):
6+ data = {"errors" : 0 , "warnings" : 0 , "info" : 0 }
7+ with open (file_path , 'r' ) as file :
8+ for line in file :
9+ if "ERROR" in line :
10+ data ["errors" ] += 1
11+ elif "WARNING" in line :
12+ data ["warnings" ] += 1
13+ elif "INFO" in line :
14+ data ["info" ] += 1
15+ return data
16+
17+ def generate_graph (data ):
18+ labels = list (data .keys ())
19+ values = list (data .values ())
20+
21+ plt .bar (labels , values , color = ['red' , 'orange' , 'blue' ])
22+ plt .xlabel ('Log Type' )
23+ plt .ylabel ('Count' )
24+ plt .title ('Log File Visualizer' )
25+ plt .show ()
26+
27+ def on_browse_click ():
28+ file_path = filedialog .askopenfilename (filetypes = [("Log files" , "*.log" )])
29+ if file_path :
30+ log_data = read_log_file (file_path )
31+ generate_graph (log_data )
32+ with open (file_path , 'r' ) as file :
33+ log_text .delete ("1.0" , tk .END )
34+ log_text .insert (tk .END , file .read ())
35+
36+ root = tk .Tk ()
37+ root .title ("Log Analyzer" )
38+
39+ main_frame = ttk .Frame (root )
40+ main_frame .pack (padx = 10 , pady = 10 )
41+
42+ heading_label = ttk .Label (main_frame , text = "Log Analyzer" , font = ("Helvetica" , 16 ))
43+ heading_label .pack ()
44+
45+ subheading_label = ttk .Label (main_frame , text = "Select a log file and analyze its data" , font = ("Helvetica" , 12 ))
46+ subheading_label .pack (pady = 5 )
47+
48+ browse_button = ttk .Button (main_frame , text = "Browse" , command = on_browse_click )
49+ browse_button .pack (pady = 10 )
50+
51+ log_text = scrolledtext .ScrolledText (main_frame , wrap = tk .WORD , width = 50 , height = 10 )
52+ log_text .pack ()
53+
54+ root .mainloop ()
0 commit comments