简体   繁体   English

Python日志python解释器输出

[英]Python log python interpreter output

I tried to build a logger that also loggs the full output from the python interpreter, what you normally see on the console. 我尝试构建一个记录器,该记录器还记录了您通常在控制台上看到的python解释器的全部输出。

This is my suggestion, it did not work. 这是我的建议,没有用。

logger = logging.getLogger()
stdoutStreamHandler = logging.StreamHandler(sys.stdout)
stderrStreamHandler = logging.StreamHandler(sys.stderr)
fileHandler = logging.FileHandler(logpath)
logger.addHandler(stdoutStreamHandler)
logger.addHandler(stderrStreamHandler)

Unsure what you mean by logging the full output from the Python Interpreter, do you mean the command-line interpreter or the background interpreter at run-time? 不确定通过记录Python解释器的完整输出是什么意思,是在运行时是命令行解释器还是后台解释器?

Either way this may help: trace 无论哪种方式都可以帮助: 跟踪

This will allow you to execute any code with the trace, and will also allow you to write it out to a file: 这将允许您使用跟踪执行任何代码,也可以将其写出到文件中:

 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 the current directory r = tracer.results() r.write_results(show_missing=True, coverdir=".") 

I have not yet used it myself, but it could be possible to execute 我尚未亲自使用过,但可以执行

logger.debug(r)

Which would create a record in your log file of the debug log level, you could, of course, change that to info, warning, error or critical. 这将在调试日志级别的日志文件中创建一条记录,您当然可以将其更改为信息,警告,错误或严重。

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

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