4 Replies Latest reply on Feb 16, 2009 10:36 AM by Peter Johnson

    Configuring LOG4J to use JDBCAppender

    Jim Boone Newbie

      Hi!

      I have tried for several days now to coax JBoss to log certain events to a database via the JDBCAppender without any luck. Hopefully someone can point me in the right direction.

      I want to log to a database using a JBoss JDBC connection pool. I added a JDBC appender to the log4j.xml configuration file (shown below) and created my own custom JDBCAppender class (also shown below). JBoss fires up fine but when an event is written to the "DATABASE" appender, JBoss tells me that my datasource is not bound (see output below). Has anyone written Log4J events to a database via JDBC? If so what am I doing wrong? All help is appreciated.

      -- Jim

      ===== log4j.xml addition ======



      <param name="sql" value="
      INSERT INTO LOG4J (Date, Logger, Priority, Thread, Message)
      VALUES ('%d', '%c', '%p', '%t', '%m')
      "/>




      ============= JDBCAppender Class ================

      package com.bofa.esm.util;

      public class JDBCAppender extends org.apache.log4j.jdbc.JDBCAppender {

      private InitialContext _jndiCtx;
      private final static String JNDI_RESOURCE = "java:/LocalDS";

      /** Creates a new instance of JDBCAppender */
      public JDBCAppender() {
      super();
      try{
      System.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
      _jndiCtx = new InitialContext();
      } catch (Exception ex){
      System.err.println(ex.getMessage());;
      }
      }

      /**
      protected Connection getConnection() throws SQLException {

      try{
      // Setup connection to inventory database
      DataSource ds = (DataSource) _jndiCtx.lookup(JNDI_RESOURCE);
      return ds.getConnection();
      } catch (NamingException ne){
      String msg = ne.getMessage();
      LogLog.error(msg);;
      throw new SQLException(msg);
      }
      }

      protected void closeConnection(Connection con) {
      try{
      con.close();
      } catch (SQLException sqle){
      LogLog.error("JDBCAppender: " + sqle.getMessage());
      }
      }
      }

      ========= JBoss Output ==================

      2002-11-09 11:36:46,424 ERROR [STDERR] log4j:ERROR LocalDS not bound
      2002-11-09 11:36:46,424 ERROR [STDERR] log4j:ERROR Failed to excute sql
      2002-11-09 11:36:46,434 ERROR [STDERR] java.sql.SQLException: LocalDS not bound
      2002-11-09 11:36:46,434 ERROR [STDERR] at com.bofa.esm.util.JDBCAppender.getConnection(JDBCAppender.java:65)
      2002-11-09 11:36:46,434 ERROR [STDERR] at org.apache.log4j.jdbc.JDBCAppender.execute(JDBCAppender.java:155)
      2002-11-09 11:36:46,434 ERROR [STDERR] at org.apache.log4j.jdbc.JDBCAppender.flushBuffer(JDBCAppender.java:230)
      2002-11-09 11:36:46,434 ERROR [STDERR] at org.apache.log4j.jdbc.JDBCAppender.append(JDBCAppender.java:126)
      2002-11-09 11:36:46,444 ERROR [STDERR] at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:222)
      2002-11-09 11:36:46,444 ERROR [STDERR] at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:57)
      2002-11-09 11:36:46,444 ERROR [STDERR] at org.apache.log4j.Category.callAppenders(Category.java:190)
      2002-11-09 11:36:46,454 ERROR [STDERR] at org.apache.log4j.Category.forcedLog(Category.java:375)
      2002-11-09 11:36:46,454 ERROR [STDERR] at org.apache.log4j.Category.debug(Category.java:244)
      2002-11-09 11:36:46,454 ERROR [STDERR] at com.bofa.esm.eplogin.EPLoginDBProcessorBean.ejbCreate(EPLoginDBProcessorBean.java:71)
      2002-11-09 11:36:46,454 ERROR [STDERR] at java.lang.reflect.Method.invoke(Native Method)
      2002-11-09 11:36:46,464 ERROR [STDERR] at org.jboss.ejb.MessageDrivenEnterpriseContext.(MessageDrivenEnterpriseContext.java:66)
      2002-11-09 11:36:46,464 ERROR [STDERR] at org.jboss.ejb.plugins.MessageDrivenInstancePool.create(MessageDrivenInstancePool.java:62)
      2002-11-09 11:36:46,464 ERROR [STDERR] at org.jboss.ejb.plugins.AbstractInstancePool.get(AbstractInstancePool.java:209)
      2002-11-09 11:36:46,484 ERROR [STDERR] at org.jboss.ejb.plugins.MessageDrivenInstanceInterceptor.invoke(MessageDrivenInstanceInterceptor.java:74)
      2002-11-09 11:36:46,484 ERROR [STDERR] at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:107)
      2002-11-09 11:36:46,484 ERROR [STDERR] at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:178)
      2002-11-09 11:36:46,484 ERROR [STDERR] at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:60)
      2002-11-09 11:36:46,484 ERROR [STDERR] at org.jboss.ejb.plugins.RunAsSecurityInterceptor.invoke(RunAsSecurityInterceptor.java:100)
      2002-11-09 11:36:46,504 ERROR [STDERR] at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:204)
      2002-11-09 11:36:46,504 ERROR [STDERR] at org.jboss.ejb.MessageDrivenContainer.invoke(MessageDrivenContainer.java:302)
      2002-11-09 11:36:46,504 ERROR [STDERR] at org.jboss.ejb.plugins.jms.JMSContainerInvoker.invoke(JMSContainerInvoker.java:962)
      2002-11-09 11:36:46,504 ERROR [STDERR] at org.jboss.ejb.plugins.jms.JMSContainerInvoker$MessageListenerImpl.onMessage(JMSContainerInvoker.java:1038)
      2002-11-09 11:36:46,504 ERROR [STDERR] at org.jboss.jms.asf.StdServerSession.onMessage(StdServerSession.java:243)
      2002-11-09 11:36:46,514 ERROR [STDERR] at org.jboss.mq.SpyMessageConsumer.sessionConsumerProcessMessage(SpyMessageConsumer.java:603)
      2002-11-09 11:36:46,514 ERROR [STDERR] at org.jboss.mq.SpyMessageConsumer.addMessage(SpyMessageConsumer.java:417)
      2002-11-09 11:36:46,514 ERROR [STDERR] at org.jboss.mq.SpySession.run(SpySession.java:296)
      2002-11-09 11:36:46,524 ERROR [STDERR] at org.jboss.jms.asf.StdServerSession.run(StdServerSession.java:178)
      2002-11-09 11:36:46,524 ERROR [STDERR] at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:642)
      2002-11-09 11:36:46,524 ERROR [STDERR] at java.lang.Thread.run(Thread.java:484)
      2002-11-09 11:36:46,524 ERROR [STDERR] log4j:WARN --- Called JDBCAppender.getConnection()
      2002-11-09 11:36:46,524 ERROR [STDERR] log4j:ERROR LocalDS not bound