log4j: different category priority than appender threshold
I've been trying to get this simple use case to work but can't: Define a default Threshold of INFO on a FILE Appender, but define a category with DEBUG level. This is a jboss 4.2.1.GA log4j.xml file that I am using where I just want to log java.sql calls.
<appender name="SQL_FILE" class="org.jboss.logging.appender.DailyRollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="${jboss.server.log.dir}/sql.log"/>
<param name="Append" value="false"/>
<param name="Threshold" value="INFO"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %C %m%n"/>
</layout>
</appender>
<category name="java.sql">
<priority value="DEBUG"/>
<appender-ref ref="SQL_FILE"/>
</category>
<root>
<appender-ref ref="SQL_FILE"/>
</root>
Instead of logging just java.sql it logs INFO and above and does not include any java.sql information. If there is no e开发者_高级运维asy solution then I am left with setting the appender Threshold to DEBUG and turning off all logging for the multiple categories that get included, which seems a waste of time.
I tried the following but couldn't get it to work: Configuring multiple log files in log4j while using categories.
I soon found the solution. Basically the use case is described in the jboss wiki here: http://docs.jboss.org/process-guide/en/html/logging.html Section 10.3.5 Redirecting Category Output. This blog article was helpful as well: ptth://ourcraft.wordpress.com/2008/10/23/customizing-log4j-logging-on-jboss/#comment-796.
Ultimately here is what I used that worked:
<appender name="SQL_FILE" class="org.jboss.logging.appender.DailyRollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="${jboss.server.log.dir}/sql.log"/>
<param name="Append" value="false"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %C %m%n"/>
</layout>
</appender>
<category name="java.sql.Connection" additivity="false">
<priority value="DEBUG"/>
<appender-ref ref="SQL_FILE"/>
</category>
<root>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
Note that you do not want to define the SQL_FILE appender in root; for some reason. That was the final piece.
To the best of my knowledge, the classes in the java.sql
package do not actually do any logging, and they definitely do not write to log4j (or even commons-logging).
However, for theoretical purposes, here is how you would solve this problem if it were a different package (and commons-logging were actually being used):
- Remove the threshold from the
SQL_FILE
appender. - Add the line:
<priority value="INFO"/>
to the<root/>
element (before theappender-ref
). - Save and re-start JBoss.
Finally, note that for certain JDBC issues, the DriverManager.setLogWriter
method can be helpful (see http://java.sun.com/products/jdbc/reference/faqs/index.html#4).
EDIT:
Since you've indicated you're actually interested in logging iBatis output, you should just change <category name="java.sql">
to <category name="com.ibatis">
.
An additional step needed for JBoss 4.2.1.GA is to remove any log4j jar files from the war. If this jar is included in your war, the sql.log file is never written to.
Example maven pom.xml entry:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.13</version>
<scope>provided</scope>
</dependency>
精彩评论