1

Is there any way in java to print the entire exception message (including all stack-trace) in different color (for example in red) . The reason being, i would like to showcase the fatal error/exception in some different way. If the exception message also prints in same way as the general log prints , then it's difficult to trace the actual exception compare to log.

Please suggest on how to do that. Is there any different way , then please suggest

Regards

user2315104
  • 2,378
  • 7
  • 35
  • 54
  • I assume all the log messages go into a log file, and then you tail/grep the log. Is that the case? If it is, then depending on your text editor of choice, you can create a syntax file that would do the highlighting. I use vim, and I created a syntax file just for the purpose of highlighting tomcat logs. – Chikipowpow Mar 26 '16 at 13:56
  • Possible duplicate of [How to print color in console using System.out.println?](http://stackoverflow.com/questions/5762491/how-to-print-color-in-console-using-system-out-println) – Siguza Mar 26 '16 at 14:18
  • You may simply redirect exception/error logs to different output stream. –  Mar 26 '16 at 14:28

2 Answers2

2

The recommended practice is to print error messages to System.err, and normal output to System.out. Modern terminal applications are usually configured to display text coming from stderr in red color, distinguishing from normal output.

If that is not good enough for you (but I hope it is), then another alternative as suggested by @Siguza is to use ANSI colors, as demonstrated in this other answer.

Community
  • 1
  • 1
janos
  • 120,954
  • 29
  • 226
  • 236
  • 1
    [ANSI colors](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors) are a thing though... – Siguza Mar 26 '16 at 14:19
0

For consoles that don't automatically print stderr in red, you can make a java class that extends output stream to add the ANSI color codes. Making a seperate output stream class and then setting the output with System.setErr(new PrintStream(new ColoredOutputStream(System.err))) makes it so that even if you call something like e.printStackTrace it will still be red. for example this is what I use:



    public class ColoredOutputStream extends OutputStream 
    {
        OutputStream errStream;
        red = "\033[31m";
        reset = "\033[0m";   
        public ColoredErrorStream(OutputStream outputStream){
            errStream = outputStream;
        }
            @Override
        public void write(int b) throws IOException
        {
            errStream.write(red.getBytes());
            errStream.write(b);     
            errStream.write(reset.getBytes());
        }
        
        @Override
        public void write(byte[] b) throws IOException
        {
            errStream.write(red.getBytes());
            errStream.write(b);
            errStream.write(reset.getBytes());
        }
    
        @Override
        public void write(byte[] b, int off, int len) throws IOException
        {
            errStream.write(red.getBytes());
            errStream.write(b, off, len);
            errStream.write(reset.getBytes());
                
        }
    
        @Override
        public void flush() throws IOException
        {
                errStream.flush();
        }
    
        @Override
        public void close() throws IOException
        {
                errStream.close();
        }
    }


oname
  • 16
  • 2