问题
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.
回答1:
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>
回答2:
Set the rootCategory to INFO
, set an apropriate Logger of your classes (e.g. org.myemployer.myapp
) to DEBUG
回答3:
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);
来源:https://stackoverflow.com/questions/9583775/excluding-only-one-level-of-log4j-logger