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

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

问题描述

我有一个大项目要调试,我想知道是否有任何方法可以用来更改 eclipse 输出中的 System.out.println 方法

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

例如:

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");

为了提高可读性.

编辑

使用 sysout 我有这个

with sysout I have this

使用 syserr 我有这个

with syserr I have this

推荐答案

在 Eclipse 中,最简单的方法是将 System.err.println 用于您想要显示为红色的行 - 我相信这是默认设置.(您可以在首选项 -> 运行/调试 -> 控制台中更改它).

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).

当然,在真实控制台中运行时不会出现这种差异,但我不认为Eclipse控制台支持ANSI颜色转义序列等.

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.

对于 Windows 控制台,我希望 ANSI 转义序列能够工作.它不是非常可移植的,但如果这不是问题,您可以创建一个类来适当地封装转义序列,因此您可以调用类似的东西:

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.)

如评论中所述,Jansi 库已经存在,因此您不妨使用它.它没有上面描述的方法,但我相信它仍然会做你想做的......

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天全站免登陆