Buscar este blog

viernes, 29 de abril de 2016

Maven - Log4j2 with slf4j

How to use log4j2 logging over the simple log facade (slf4j).

pom.xml
<dependencies>

  <dependency>
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-slf4j-impl</artifactId>
   <version>2.5</version>
  </dependency>



  <dependency>
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-api</artifactId>
   <version>2.5</version>
  </dependency>

  <dependency>
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-core</artifactId>
   <version>2.5</version>
  </dependency>
  
 </dependencies>


You need to configure de log4j2 impl, for example, by using a log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
 <Appenders>
  <Console name="CONSOLE" target="SYSTEM_OUT">
   <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
  </Console>

  <RollingFile name="FILE" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz" ignoreExceptions="false">
   <PatternLayout>
    <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
   </PatternLayout>
   <TimeBasedTriggeringPolicy />
  </RollingFile>
 </Appenders>
 
 
 <Loggers>
  
  <Logger name="com.foo.Bar" level="trace" additivity="false">
      <AppenderRef ref="CONSOLE" />
   <AppenderRef ref="FILE" />
    </Logger>
 
  <Root level="INFO">
   <AppenderRef ref="CONSOLE" />
   <AppenderRef ref="FILE" />
  </Root>
 </Loggers>
</Configuration>

1 comentario: