Uploaded image for project: 'JBoss Logging'
  1. JBoss Logging
  2. JBLOGGING-6

DatasourceAppender

    XMLWordPrintable

Details

    Description

      hi.... what do you think about a DatasourceAppender? This is very simple. It overrides getConnection and closeConnection methods of org.apache.log4j.jdbc.JDBCAppender class and it is configurable by a *-log4j.xml file. Here there is an example of xml file:

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

      <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">

      <appender name="DATABASE" class="org.jboss.logging.appender.DatasourceAppender">
      <param name="Threshold" value="INFO"/>
      <param name="datasource" value="java:/PortalDS"/>
      <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value=
      "INSERT INTO JBP_LOGGING_SAMPLES_USERS (log_date, log_type, log_user, operation) VALUES ( '%d

      {yyyy-MM-dd HH:mm:ss.SSS}

      ','%p', '%C;%L', '%C;%L')"/>
      </layout>
      </appender>

      <category name="org.jboss.portal.core.identity.UsersActivityStatsServiceImpl">
      <appender-ref ref="DATABASE"/>
      </category>

      </log4j:configuration>

      it is the same configuration for JDBCAppender but there is a 'datasource' parameter so you can specify the datasource location. Below there is the whole class:

      /*

      • JBoss, Home of Professional Open Source
      • Copyright 2005, JBoss Inc., and individual contributors as indicated
      • by the @authors tag. See the copyright.txt in the distribution for a
      • full listing of individual contributors.
        *
      • This is free software; you can redistribute it and/or modify it
      • under the terms of the GNU Lesser General Public License as
      • published by the Free Software Foundation; either version 2.1 of
      • the License, or (at your option) any later version.
        *
      • This software is distributed in the hope that it will be useful,
      • but WITHOUT ANY WARRANTY; without even the implied warranty of
      • MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
      • Lesser General Public License for more details.
        *
      • You should have received a copy of the GNU Lesser General Public
      • License along with this software; if not, write to the Free
      • Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
      • 02110-1301 USA, or see the FSF site: http://www.fsf.org.
        */
        package org.jboss.logging.appender;

      import java.sql.Connection;
      import java.sql.SQLException;

      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      import javax.sql.DataSource;

      /**

      • An extension of the default Log4j JDBCAppender which will use a datasource to
      • handle datas into database.
      • @version <tt>$Revision: 1958 $</tt>
      • @author <a href="mailto:jedim@vige.it">Luca Stancapiano</a>
        */
        public class DatasourceAppender extends org.apache.log4j.jdbc.JDBCAppender {

      /**

      • URL of the DB for default connection handling
        */
        protected String datasource = "java:/DefaultDS";
        protected DataSource ds;

      public void setDatasource(final String datasource)

      { this.datasource = datasource; }

      /**

      • I override this method to get a connection from datasource.
        */
        protected Connection getConnection() throws SQLException {
        if (connection == null || connection.isClosed())
        Unknown macro: { if (ds == null) try { InitialContext ic = new InitialContext(); ds = (DataSource) ic.lookup(datasource); } catch (NamingException e) { e.printStackTrace(); } connection = ds.getConnection(); }

      return connection;
      }

      protected void closeConnection(Connection con) {
      try

      { connection.close(); }

      catch (SQLException ex)

      { ex.printStackTrace(); }

      }
      }

      Attachments

        Activity

          People

            dlloyd@redhat.com David Lloyd
            luca.stancapiano@vige.it Luca Stancapiano (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: