用另一种颜色显示System.out.println输出 [英] Show System.out.println output with another color

查看:646
本文介绍了用另一种颜色显示System.out.println输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的项目进行调试,我想知道是否有可以用来更改eclipse的输出中的 System.out.println 方法/ p>

例如:

  System.out.println(我想要这是红色); 
System.out.println(我希望这是蓝色);
System.out.println(我希望这是黄色);
System.out.println(我希望这是洋红色);

更多的可读性。



编辑



与sysout我有这个



与syserr我有这个

解决方案

在Eclipse中,最简单的方法是使用 System.err.println 为您想要红色的行 - 我认为这是默认值。 (您可以在首选项 - >运行/调试 - >控制台)中更改。



实际运行时,差异不会显示>控制台当然,但我不认为 Eclipse控制台支持ANSI颜色转义序列等。



编辑:对于Windows控制台,我希望 ANSI转义序列工作。它不是非常便于携带,但如果这不是问题,您可以创建一个类来适当地封装转义序列,因此可以调用以下内容:

  ansiConsole.printRed(sample line in red); 
ansiConsole.printBlue(sample line in blue);

(我可能会使这些方法返回到每次调用后的当前颜色。)



编辑:如评论中所述, Jansi <一个>库已经存在,所以你也可以使用它。它没有上述方法,但我确定它仍然会做你想要的...


I have a big project to debug, and I was wondering if there is anyway I could use to change the System.out.println method in the output of eclipse

for example :

System.out.println("I want this to be red");
System.out.println("I want this to be blue");
System.out.println("I want this to be yellow");
System.out.println("I want this to be magenta");

for more readability.

EDIT

with sysout I have this

with syserr I have this

解决方案

Within Eclipse, the simplest approach would be to use System.err.println for lines you want to be in red - I believe that's the default. (You can change it in Preferences -> Run/Debug -> Console).

That difference won't show up when running in a real console of course, but I don't think the Eclipse console supports ANSI colour escape sequences etc.

EDIT: For the Windows console, I'd expect ANSI escape sequences to work. It's not hugely portable, but if that's not a problem, you could just create a class to encapsulate the escape sequences appropriately, so you could call something like:

ansiConsole.printRed("sample line in red");
ansiConsole.printBlue("sample line in blue");

(I'd probably make those methods return back to whatever the "current" colour was after each call.)

EDIT: As noted in comments, the Jansi library already exists, so you might as well use that. It doesn't have the methods described above, but I'm sure it'll still do what you want...

这篇关于用另一种颜色显示System.out.println输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆