Specify only some packages to have debug output
Asked Answered
C

3

6

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.

Clothier answered 22/7, 2011 at 21:39 Comment(5)
This may help with hibernate: #2077877Foreyard
How do you create your Logger objects?Bulge
@kmb385 your link helps me a lot. thanksClothier
@kevin initiated properties and call static method getLogger("some_name"); but i already solved it :] but thanks anywayClothier
When you say some_name, would that be like com.my.package.some_name or more like calculator_logs? Log4J Loggers 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.Bulge
R
13

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

Royo answered 22/7, 2011 at 21:48 Comment(3)
Why would you not just specify ERROR for the root logger and then DEBUG for the specific package that needs debugging?Bulge
@Kevin i don't know why but it just don't work that way for me.Clothier
@Kevin Because it won't work. Root level overrides individual package levels. You need to set additvity=false to your package to override the root level. Look at my solution https://mcmap.net/q/1609412/-specify-only-some-packages-to-have-debug-output - that one works with specifying just the root level and then exceptions. Much better then this approved solution.Gavan
S
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.

Schulein answered 22/7, 2011 at 21:51 Comment(1)
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.Bulge
G
1

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://mcmap.net/q/142358/-log4j-log-output-of-a-specific-class-to-a-specific-appender

Gavan answered 25/4 at 16:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.