5

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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Smolda
  • 882
  • 5
  • 13
  • 34
  • This may help with hibernate: http://stackoverflow.com/questions/2077377/cant-stop-hibernate-from-writing-log-to-console-log4j-properties-is-ok – Kevin Bowersox Jul 22 '11 at 21:44
  • How do you create your `Logger` objects? – Kevin Panko Jul 22 '11 at 21:59
  • @kmb385 your link helps me a lot. thanks – Smolda Jul 22 '11 at 22:01
  • @kevin initiated properties and call static method getLogger("some_name"); but i already solved it :] but thanks anyway – Smolda Jul 22 '11 at 22:03
  • When you say `some_name`, would that be like `com.my.package.some_name` or more like `calculator_logs`? Log4J `Logger`s form a hierarchy based on the periods in their names, so it is usually best to name your loggers with the same names as the classes that are doing the logging. – Kevin Panko Jul 22 '11 at 22:11

2 Answers2

12

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

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
3

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.

chahuistle
  • 2,627
  • 22
  • 30
  • This should be the correct answer, but I think because the question asker is not naming the logger objects like `com.your.package.foo.bar`, the DEBUG configuration line has no effect. – Kevin Panko Jul 26 '11 at 20:49