简体   繁体   English

通过Pygments重定向python回溯

[英]Redirect python tracebacks through Pygments

My application is invoked from run.py . 我的应用程序是从run.py调用的。 I've installed Pygments and I'd like to redirect Python's output so that as tracebacks occur, they're formatted in an easier to read format. 我已经安装了Pygments,我想重定向Python的输出,以便在发生回溯时,它们以更易于阅读的格式进行格式化。

This is the command that I've tried, python run.py 2>&1 | pygmentize -l py 这是我试过的命令, python run.py 2>&1 | pygmentize -l py python run.py 2>&1 | pygmentize -l py

Redirecting both stdout and stderr through pygmentize. 通过pygmentize重定向stdoutstderr The pygmentize documentation says that it will read from stdin if no file is provided, "If no input file is given, use stdin, if -o is not given, use stdout." pygmentize文档说如果没有提供文件,它将从stdin读取,“如果没有给出输入文件,使用stdin,如果没有给出-o,请使用stdout。”

However, when I redirect like this, no output--errors or log statements or anything else--is output to my terminal. 但是,当我像这样重定向时,没有输出 - 错误或日志语句或其他任何东西 - 输出到我的终端。

When I run my command without piping its output into pygmentize, ie python run.py 2>&1 I get this output: 当我运行我的命令而不将其输出汇总到pygmentize,即python run.py 2>&1我得到这个输出:

INFO:werkzeug: * Running on http://0.0.0.0:5000/
INFO:werkzeug: * Restarting with reloader

Any suggestions? 有什么建议么?

pygmentize reads the whole input stream when reading from stdin and only outputs everything if the input stream is closed, so you only will get any output piped through pygments after your first process has ended. pygmentize在从stdin读取时读取整个输入流 ,并且仅在输入流关闭时输出所有内容,因此在第一个进程结束后,您将只通过pygments获取任何输出。

I'm afraid there's little you can do about that. 我担心你无能为力。 If you want to format a continuous stream, You could only try to write your own wrapper around pygments wich reads the input stream in chunks and feeds that to pygments. 如果你想格式化连续流,你只能尝试围绕pygments编写自己的包装器,它以块的形式读取输入流并将其提供给pygments。

The reason why you don't see any output at all now is that you're probably using Ctrl+C to stop your process in the shell, but that kills both processes. 您现在根本没有看到任何输出的原因是您可能正在使用Ctrl + C来停止shell中的进程,但这会杀死这两个进程。 If you use kill to only terminate the server process, then you should see at least the lines you mentioned above going through pygments. 如果你使用kill来终止服务器进程,那么你至少应该看到上面提到的那些行经过pygments。

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

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