[jboss-jira] [JBoss JIRA] Created: (JBLOGGING-6) DatasourceAppender

Luca Stancapiano (JIRA) jira-events at lists.jboss.org
Tue Aug 19 18:03:35 EDT 2008


DatasourceAppender
------------------

                 Key: JBLOGGING-6
                 URL: https://jira.jboss.org/jira/browse/JBLOGGING-6
             Project: JBoss Logging
          Issue Type: Feature Request
      Security Level: Public (Everyone can see)
          Components: jboss-logging-log4j
    Affects Versions: 1.0.0.GA-slf4j-jboss-logging,  2.0.6.GA-spi
            Reporter: Luca Stancapiano
            Assignee: Dimitris Andreadis
             Fix For:  2.0.6.GA-spi, 1.0.0.GA-slf4j-jboss-logging


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 at 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()) {
			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();
		}
	}
}




-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: https://jira.jboss.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the jboss-jira mailing list