Show System.out.println output with another color

后端 未结 4 1463
你的背包
你的背包 2020-12-03 15:17

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 e

相关标签:
4条回答
  • 2020-12-03 16:05

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

    0 讨论(0)
  • 2020-12-03 16:05

    Please have a look at Jansi (Jansi's Github)

    Jansi is a small java library that allows you to use ANSI escape sequences to format your console output which works even on windows.

    0 讨论(0)
  • 2020-12-03 16:08

    Please Refer the following code.Also refer this link for ANSI color codes. http://en.wikipedia.org/wiki/ANSI_escape_code

    public class ColourConsoleDemo {
        public static void main(String[] args) {
            // TODO code application logic here
            System.out.println("\033[0m BLACK");
            System.out.println("\033[31m RED");
            System.out.println("\033[32m GREEN");
            System.out.println("\033[33m YELLOW");
            System.out.println("\033[34m BLUE");
            System.out.println("\033[35m MAGENTA");
            System.out.println("\033[36m CYAN");
            System.out.println("\033[37m WHITE");
        }   
    }
    
    0 讨论(0)
  • 2020-12-03 16:08

    It seems that you want to highlight the output of System.out.println() using different colours in order to help you to debug , why don't redirect all the output of System.out to a file in your program entry point :

    FileOutputStream fis = new FileOutputStream(new File("log.txt"));
    PrintStream out = new PrintStream(fis); 
    System.setOut(out);
    

    Then using some free and portable real-time log file monitoring tool that has configurable highlighting function using different colours , such as BareTail , to view this file.

    0 讨论(0)
提交回复
热议问题