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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Two Engines, One Query: DuckDB Inside Neo4j
  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • Wasm Inside Neo4j: Building the Example That Didn't Exist
  • OpenAPI From Code With Spring and Java: A Recipe for Your CI

Trending

  • Why Kotlin or Java Is the Wrong Question For Enterprises
  • Part 1: Building Governed MCP Tool Services With Quarkus LangChain4j and Goose
  • Moving 900+ GB of Prometheus Without a Cutover Weekend
  • Designing Frontend Applications for Failure, Not Just Success

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Sep. 28, 11 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
264.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Two Engines, One Query: DuckDB Inside Neo4j
  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • Wasm Inside Neo4j: Building the Example That Didn't Exist
  • OpenAPI From Code With Spring and Java: A Recipe for Your CI

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook