1
votes

At the moment, I'm working with

  • log4j 2.7;
  • springframework 4.3.5;
  • hibernate 5.2.3.

I configure log4j via an xml file.
To do this, I created some appenders, one of which is named "General". What I need is that all logs must go to that appender (included the ones generated by springframework or hibernate) and that none of the latter are printed on the console (I still need for other log from other classes).
I tried writing these Loggers:

<Loggers>
    <Logger name="org" level="ALL" />
    <Root level="ALL" additivity="false">
        <AppenderRef ref="General"/>
    </Root>
    <Logger name="tests" level="ALL" additivity="false">
        <AppenderRef ref="console"/>
        <AppenderRef ref="General"/>
    </Logger>
</Loggers>

The point is that while all the packages but springframework are correctly logging via that appender, springframework still logs on console and does not write any line via the "General" appender. How can I fix that?

EDIT: I found out that it can be due to the fact that springframework seems to log using other libraries (logback, slf4j and similar). I also read that springboot can be configured to use log4j and, since I still don't know much about Spring and its libraries, I don't know how to get the result I want.

2
I'm not sure log4j XML elements are capitalized (eg logger, not Logger) , also there is a dash in <appender-ref>Fabien Benoit-Koch
@fabienbk, under that point of view, all seems to work properly, as you can see here (logging.apache.org/log4j/2.x/manual/appenders.html). The point is that only one library doesn't seem to see my configurations, while another one does.SamCle88
Oh, my bad! it's log4j 2 indeed. What log statement did you observe from spring, in the console ?Fabien Benoit-Koch
@fabienbk, at the moment I see all what springframework log, things like "10:32:07.326 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence". Moreover, they are so many that they almost prevent me from seeing the ones I produce. The reason why I don't mean to remove them at all is that, moving them to a file, I'm still allowed to use them for debugging, but only if I want to do so.SamCle88

2 Answers

2
votes

When u define org logger you need to pass the appender ref in that logger Hope this will work.

<Logger name="org" level="ALL" >
 <AppenderRef ref="General"/>
</Logger>

Spring by defaults Commons Logging library. You need to disable it and instead use the lg4j library. You need to exclude the commons-logging dependency.

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.0.0.RELEASE</version>
        <scope>runtime</scope>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

You need to put Log4j on the classpath, and provide it with a configuration file i.e log4j.xml in the root of the classpath. And add the following dependency in your pom.xml.

<dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
        <scope>runtime</scope>
    </dependency>

Reference - http://docs.spring.io/spring/docs/4.0.0.RELEASE/spring-framework-reference/htmlsingle/#overview-not-using-commons-logging

0
votes

At the end, I ended up by simply adding these dependencies on pom.

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.12</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-jcl</artifactId>
        <version>2.7</version>
    </dependency>

and removing the logger

<Logger name="org" level="ALL" />