Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce number of logging libraries in project

Tags:

java

logging

Project, I am working on, is using several open source frameworks. Each framework uses some logging library. My problem is that final deliverable contains log4j (in two different versions), slf4j with logback and of course common logging. I am looking for solution how to reduce number of libraries, can I exclude logging libraries in maven's pom file and configure slf4j to 'intercept' logging messages?

like image 632
David Warsow Avatar asked May 09 '12 12:05

David Warsow


2 Answers

Slf4j has some information in its manual about how to do some of this.

I think what you are looking for is to exclude log4j and commons-logging from all of your maven dependencies and load the log4j-over-slf4j and the jcl-over-slf4j packages instead. They are designed to replace the log4j and commons-logging and contains the appropriate org.apache.log4j and org.apache.commons.logging classes to replace Logger, Log, and friends. This won't cut down on the libraries but it will put all of the log output through sl4fj. Unless you get rid of the usage of these classes (obviously) you can't reduce the dependency on either the slf4j wrapper or the original.

If you are instead looking to use one logging package then the right way to do it might instead to switch to use commons-logging which is designed as a logging delegator and then use the slf4j-jcl package which hooks into commons-logging.

For posterity, you exclude the dependency this way for each of the dependencies that requires log4j.

<dependency>
    <groupId>.../groupId>
    <artifactId>...</artifactId>
    <version>...</version>
    <exclusions>
        <exclusion>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>
like image 166
Gray Avatar answered Sep 30 '22 15:09

Gray


Check this out

EDIT read your question a bit more closely. slf4j adds a legacy bridge for non slf4j projects. But you will still need to supply the dependent jars on your runtime path. Its just the cost of not repeating someone else's work.

I mean think about it you have all those fully qualified symbols that essentially need linked in your jvm. Nothing short of some straight voodoo is going to get you around including the dependent libraries.

like image 26
nsfyn55 Avatar answered Sep 30 '22 15:09

nsfyn55