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.
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.
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?"
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With