Reputation: 902
I want to log some behavior of my web application which also implements hibernate, spring and so on. When I tried to implement log4j logger from apache I had some troubles.
When I turn on logger it is also debugging hibernate and spring which I don't want. I tried to configure properties file to the specify the package of my project but it does not work.
Here is my code of property file:
log4j.rootCategory=ERROR, O
log4j.category.com.my.package= DEBUG, FILE, O
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=log/logger.log
log4j.appender.O=org.apache.log4j.ConsoleAppender
.... and some layout
It works when I switch rootCategory = DEBUG
but it is also debugging the hibernate and spring as I said.
Upvotes: 6
Views: 18870
Reputation: 2613
You need to set additivity=false to your package specific logger to override the root threshold level of INFO.
I don't know how to do it using the properties notation. I know only using the XML configuration form. I strongly recommend to use XML format.
So let's switch to Log4j, version 1, XML. Your configuration is roughly equivalent to this XML (omitting the FILE appender):
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" ...>
<logger name="com.my.package">
<level value="debug"/>
</logger>
<root>
<priority value="info"/>
<appender-ref ref="console"/>
</root>
</log4j:configuration>
Name it as log4j.xml, place it to the root of the class-path, and it will picked up automatically.
If you prefer and outside location, or different name, you have to use -Dlog4j.configuration=..path
, like this example, -Dlog4j.configuration=file:/C:\foo\bar\log4j_local.xml
on Windows.
Now let's set additivity=false to "com.my.package" logger.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" ...>
<logger name="com.my.package" additivity="false">
<level value="debug"/>
<appender-ref ref="console"/>
</logger>
<root>
<priority value="info"/>
<appender-ref ref="console"/>
</root>
</log4j:configuration>
Debug output from "com.my.package" will still get through despite root level is set to INFO.
You also need to add reference to appender to your logger:
<appender-ref ref="console"/>
Otherwise log4j complains about not having any appenders configured when logging from your package.
All other packages, like Hibernate or Spring, will be filtered on at lest INFO level importance level, no more spamming from them; only loggers from your package will get through with DEBUG level.
I found the inspiration on this post comment: https://stackoverflow.com/a/38040074/1185845
Upvotes: 1
Reputation: 2645
You would need to know the name of the loggers that are actually writing stuff... The simplest way is to set the root category to error:
log4j.rootCategory=ERROR, 0
Then set the level for your logs accordingly:
log4j.com.your.package=DEBUG...
Setting the rootCategory
to DEBUG
will turn everything to DEBUG
, unless you specifically configure a logger otherwise.
B.T.W, this is NOT a hibernate issue, this is related to how you are configuring your logger.
Upvotes: 3
Reputation: 597116
Yes, you have to specfiy the log level per package:
log4j.logger.org.hibernate=info
log4j.logger.org.springframework=info
log4j.logger.com.yourapplication=debug
Note that you should switch from categories (obsolete) to loggers. So log4j.rootLogger=...
Upvotes: 13