Reputation: 275
At the moment, I'm working with
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.
Upvotes: 1
Views: 562
Reputation: 275
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" />
Upvotes: 0
Reputation: 482
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>
Upvotes: 2