How to parse only the beginning of this log from messages

27 views Asked by At
2015-05-22 16:46:46,985 - __main__ - INFO - Starting to Wait for Files
2015-05-22 16:46:56,645 - __main__ - INFO - Starting: Attempt 1 Checking for New Files from gs://folder/folder/
2015-05-22 16:47:46,488 - __main__ - INFO - Success: Downloading the Files from Cloud Storage: Return Code - 0 and FileCount 1
2015-05-22 16:48:48,180 - __main__ - ERROR - Failed: Waiting for files the Files from Cloud Storage: gs://folder/folder/

I need to print every message with ERROR

Failed: Waiting for files the Files from Cloud Storage: gs://folder/folder/
...
2

There are 2 answers

0
RomanPerekhrest On

The solution using simple list comprehension and str.split() function:

err_mark = '- ERROR -'
with open('error.log', 'r') as fh:
    err_log = [l.split(err_mark)[1] for l in fh.readlines() if err_mark in l]

print(err_log)

The output:

[' Failed: Waiting for files the Files from Cloud Storage: gs://folder/folder/']
0
Iarwa1n On
import re
lines = [line for line in in_.splitlines() if "ERROR" in line]
x = [re.sub(".+ERROR - ",'', line ) for line in lines]
print(x)