Python parsing log file to extract events in real time
C programs usually seek to the current position to clear any “end of file” flags. But as @9000 correctly pointed out, python apparently takes care of this, so you can read from the same file repeatedly even if it has reached end of file.
You might have to take care of incomplete lines, though. If your application writes its log in pieces, then you want to make sure that you handle whole lines, and not those pieces. The following code will accomplish that:
f = open('some.log', 'r')
while True:
line = ''
while len(line) == 0 or line[-1] != '\n':
tail = f.readline()
if tail == '':
time.sleep(0.1) # avoid busy waiting
# f.seek(0, io.SEEK_CUR) # appears to be unneccessary
continue
line += tail
process(line)
Parse a custom log file in python
You don't need to be that precise with your regex:
import re
log_pattern = re.compile(r"([0-9\-]*)T([0-9\-:.+]*)\s*\[([^]]*)\](.*)")
with open(name, "r") as f:
for line in f:
match = log_pattern.match(line)
if not match:
continue
grps = match.groups()
print("Log line:")
print(f" date:{grps[0]},\n time:{grps[1]},\n type:{grps[2]},\n text:{grps[3]}")
You could even imagine being less precise than that, for example r"(.*)T([^\s]*)\s*\[([^]]*)\](.*)"
works too. Here is a nice tool to use to test regular expressions: regex101.
Related Topics
Multiprocessing Module Showing Memory for Each Child Process Same as Main Process
How Would a Python Script Running on Linux Call a Routine in a Python Script Running Under Wine
What Are the Tkinter Events for Horizontal Edge Scrolling (In Linux)
Maltparser Giving Error in Nltk
A Simple Python Deployment Problem - a Whole World of Pain
Importerror: Libtk8.6.So: Cannot Open Shared Object File: No Such File or Directory
How to Use Os.Umask() in Python
How to Perform Low Level I/O on a Linux Device File in Python
Python Portable, Linux & Windows
How to Deal with Linux/Python Dependencies
How to Download Python from Command-Line
How to Kill Zombie Processes Created by Multiprocessing Module
Howto Do Python Command-Line Autocompletion But Not Only at the Beginning of a String
Pyodbc:Can't Open the Driver Even If It Exists
Install Tkinter and Python Locally