Hi I just downloaded and configured log4j-2. I am stuck on applying color codes to the SlowConsole
console appender. My console appender is like below.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n"/>
</Console>
<Console name="SlowConsole" 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>
<File name="File" fileName="C:\log\out.txt">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<logger name="org.abc.ea.web" level="ALL" additivity="false">
<!--Log4j for the WEB MODULE -->
<appender-ref ref="SlowConsole"/>
</logger>
<logger name="org.abc.ea.ejb" level="ALL" additivity="false">
<!--Log4j for the EJB MODULE -->
<appender-ref ref="SlowConsole"/>
</logger>
<Root level="ERROR">
<AppenderRef ref="Console"/>
<AppenderRef ref="File" />
</Root>
</Loggers>
</Configuration>
I have two questions,
I am new to log4j, is this the right way to write xml config file?
How can i add two color codes to each log level?
for example: DEBUG=green -> will output light green font, But i need it to be
dim
andbold
For a log4j2 colored output that looks very close to Spring Boot's default logback console output:
When using the eclipse console, its nice to see errors in red and all other logs in black. You can do this using Filters:
This worked in my Spring Boot application.
But I am not sure if it will work without Spring Boot.
1 : yes it's ok! and you can add some other options. see http://logging.apache.org/log4j/2.x/manual/configuration.html
2 : you can use a HTMLLayout for your file appender.
see http://logging.apache.org/log4j/2.x/manual/layouts.html
and http://www.tutorialspoint.com/log4j/log4j_htmllayout.htm
I think I found the solution. I downloaded log4j2-core-sources.jar and traced the source. You can write it as below;
I think log4j2 documentation and its examples may need to be updated.