简体   繁体   English

Eclipse 控制台中的 Pydev 源文件超链接

[英]Pydev source file hyperlinks in Eclipse console

When a python app crashes the console displays hyperlinks to the source code where the exceptions occurred.当 Python 应用程序崩溃时,控制台会显示指向发生异常的源代码的超链接。 You just click the link and your source file is opened in the Eclipse editor.您只需单击该链接,您的源文件就会在 Eclipse 编辑器中打开。

I have my own exception handler and would like to put links to source files in the console when my apps crash.我有自己的异常处理程序,并希望在我的应用程序崩溃时在控制台中放置指向源文件的链接。

I have looked for info on this a few times and cannot see how to do it or if it is possible at all.我已经找了几次关于这方面的信息,但看不到如何做或者是否有可能。

It seems that it can be done in java by writing to the console in the form ':' - A listener picks this up and converts it to a clickable link.似乎可以通过以 ':' 的形式写入控制台在 Java 中完成它 - 一个侦听器选择它并将其转换为可点击的链接。 This does not work in Python.这在 Python 中不起作用。

Any ideas?有任何想法吗?

If you want to put links to be clickable in PyDev, you have to do it in the same way that's presented in Python.如果您想在 PyDev 中放置可点击的链接,您必须按照 Python 中提供的相同方式进行操作。

Ie: You can see from https://github.com/fabioz/Pydev/blob/master/plugins/org.python.pydev.debug/src/org/python/pydev/debug/ui/PythonConsoleLineTracker.java that the pattern it matches is:即:您可以从https://github.com/fabioz/Pydev/blob/master/plugins/org.python.pydev.debug/src/org/python/pydev/debug/ui/PythonConsoleLineTracker.java看到该模式它匹配的是:

.*(File) \\\\\\"([^\\\\\\"]*)\\\\\\", line (\\\\d*).

(note that \\ is the escape in a string on on java). (请注意, \\是 java 上字符串中的转义符)。

import logging, sys, os
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format='%(message)s | \'%(name)s:%(lineno)s\'')
log = logging.getLogger(os.path.basename(__file__))

log.debug("hello logging linked to source")

The above is all you need, below is more extended discussion, includes use of a config file for formatting and a wrapper around getLogger.以上就是你所需要的,下面是更多扩展的讨论,包括使用配置文件进行格式化和围绕 getLogger 的包装器。

Two additional regexes were added Nov 19, 2017, so that a logging formatter in a logging config just needs to include '%(filename)s:%(lineno)s' to create a more concise link. 2017 年 11 月 19 日添加了两个额外的正则表达式,因此日志配置中的日志格式化程序只需要包含'%(filename)s:%(lineno)s'即可创建更简洁的链接。 Example:例子:

[formatter_customFormatter] format=%(asctime)s | %(name)s | %(levelname)s | %(message)s '%(filename)s:%(lineno)s'

Yet this fails when calling imported code from subdirectories, since filename doesn't include any path info.然而,当从子目录调用导入的代码时,这会失败,因为文件名不包含任何路径信息。 Using %(filepath)s works, but at the cost of printing the entire absolute path appended to your log output, the same problem encountered with the previous File regex.使用%(filepath)s有效,但以打印附加到日志输出的整个绝对路径为代价,与之前的 File regex 遇到相同的问题。 But %(name)s usually has almost exactly what you want, parent_dir.filename, because common practice is to pass __name__ to the getLogger call.但是%(name)s通常几乎完全符合您的要求,parent_dir.filename,因为通常的做法是将__name__传递给 getLogger 调用。 What to do?该怎么办?

logging.getLogger(__name__.replace(".","/") + ".py")

You could also write a wrapper around getLogger with same code as in您还可以使用与中相同的代码围绕 getLogger 编写一个包装器

def getLogger(name): return logging.getLogger(name.replace(".","/") + ".py")

and then your call for Logger would be normal然后你对 Logger 的调用就正常了

logger = myLogger.getLogger(__name__).

One caveat is that __name__ is '__main__' where main is run, so you need to treat differently to get link from file.一个警告是 __name__ 是运行 main 的 '__main__',因此您需要区别对待以从文件中获取链接。 One easy way is to just pass my_filename_with_main instead of __name__ to getLogger.一种简单的方法是将 my_filename_with_main 而不是 __name__ 传递给 getLogger。

or perhaps use a different wrapper for everything as in或者可能对所有内容使用不同的包装器,如

def getLogger(name): from os import path name = path.basename(name) return logging.getLogger(name)

and then your call for Logger would be然后您对 Logger 的调用将是

logger = myLogger.getLogger(__file__).

And then no special treatment in case file run as '__main__'然后没有特殊处理,以防文件运行为“__main__”
Here is a format example, name is whatever is passed to getLogger:这是一个格式示例,名称是传递给 getLogger 的任何内容:
format=%(asctime)s | %(levelname)s | %(name)-20s | %(funcName)-10s | %(message)-35s | '%(name)s:%(lineno)s'

The link provided by Fabio, March '17 very helpful, I'll repeat it here, https://github.com/fabioz/Pydev/blob/master/plugins/org.python.pydev.debug/src/org/python/pydev/debug/ui/PythonConsoleLineTracker.java lines 102 - 104 where the regexes can be found. Fabio 于 17 年 3 月提供的链接非常有用,我将在这里重复, https://github.com/fabioz/Pydev/blob/master/plugins/org.python.pydev.debug/src/org/python /pydev/debug/ui/PythonConsoleLineTracker.java第 102 - 104 行,可以在其中找到正则表达式。

Note: PyCharm only liked the single quote version (as given above), Eclipse was not so particular, either worked.注意:PyCharm 只喜欢单引号版本(如上所示),Eclipse 没有那么特别,两者都有效。

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

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