Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SLF4J best practices

I'm writing a library and I use sfl4j to handle logging.

I think it's good idea as long as every body can provide it's own implementation and then, the log provided by my application will be handled correctly.

But I don't know if I have to provide an implementation as a transitive dependency or not.

Exemple:

If I only provide that:

    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.1</version>
        </dependency>
    </dependencies>

The user of my lib can choose the implementation, but if he just add my lib without reading configuration, it will not work

Otherwise, if I provide that:

    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.6.1</version>
        </dependency>
    </dependencies>

My lib will work correctly just when adding dependency to it, but If the user want to use an other slf4j, he will have to exclude mine.

What do you thing about that?

like image 217
Christophe Avatar asked Aug 18 '11 12:08

Christophe


People also ask

What is difference between log4j and SLF4J?

Unlike log4j, SLF4J (Simple Logging Facade for Java) is not an implementation of logging framework, it is an abstraction for all those logging frameworks in Java similar to log4J. Therefore, you cannot compare both. However, it is always difficult to prefer one between the two.

What is the best logger for Java?

One of the most popular solutions for the Java world is the Apache Log4j 2 framework.

Is SLF4J a wrapper of log4j?

SLF4J and Apache commons logging (jcl) )are widely used logging wrappers that will use discovery at run time to check for pluggable frameworks like log4j,log4j2, logback in the classpaths that provide implementations.

How do I improve application logging?

Use structured logging Human readable logs are helpful, but structured logs add value, especially as an application's logs become more complex or as request throughput increases. Structured logging provides a well-defined format for your log entries so you can more easily parse, query, and process them for analytics.


1 Answers

You never provide a log implementation. The client application has to do so. Otherwhise this would be a violation of separation of concerns. Don't do any assumptions about an unknown client.

like image 168
Michael-O Avatar answered Oct 11 '22 13:10

Michael-O