tags:

views:

656

answers:

3

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 easy 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: http://stackoverflow.com/questions/2050615/configuring-multiple-log-files-in-log4j-while-using-categories.

A: 

Hi James. 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):

  1. Remove the threshold from the SQL_FILE appender.
  2. Add the line: <priority value="INFO"/> to the <root/> element (before the appender-ref).
  3. 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"> .

Matt Solnit
Thanks for the reply. See my solution below. It is ironic, but you are right that java.sql does not actually do any logging. But I guess through ibatis I am able to get the output. Here is a sample:2010-02-22 16:48:45,469 DEBUG [java.sql.Connection] com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl {conn-100000} Connection2010-02-22 16:48:45,479 DEBUG [java.sql.Connection] com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl {conn-100000} Preparing Statement: SELECT.......
james.lorenzen
There is nothing ironic about it James. java.sql is a core JAVA package. It should not use log4j or commons-logging. java.util.logging.Logger is a JDK class which can be used but that came in JDK 1.4.
Calm Storm
A: 

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.

james.lorenzen
This solution is not great for performance. In JBoss 4.2.1, by default, the `root` logger is set to `DEBUG`, and the appenders use thresholds to work around this. This means that any Java code checking `Log.isDebugEnabled()` will execute. Try it in a debugger and see :-). My solution will avoid this problem.
Matt Solnit
In fact, I wouldn't be surprised if __everything__ is writing DEBUG output to the `SQL_FILE` appender, not just iBatis.
Matt Solnit
A: 

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>
Jeff