Configure “Slf4j” log with Maven

Pubblicato da lentux-informatica il

  •  
  •  
  •  
  •  
  •  

In this article we see solution for Slf4j config.

The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for various logging frameworks (e.g. java.util.logging, logback, log4j) allowing the end user to plug in the desired logging framework at deployment time.

The underlying logging backend is determined at runtime by adding the desired binding to the classpath and may be the standard Sun Java logging package java.util.logging, log4j, logback or tinylog.

Separation of the client API from the logging backend reduces the coupling between an application and any particular logging framework.

This can make it easier to integrate with existing or third-party code or to deliver code into other projects that have already made a choice of logging backend.

This allows for different logging frameworks to coexist. It also helps migrate from one framework to another.

Finally, apart from standardized API, it also offers some “syntactic sugar”.

This article will discuss the dependencies and configuration needed to integrate SLF4J with Log4j2, Logback, Log4J2 and Jakarta Commons Logging.

More about each of this implementations you can read in article Introduction to Java Logging.

To set Slf4j inside you application maven import these dependencies:




        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>com.springsource.slf4j.api</artifactId>
            <version>1.5.10</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>com.springsource.slf4j.org.apache.commons.logging</artifactId>
            <version>1.5.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>com.springsource.org.apache.commons.logging</artifactId>
            <version>99.0-does-not-exist</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>com.springsource.ch.qos.logback.classic</artifactId>
            <version>0.9.15</version>
        </dependency>

In this case you have a default log for your application…

Configuration

If you want to change the settings you need to create a log file named logback.xml” under src/main/resource like this: 

<configuration>

<appender name=”CONSOLE” class=”ch.qos.logback.core.ConsoleAppender”>
<layout class=”ch.qos.logback.classic.PatternLayout”>
<Pattern>[%-5p] %d{HH:mm:ss} [%c] – %msg%n</Pattern>
</layout>
</appender>

<appender name=”FILE” class=”ch.qos.logback.core.FileAppender”>
<file>C:apache-tomcat-6.0.26apache-tomcat-6.0.26logstestFile.log</file>
<append>true</append>
<layout class=”ch.qos.logback.classic.PatternLayout”>
<pattern>[%-5p] %d{HH:mm:ss} [%c] – %msg%n</pattern>
</layout>
</appender>

<root>
<level value=”DEBUG” />
<appender-ref ref=”FILE”/>
<appender-ref ref=”CONSOLE” />
</root>

</configuration>

Look here to see the variables inside tag “Pattern”.
You can set output file, the level of your log etc etc…
Redeploy your application and work it….
     


  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •