Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j 2 adding multiple colors to console appender

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,

  1. I am new to log4j, is this the right way to write xml config file?

  2. 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 and bold

like image 998
Govinnage Rasika Perera Avatar asked Feb 24 '14 05:02

Govinnage Rasika Perera


People also ask

What is console Appender in log4j?

Log4j provides Appender objects which are primarily responsible for printing logging messages to different destinations such as console, files, NT event logs, Swing components, JMS, remote UNIX syslog daemons, sockets, etc.

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.

How does log4j Appender work?

In the log4j2 architecture, an appender is basically responsible for sending log messages to a certain output destination. Here are some of the most useful types of appenders that the library provides: ConsoleAppender – logs messages to the System console. FileAppender – writes log messages to a file.


1 Answers

I think I found the solution. I downloaded log4j2-core-sources.jar and traced the source. You can write it as below;

<Console name="SlowConsole" target="SYSTEM_OUT">      <PatternLayout disableAnsi="false"  pattern="%highlight{%d{HH:mm:ss.SSS} %-5level %logger{36}.%M() @%L - %msg%n}{FATAL=red blink, ERROR=red, WARN=yellow bold, INFO=black, DEBUG=green bold, TRACE=blue}"/> </Console> 

I think log4j2 documentation and its examples may need to be updated.

like image 83
Govinnage Rasika Perera Avatar answered Oct 05 '22 05:10

Govinnage Rasika Perera