简体   繁体   中英

Specify only some packages to have debug output

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.

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=...

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.

BTW, this is NOT a hibernate issue, this is related to how you are configuring your logger.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM