简体   繁体   中英

Excluding only one level of Log4j Logger

I'm using Log4j in a application in which I also use Axis2 and Jetty web server.

I configured the Log4J property file to exclude these classes from logging when in debug priority. But when I did this, the other priority messages also began to be excluded from the main logger.

Is there a way that I can tell Log4j that I just want to log INFO logs from these classes while logging debug logs from my classes?

Here's what I have done:

#Jetty Server and Axis2
log4j.category.org.apache.axiom=DEBUG
log4j.additivity.org.apache.axiom=false
log4j.category.org.apache.axis2=DEBUG
log4j.additivity.org.apache.axis2=false

################# MAIN LOGGER #################
log4j.rootCategory=DEBUG, mainLogger

#File configuration

But as I said this configuration also exclude INFO messages from the main logger.

No, set the Root level to DEBUG, and

log4j.category.org.apache.axiom=INFO
log4j.category.org.apache.axis2=INFO

Also, do not set the additivity to false .

When you're starting from scratch, you might want to use the more modern XML config format right away. There, it would look like this:

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<appender name="FILE" class="org.apache.log4j.RollingFileAppender"> 
...
</appender> 

<category name="com.apache.axiom">
    <priority value="INFO" />
</category>

<root> 
    <priority value ="DEBUG" /> 
    <appender-ref ref="FILE" /> 
</root>

</log4j:configuration>

将rootCategory设置为INFO ,将您的类的适当Logger(例如org.myemployer.myapp )设置为DEBUG

Using this you can set your log level:

    import org.apache.log4j.Level;

    public static Logger logger = null;

    logger.setLevel(Level.DEBUG);

and

 Logger.getRootLogger().setLevel(Level.INFO);

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