Java 在 Eclipse 控制台中为日志着色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/233790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:50:04  来源:igfitidea点击:

Colorize logs in eclipse console

javaeclipseconsoleescapingmetadata

提问by Newtopian

Is there a way to colorize parts of logs in the eclipse console. I know I could send to error and standard streams and color them differently but I'm more looking someting in the lines of ANSI escape codes (or anyother, HTML ?) where I could embed the colors in the string to have it colored in the logs.

有没有办法在 Eclipse 控制台中为部分日志着色。我知道我可以发送到错误和标准流并以不同的方式为它们着色,但我更喜欢在 ANSI 转义码(或任何其他 HTML ?)日志。

It sure would help making the important bits stand out without resorting to weird layout, rather keep the layout to the log4j setups

它肯定有助于使重要的部分脱颖而出,而无需诉诸奇怪的布局,而是将布局保持在 log4j 设置中

here is an example of what I am looking for :

这是我正在寻找的一个例子:

[INFO ] The grid is complete ....... false

[INFO]网格已完成.......

where the bold parts would be in blue, this coloring can be controlled by the application to an extent. like so (tags are conceptual and arbitrary, but you get the idea):

粗体部分是蓝色的,这种颜色可以在一定程度上由应用程序控制。像这样(标签是概念性的和任意的,但你明白了):

log.info(String.format("The grid is complete ....... <blue>%s</blue>", isComplete ));

log.info(String.format("网格完成....... <blue>%s </blue>", isComplete ));



On a more general note it is the ability to embed meta information in the logs to help the presentation of these logs. Much like we tag web pages content to help the presentation of the information by CSS.

更一般地说,它能够在日志中嵌入元信息以帮助呈现这些日志。就像我们标记网页内容以帮助通过 CSS 呈现信息一样。

采纳答案by Benjamin Seiller

Have a try with this Eclipse Plugin: Grep Console

试试这个 Eclipse 插件:Grep Console

[Update]:
As pointed out by commenters: When installing Grep Console in the currently last version of Eclipse, you need to uncheck 'Group items by category' in the Install dialog to see the available items.
As pointed out by @Linethe plugin can now be easily installed via the Eclipse Marketplace again without changing any options.

[更新]
正如评论者所指出的:在当前最新版本的 Eclipse 中安装 Grep 控制台时,您需要在安装对话框中取消选中“按类别分组项目”以查看可用项目。
正如@Line所指出的,该插件现在可以通过 Eclipse Marketplace 轻松安装,无需更改任何选项。

[Update 2]:
As pointed out by @azdev, to get proper highlighting:

[更新 2]
正如@azdev所指出的,要获得适当的突出显示:

Entering just literal strings doesn't work. To get a line to be colored, you have to enclose the string in .*on either side, like so: .*ERROR.*

仅输入文字字符串不起作用。要使线条着色,您必须将字符串括 .*在任一侧,如下所示: .*ERROR.*

回答by GHad

You may consider trying Apache Chainsaw (http://logging.apache.org/chainsaw/index.html) if you are already working with log4j. Lets you define colors and filtern and works with (nearly) zero configuration.

如果您已经在使用 log4j,您可以考虑尝试使用 Apache Chainsaw ( http://logging.apache.org/chainsaw/index.html)。允许您定义颜色和过滤器并使用(几乎)零配置。

回答by David Santamaria

What about use Logbackand its property converterand log everything in log4j, that may allow you see the differents levels on differents colors.

如何使用Logback及其属性转换器并将所有内容记录在 log4j 中,这可能会让您看到不同颜色的不同级别。

Good luck!

祝你好运!

EDIT: the eclipseplugin

编辑eclipse插件

回答by Neal Swearer

We use the Ganymede Eclipse plugin where I work, and it works well.

我们在我工作的地方使用 Ganymede Eclipse 插件,它运行良好。

http://sourceforge.net/projects/ganymede/

http://sourceforge.net/projects/ganymede/

"A log4j plugin to Eclipse that works similar to chainsaw (SocketServer). Includes color, filtering, detailed information, and saves settings."

“Eclipse 的 log4j 插件,其工作方式类似于电锯 (SocketServer)。包括颜色、过滤、详细信息和保存设置。”

回答by Neal Swearer

I've used this plugin before, it lets you colourize lines of the log based on customized regex.

我以前用过这个插件,它可以让你根据自定义的正则表达式对日志的行进行着色。

For example, when I was using it, any lines with the words error would be red, warning would be orange, info would be blue... etc.

例如,当我使用它时,任何带有错误字样的行都是红色的,警告是橙色的,信息是蓝色的......等等。

Since it's regex, you could do anything. Set it up to make the line green whenever it starts with ">>>" and then prepend your message string with ">>>".

因为它是正则表达式,所以你可以做任何事情。将其设置为在以“>>>”开头时使该行变为绿色,然后在您的消息字符串前加上“>>>”。

http://sourceforge.net/projects/logfiletools

http://sourceforge.net/projects/logfiletools

回答by thSoft

Read about the org.eclipse.ui.console.consolePatternMatchListenersextension point.

阅读org.eclipse.ui.console.consolePatternMatchListeners扩展点。

回答by Ashutosh Jindal

As already pointed out by @Benjamin Grep Console is a great way to colorize output in the Console.

正如@Benjamin Grep 已经指出的那样,控制台是在控制台中为输出着色的好方法。

I had made a short video to demonstrate how it worked and heard back from the Creator of the Grep Console plugin. He mentioned that Grep console 3 is has been released.

我制作了一个简短的视频来演示它是如何工作的,并从 Grep 控制台插件的创建者那里得到了反馈。他提到Grep 控制台 3 已经发布。

Screen cast: http://www.youtube.com/watch?v=fXjgGZAxToc

屏幕投射http: //www.youtube.com/watch?v=fXjgGZAxToc

Update Sites

更新站点

Grep Console 2
http://eclipse.musgit.com
(requires Eclipse 3.4 (Ganymede) or higher and Java 5.0 or higher)

Grep 控制台 2
http://eclipse.musgit.com
(需要 Eclipse 3.4 (Ganymede) 或更高版本和 Java 5.0 或更高版本)

Grep Console 3
http://eclipse.schedenig.name
(requires Eclipse 3.7 (Indigo) or higher and Java 6.0 or higher)

Grep 控制台 3
http://eclipse.schedenig.name
(需要 Eclipse 3.7 (Indigo) 或更高版本和 Java 6.0 或更高版本)

回答by fmjrey

Actually the ANSI Console pluginadds ANSI escape code support to Eclipse console. At present it does have a limitation though, whereby escape codes that span multiple lines leak incorrectly to other lines when scrolling, see issue #3.

实际上ANSI 控制台插件向 Eclipse 控制台添加了 ANSI 转义码支持。目前它确实有一个限制,即跨多行的转义码在滚动时错误地泄漏到其他行,请参阅问题 #3

Otherwise some terminal view plugin as explained in this other questionmight be an option for some.

否则,在另一个问题中解释的某些终端视图插件可能是某些人的选择。

回答by jajube

You can use ANSI Escape in Console pluginfor Eclipse. You'll need this so the Eclipse console can interpret ANSI Escape codes. Install it and restart Eclipse. Then you can use ANSI Color Codes to write in a certain color. Here's a list of ANSI Color Codesfrom another stackoverflow answer. Then you can do this:

您可以Eclipse 的控制台插件中使用ANSI Escape。您将需要它,以便 Eclipse 控制台可以解释 ANSI 转义码。安装它并重新启动 Eclipse。然后您可以使用 ANSI 颜色代码以某种颜色书写。这是另一个 stackoverflow 答案中的 ANSI 颜色代码列表。然后你可以这样做:

public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_RED = "\u001B[31m";

public static void main(String[] args) {
    System.out.println(ANSI_RED + "This text is red!" + ANSI_RESET);
}

I used this to create a Custom Formatter for the Console Handler so it shows logs from Logger in different levels with different colors (INFO logs in cyan, WARNING logs in yellow and SEVERE logs in red). Here's how I did it, if you're interested.

我用它为控制台处理程序创建了一个自定义格式化程序,因此它以不同的颜色显示来自 Logger 的不同级别的日志(青色的信息日志,黄色的警告日志和红色的严重日志)。如果你有兴趣,这就是我是如何做到的