简体   繁体   English

Python 正则表达式在日志文件中查找异常

[英]Python Regular Expression to find exception in log file

I am writing a script to parse the logs.我正在编写一个脚本来解析日志。 I need a regular expression to find the python exception in the log file, if any.我需要一个正则表达式来查找日志文件中的 python 异常(如果有)。

For example if below exception is in the log file, it should return below exception in string format.例如,如果日志文件中有以下异常,则应以字符串格式返回以下异常。

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'klj' is not defined

I tried below code, it won't work:我试过下面的代码,它不会工作:

import urllib.request
import re 

txt = urllib.request.urlopen(log_url).read().decode('utf-8')
exceptions = re.findall("(\w+)Error:", txt)

Thanks in Advance提前致谢

Your regex was only looking for exception names that ended in 'Error', but many end in 'Exception'.您的正则表达式只寻找以“错误”结尾的异常名称,但许多以“异常”结尾。 And you were capturing in capture group 1 only the characters that preceded 'Error', so you would not get the full name.而且您在捕获组 1 中仅捕获“错误”之前的字符,因此您不会获得全名。

The following regex will look only for exception names at the start of a line and capture the full name:以下正则表达式将仅在行首查找异常名称并捕获全名:

import re

txt = """Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'klj' is not defined"""

exceptions = re.findall("""(?x) # Verbose mode
    (?m)                # Multiline mode (^ matches start of a line)
    ^                   # Match start of a line
    (?:\w+)             # Match one or more word characters in a non-capturing group
    (?:Error|Exception) # Match 'Error' or 'Exception':
    (?=: )              # Lookahead assertion: next characters are ': '
    """, txt)
print(exceptions)

Prints:印刷:

['NameError']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM