Monday, 18 April 2016

How to configure SLF4j & Logback Logger in Spring+Hibernate applications

Add the below dependencies in the pom.xml

 <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
      </dependency>

 <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>${logback.version}</version>
      </dependency>



if it is a maven project , place the below logback.xml in travel-desk\src\main\resources\. Place the **logback-appenders.xml and **logback-loggers.xml in path specified in {-Dapp.configs=c:\<path>}, try to add {-Dapp.configs=c:\<path>} in vm-arguments of  the application server that  you have configured in Eclipse IDE or whichever IDE you use.

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scanPeriod="60 seconds" scan="true">

<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>

<!-- To enable JMX Management -->
<jmxConfigurator/>
<include file="${app.configs}/travelDesk/travelDesk-logback-appenders.xml"/>
<include file="${app.configs}/travelDesk/travelDesk-logback-loggers.xml"/>
</configuration>


 Try to add {-Dapp.logs=<path>}  in vm-arguments of  the application server that  you have configured in Eclipse IDE or whichever IDE you use. Below is an snippet from **appenders.xml

/travelDesk-logback-appenders.xml

<included>
  <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%date{ISO8601} %-5p [%t][%c] %m%n</pattern>
    </encoder>
  </appender>
 
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${app.logs}/travelDesk/travelDesk.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>${app.logs}/travelDesk/travelDesk.%d{yyyy-MM-dd}.log</fileNamePattern>
      <!-- keep 50 days' worth of history -->
      <maxHistory>50</maxHistory>
    </rollingPolicy>
    <encoder>
      <pattern>%X{NDC0}%X{NDC1}%X{NDC2}%X{NDC3}%date{ISO8601} %-5p [%t][%c] %m%n</pattern>
    </encoder>
  </appender>
</included>


Below are the lines from  **loggers.xml
travelDesk-logback-loggers.xml

<included>
<!-- Loggers -->
<logger name="com.traveldesk" level="debug"/>
<logger name="org.springframework.integration" level="warn"/>

<root level="warn">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</included>










No comments:

Post a Comment