0 Replies Latest reply: May 6, 2011 4:47 PM by Rino Finazzo RSS

    JDBCAppender Configuration in JBoss

    Rino Finazzo Newbie

      Hello

       

      I deployed my application to a JBoss AS 5.1.0.GA.

      It cointains a logging system which uses Log4j logging format.

      Here is the code of the MDB which implements the listener and launches the appending procedure.

      @MessageDriven(
              name="DBLogAppender", 
              activationConfig=
              {
                      @ActivationConfigProperty(propertyName="messagingType", propertyValue="javax.jms.MessageListener"),
                  @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
              @ActivationConfigProperty(propertyName="destination", propertyValue="queue/MyQueue")                        
              }
          )
      
      public class LogConsumer implements MessageListener{
          private static Logger logger = Logger.getLogger("DBLogAppender");
          public void onMessage(Message arg0) {
              logger.info(arg0);
      
          }
      

       

      In order to setup a JDBC appender for Log4j and make it send all logs to a DB, i tried to deploy my .ear with a log4j.properties file at its root directory as it follows:

       

       

      # Define the root logger with appender file
      log4j.rootLogger = DEBUG, DB
      
      # Define the DB appender
      log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender
      
      # Set JDBC URL
      log4j.appender.DB.URL=jdbc:mysql://localhost/MyDB
      
      # Set Database Driver
      log4j.appender.DB.driver=com.mysql.jdbc.Driver
      
      # Set database user name and password
      log4j.appender.DB.user=user
      log4j.appender.DB.password=password
      
      # Set the SQL statement to be executed.
      log4j.appender.DB.sql=INSERT INTO LOGS 
                            VALUES('%x','%d','%C','%p','%m')
      
      # Define the layout for file appender
      log4j.appender.DB.layout=org.apache.log4j.PatternLayout
      

       

      But it doesn't work because although i get

       

      21:36:52,759 INFO  [DBLogAppender] delegator->JBossMessage[21376378679263234]:PERSISTENT, deliveryId=0
      21:36:52,885 INFO  [DBLogAppender] delegator->JBossMessage[21376378681229315]:PERSISTENT, deliveryId=1
      

       

      from the jboss server log, i have my table empty in the DB.

       

      The table is created as it follows:

       

      CREATE TABLE LOGS
         (USER_ID VARCHAR(20) NOT NULL,
          DATED   DATE NOT NULL,
          LOGGER  VARCHAR(50) NOT NULL,
          LEVEL   VARCHAR(10) NOT NULL,
          MESSAGE VARCHAR(1000) NOT NULL
         );
      

      according to the log4j-compliant format.

       

      I also edited the conf/jboss-log4j.xml file:

       

      <appender name="DBLogAppender" class="org.apache.log4j.jdbc.JDBCAppender">
         <param name="url" value="jdbc:mysql://localhost/MyDB"/>
         <param name="driver" value="com.mysql.jdbc.Driver"/>
         <param name="user" value="user"/>
         <param name="password" value="password"/>
         <param name="sql" value="INSERT INTO LOGS VALUES('%x',
                                   '%d','%C','%p','%m')"/>
         <layout class="org.apache.log4j.PatternLayout">
         </layout>
      
      </appender>
      
      <logger name="log4j.rootLogger" additivity="false">
         <level value="DEBUG"/>
         <appender-ref ref="DBLogAppender"/>
      </logger>
      

      But actually I don't know if those lines are redundant because of the log4j.properties file i deployed, nevertheless it doesn't work.

       

      Anyone could help me to fix this.

       

      Thanks, Rino.