从 python 源代码中提取完整的日志语句

Extracting complete log statements from python source code

能否请您分享一下如何从 python 源代码中提取记录器语句。

例如我有以下片段:

import logger
def some_function(some_list):
    #multi-line logger
    logger.info("Inside 
    some function")
    for item in some_list:
        logger.debug("item is {}}.format(item)

输出应该是:

logger.info("Inside some function") #converted to single line 
logger.debug("item is {}}.format(item)

我在 python 中使用了 AST 模块,它为我提供了具有记录器变量的源代码行

#tree is a AST parse tree of python source file
for node in ast.walk(tree):
    #previous = node.parent
    try:
        for child in ast.iter_child_nodes(node):
            #print("The length of child nodes is {}".format(len(ast.iter_child_nodes(node))))
            if  child.id == "logger":
                print("The start line no is {} and end line no is {}".format(child.lineno,child.end_lineno))
    except AttributeError:
        pass

下面是 AST 脚本的输出:

The start line no is 150 and end line no is 150
logger = logging.getLogger(__name__)

The start line no is 226 and end line no is 226
logger.info

The start line no is 232 and end line no is 232
logger.info

The start line no is 294 and end line no is 294
logger.info

The start line no is 300 and end line no is 300
logger.info

The start line no is 303 and end line no is 303
logger.info

如您所见,我无法检索整个记录器源代码行,非常感谢任何帮助,谢谢!

astor module 使这很容易做到:

import ast
import astor

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 == "logger"
           ):
            print(astor.to_source(self.cur_node), end="")

你可以用 LogPrinter(ast.parse(some_source_code)).

这样的名字来称呼它

它的工作原理是在 ast 树中查找任何 ast.Call 节点,然后检查是否正在对名为 logger 的属性进行调用。如果你愿意,你可以用其他逻辑代替,这只是一个快速而肮脏的版本,适用于你的特定示例代码(如果它的语法错误已修复):

code_text = """import logger
def some_function(some_list):
    #multi-line logger
    logger.info(
        "Inside some function"
    )
    for item in some_list:
        logger.debug("item is {}".format(item))
"""

LogPrinter(ast.parse(code_text))

输出为:

logger.info('Inside some function')
logger.debug('item is {}'.format(item))