提取日志记录语句前后的源代码行



我正试图使用astor和python AST提取日志记录语句前后的源代码行,这要归功于我之前的StackOverflow问题的答案:

import ast
import astor
import json
class LogPrinter(astor.TreeWalk):
def pre_Call(self):
if (isinstance(self.cur_node.func, ast.Attribute) and
isinstance(self.cur_node.func.value, ast.Name) and
(self.cur_node.func.value.id.lower() == "logging" or self.cur_node.func.value.id.lower() == "log" or self.cur_node.func.value.id.lower() == "logger")
):
print(astor.to_source(self.cur_node))

def post_Call(self):
if (isinstance(self.cur_node.func, ast.Attribute)):
print(astor.to_source(self.cur_node))

我可以从上面的代码中获得日志记录语句,但我无法获得完整的前面和下面的语句:

示例:

def db_contecton(string url):
if URL:
#multiline logger
logger.debug(
"connecting to 
URL : " +url)
#multiline source code
db = db
.conn.
init()
db.connect(url)
#multiline logger
logger.
info("connection 
to DB 
successful")

我试图得到的输出是:1(

if url:
logger.debug("connecting to URL : " +url)
db = db.conn.init()

以及2(

db = db.conn.init()
db.connect(url)
logger.info("connection to DB successful")

注:多行记录器和源代码转换为单行

在这一点上,我可以提取记录器并获得以下没有完整源代码的语句:我当前的输出是

#the statement above logger is missing
logger.debug("connecting to URL : " +url) 
db.conn.init() #the db variable is missing should be db = db.conn.init()

以上只是python源代码的示例片段,可能在逻辑上不正确。

我不确定我是否理解你的问题:你想将python文件解析为文本,并提取上面的1行和下面的1行,只要它有一些日志相关的代码?

您可以将文件读取为文本,将其分成行,与您想要的单词匹配(例如:logger,logging,log(,保留这些行的索引,然后对其执行任何操作:

log_words = 'logger logging log'.split()
python_files = ['file1.py', 'file2.py']
keep = dict()
for f in python_files:
keep[f] = []
with open(f, 'r') as f:
lines = f.readlines()
for i in range(len(lines)):
for w in log_words:
if w in lines[i]:
keep[f].append(i - 1) # previous line
keep[f].append(i)     # actual line
keep[f].append(i + 1) # next line

它检查每一行中每个给定文件的每个所需单词。不过这不是表演性的。但我相信它能解决你的问题。

最新更新