DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Build a New API Quickly Using Spring Boot and Maven
  • Configurable Feign Client Retry With Reusable Library and DRY
  • Using Lombok Library With JDK 23
  • Enhancing Software Quality with Checkstyle and PMD: A Practical Guide

Trending

  • A Modern Stack for Building Scalable Systems
  • Solid Testing Strategies for Salesforce Releases
  • Streamlining Event Data in Event-Driven Ansible
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  1. DZone
  2. Coding
  3. Java
  4. Adding SLF4J to Your Maven Project

Adding SLF4J to Your Maven Project

Learn how to add SLF4J, or Simple Logging Facade for Java, to your Maven project in this tutorial.

By 
Roger Hughes user avatar
Roger Hughes
·
Sep. 28, 11 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
260.7K Views

Join the DZone community and get the full member experience.

Join For Free

In yesterday’s blog, I documented the various bits of the Maven POM file created for a ‘Spring MVC Project’ using one of Spring’s project templates. In that POM file, you may have noticed a reference to an slf4j-api artefact and comment to the effect that the Guy’s at Spring were excluding “Commons Logging in favor of SLF4j”. This blog takes a look at SLF4j, or to give it its full title: “Simple Logging Facade for Java” and demonstrates how to add it to a project.

SLF4j is not a logger in itself, it’s a facade or wrapper that delegates the actual business of logging to one of the more well-known logger implementations, which is usually Log4J. The idea behind it is that’s its a replacement for that other well-known logging facade: Commons Logging. The reason that it has been written as an alternative to Commons Logging, is that Commons Logging loads your logging library by using some whizzy Java ClassLoader techniques. Unfortunately, this has gained it a reputation for being somewhat buggy (although it has to be said I’ve never come across any problems with Commons Logging).

SLF4j, on the other hand, does things more simply; there’s no whizzy ClassLoader, you simply specify the slf4j API library, the one you use in your application, plus another SLF4j library that binds the first library to the logger implementation you’re using such as Log4J. The shortlist of binding libraries that SLF4j supports can be found here.

To add SLF4j to your project, the first thing to is to add in the SLF4j API

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


...which is pretty simple. The thing to note is that this must be in compile scope. The next step is to choose a binding library; one of the following:


<!-- CHOOSE BETWEEN ONE OF THESE DIFFERENT BINDINGS -->
<!-- Binding for NOP, silently discarding all logging. -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>${slf4jVersion}</version>
</dependency>
<!-- Binding for System.out -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4jVersion}</version>
</dependency>
<!--Binding for log4j version 1.2.x You also need to 
place log4j.jar on your class path. -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4jVersion}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>runtime</scope>
</dependency>
<!--Binding for commons logging over slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4jVersion}</version>
<scope>runtime</scope>
</dependency>


...and note that the scope here can be runtime.


If you add more than one binding JAR to your project config, then you'll get the following error message:


SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/Roger/.m2/repository/org/slf4j/slf4j-nop/1.6.1/slf4j-nop-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/Roger/.m2/repository/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.


...and alternatively, if you forget to add any bindings, the following message is written to Standard Err and all log messages are sent to nop (no/null output):


SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.


The following code demonstrates how to use SLF4j in an application, as you can see the fatal logging level has not been implemented.


public static void main(String[] args) {

    Logger logger = LoggerFactory.getLogger(Slf4jHelloWorld.class);

    logger.trace("Hello World");
    logger.debug("Hello World");
    logger.info("Hello World");
    logger.warn("Hello World");
    logger.error("Hello World");
  }


SLF4j is well documented and you can find all the manuals and what-not on their site.


Finally, at the time of writing, the lastest version of SLF4j is 1.6.2; however, this only seems to be available from the SLF4j website. The lastest version available from Maven Central is 1.6.1.

 

From http://www.captaindebug.com/2011/09/adding-slf4j-to-your-maven-project.html

Log4j Apache Maven

Opinions expressed by DZone contributors are their own.

Related

  • How to Build a New API Quickly Using Spring Boot and Maven
  • Configurable Feign Client Retry With Reusable Library and DRY
  • Using Lombok Library With JDK 23
  • Enhancing Software Quality with Checkstyle and PMD: A Practical Guide

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!