5

I am new for log4j2.I want to print logs in different colors for different levels.I am using eclipse ide for development java with log4j2 for application logging. This is my log4j2.xml file

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
   <Console name="STDOUT" target="SYSTEM_OUT">
     <PatternLayout pattern="%highlight{%d{HH:mm:ss.SSS} %-5level %logger{36}.%M() @%L - %msg%n}{FATAL=red, ERROR=red, WARN=yellow, INFO=black, DEBUG=green, TRACE=blue}"/>
   </Console>
  </Appenders>
   <loggers>
  <Logger name="org.apache.log4j.xml" level="all"/>
    <root level="all">
      <appender-ref ref="STDOUT"/>
    </root>
  </loggers>
</Configuration>

when i am execute the log4j2 example I am getting below result in eclipse console.

[32m15:56:30.536 DEBUG com.syn.test.Test.main() @15 - this is debug message
[m[32m15:56:30.539 DEBUG com.syn.test.Test.main() @19 - this is debug messge 
[m

now my question is how to print logs in different colors for different levels in eclipse console.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
madhu
  • 1,083
  • 3
  • 12
  • 31

3 Answers3

5

The ANSI Console plugin from Eclipse market place does the job. It works great and you can see the logs in colors both on Windows & Unix

jhas
  • 587
  • 6
  • 8
3

The highlight and color syntax works on most Unix and Mac terminals but apparently not in the Windows DOS console (according to this).

Unfortunately, I very much doubt that the Eclipse console supports highlighting and colors with ANSI escape codes.

UPDATE: Windows color support is possible with Jansi.

BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
0

I had success in IntelliJ IDEA using jcabi-log.

Maven dependency:

<dependency>
  <groupId>com.jcabi</groupId>
  <artifactId>jcabi-log</artifactId>
  <version>LATEST</version>
</dependency>

Example PatternLayout:

<PatternLayout pattern="%d{HH:mm:ss.SSS} [%highlight{%level}{FATAL=bg_red, ERROR=red, WARN=yellow, INFO=green, DEBUG=blue}] [%t]: %logger{36}.%M@L%L - %msg%n" disableAnsi="false"/>

It's important to retain the disableAnsi="false" property to make the colors work.

BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185