用Python登录的方法

mel*_*ort 4 python logging

我想要一些相当于的东西

calling method: $METHOD_NAME
args:           $ARGS
output:         $OUTPUT
Run Code Online (Sandbox Code Playgroud)

logging为每个(用户定义的)方法调用自动记录到文件(可能通过模块).我能想到的最好的解决方案是编写一个装饰器来执行此操作,然后将其添加到每个函数中.有没有更好的办法?

谢谢

mik*_*iku 6

您可以查看标准库中的跟踪模块

允许您跟踪程序执行,生成带注释的语句覆盖列表,打印调用者/被调用者关系以及在程序运行期间执行的列表函数.它可以在其他程序中使用,也可以在命令行中使用.

您还可以登录磁盘:

import sys
import trace

# create a Trace object, telling it what to ignore, and whether to
# do tracing or line-counting or both.
tracer = trace.Trace(
    ignoredirs=[sys.prefix, sys.exec_prefix],
    trace=0,
    count=1)

# run the new command using the given tracer
tracer.run('main()')

# make a report, placing output in /tmp
r = tracer.results()
r.write_results(show_missing=True, coverdir="/tmp")
Run Code Online (Sandbox Code Playgroud)