How to get coloured Output on the Log file in java?

5k Views Asked by At

I want to write a message in red colour on the log file in java using logger.debug method,is there any way to do it?

3

There are 3 best solutions below

0
On

Logs don't support styled text! The software used to display them might use some cleverness to color different parts differently, but that is done purely in the software.

0
On

You could look for a plugin to your IDE if you want colorized logs.

0
On

Logging itself is just plain text with no means of displaying a message in a particular colour (think of it as a txt file). So if you really want colour in your logs you need to encode that as formatting information which can later be interpreted by a text reader, log viewer or web browser. Probably the easiest would be to use HTML for this. This could for example look like this:

<span class="error">02.01.13 14:23 Something bad happend</span>
<span class="info">02.01.13 14:24 This is just an info message</span>

and additionally providing a CSS file containing the styling information.

.info{ color: #000000; }
.error{ color: #FF0000; font-weight:bold;}