Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print logs in color using log4j2 highlight pattern?

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.

like image 968
madhu Avatar asked Feb 19 '15 10:02

madhu


People also ask

What is pattern layout in Log4j2?

The PatternLayout class extends the abstract org. apache. log4j. Layout class and overrides the format() method to structure the logging information according to a supplied pattern.

What are Appenders in logging?

The appender is the part of a logging system that's responsible for sending the log messages to some destination or medium. It answers the question "where do you want to store this stuff?"

What is configuration status in Log4j2?

Configuration: the root element of a log4j2 configuration file; the status attribute represents the level at which internal log4j events should be logged. Appenders: this element contains a list of appenders; in our example, an appender corresponding to the System console is defined.


1 Answers

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

like image 192
jhas Avatar answered Nov 15 '22 06:11

jhas