sdeboy 2003/06/04 22:19:05 Added: src/java/org/apache/log4j/jdbc JDBCReceiver.java Log: created a first pass at a jdbcreceiver for use with chainsaw... the receiver only runs the sql query one time..subsequent runs would create duplicate event entries in chainsaw not all fields are supported...patternlayout doesn't support properties and jdbcappender doesn't support exceptions. example appender configuration: <appender name="JDBC" class="org.apache.log4j.jdbc.JDBCAppender"> <param name="password" value="userpass"/> <param name="user" value="username"/> <param name="driver" value="org.gjt.mm.mysql.Driver"/> <param name="sql" value='insert into logtable (logger, timestamp , level, thread, message, ndc, mdc, class, method, file, line) values ("%c", "%d ", "%p", "%t", "%m", "%x", "%X", "%C", "%M", "%F", "%L")'/> <param name="URL" value="jdbc:mysql://127.0.0.1/test"/> </appender> If fields weren't separated when they were added to the database, unused fields still need to be defined (all columns in the list below need provided, even if you don't have them in your database) - use AS statements to make the column names match example receiver configuration: <plugin name="JDBCReceiver" class="org.apache.log4j.jdbc.JDBCReceiver"> <param name="password" value=""/> <param name="user" value="root"/> <param name="driver" value="org.gjt.mm.mysql.Driver"/> <param name="sql" value='select logger as LOGGER, timestamp as TIMESTAMP, level as LEVEL, thread as THREAD, message as MESSAGE, ndc as NDC, mdc as MDC, class as CLASS, method as METHOD, file as FILE, line as LINE from logtable'/> <param name="URL" value="jdbc:mysql://127.0.0.1/test"/> </plugin> Revision Changes Path 1.1 jakarta-log4j-sandbox/src/java/org/apache/log4j/jdbc/JDBCReceiver.java Index: JDBCReceiver.java =================================================================== /* * ============================================================================ * The Apache Software License, Version 1.1 * ============================================================================ * * Copyright (C) 1999 The Apache Software Foundation. All rights reserved. * * Redistribution and use in source and binary forms, with or without modifica- * tion, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * 3. The end-user documentation included with the redistribution, if any, must * include the following acknowledgment: "This product includes software * developed by the Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, if * and wherever such third-party acknowledgments normally appear. * * 4. The names "log4j" and "Apache Software Foundation" must not be used to * endorse or promote products derived from this software without prior * written permission. For written permission, please contact * [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", nor may * "Apache" appear in their name, without prior written permission of the * Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * This software consists of voluntary contributions made by many individuals * on behalf of the Apache Software Foundation. For more information on the * Apache Software Foundation, please see <http://www.apache.org/>. * */ package org.apache.log4j.jdbc; import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.log4j.plugins.Receiver; import org.apache.log4j.spi.LocationInfo; import org.apache.log4j.spi.LoggingEvent; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.text.DateFormat; import java.text.ParseException; import java.util.Calendar; import java.util.Date; import java.util.Hashtable; import java.util.StringTokenizer; public class JDBCReceiver extends Receiver { private boolean isActive = false; /** * URL of the DB for default connection handling */ protected String databaseURL = "jdbc:odbc:myDB"; /** * User to connect as for default connection handling */ protected String databaseUser = "me"; /** * User to use for default connection handling */ protected String databasePassword = "mypassword"; protected Connection connection = null; protected String sqlStatement = ""; public JDBCReceiver() { } public void activateOptions() { new JDBCReceiverThread().start(); } protected Connection getConnection() throws SQLException { if (!DriverManager.getDrivers().hasMoreElements()) { setDriver("sun.jdbc.odbc.JdbcOdbcDriver"); } if (connection == null) { connection = DriverManager.getConnection( databaseURL, databaseUser, databasePassword); } return connection; } public void close() { try { if ((connection != null) && !connection.isClosed()) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } public void finalize() { close(); } protected void setActive(boolean active) { isActive = active; } public synchronized boolean isActive() { return isActive; } public synchronized void shutdown() { setActive(false); } public void setSql(String s) { sqlStatement = s; } public String getSql() { return sqlStatement; } public void setUser(String user) { databaseUser = user; } public void setURL(String url) { databaseURL = url; } public void setPassword(String password) { databasePassword = password; } public String getUser() { return databaseUser; } public String getURL() { return databaseURL; } public String getPassword() { return databasePassword; } /** * Ensures that the given driver class has been loaded for sql connection * creation. */ public void setDriver(String driverClass) { try { Class.forName(driverClass); } catch (Exception e) { e.printStackTrace(); } } class JDBCReceiverThread extends Thread { public JDBCReceiverThread() { setDaemon(true); } public void run() { setActive(true); try { Logger logger = null; long timeStamp = 0L; String level = null; String threadName = null; Object message = null; String ndc = null; Hashtable mdc = null; String[] exception = null; String className = null; String methodName = null; String fileName = null; String lineNumber = null; Hashtable properties = null; Statement statement = getConnection().createStatement(); ResultSet rs = statement.executeQuery(sqlStatement); rs.beforeFirst(); while (rs.next()) { logger = Logger.getLogger(rs.getString("LOGGER")); DateFormat df = DateFormat.getDateInstance(DateFormat.LONG); df.setLenient(true); try { timeStamp = df.parse(rs.getString("TIMESTAMP")).getTime(); } catch (ParseException pe) { timeStamp = new Date().getTime(); } level = rs.getString("LEVEL"); threadName = rs.getString("THREAD"); message = rs.getString("MESSAGE"); ndc = rs.getString("NDC"); String mdcString = rs.getString("MDC"); mdc = new Hashtable(); if (mdcString != null) { if ( (mdcString.indexOf("{{") > -1) && (mdcString.indexOf("}}") > -1)) { mdcString = mdcString.substring( mdcString.indexOf("{{") + 2, mdcString.indexOf("}}")); StringTokenizer tok = new StringTokenizer(mdcString, ","); while (tok.countTokens() > 1) { mdc.put(tok.nextToken(), tok.nextToken()); } } } //exception not supported for now - but a placeholder is here for when it is supported //exception = new String[] {rs.getString("EXCEPTION")}; className = rs.getString("CLASS"); methodName = rs.getString("METHOD"); fileName = rs.getString("FILE"); lineNumber = rs.getString("LINE"); //properties not supported for now properties = new Hashtable(); Level levelImpl = Level.toLevel(level); LoggingEvent event = new LoggingEvent( logger.getName(), logger, timeStamp, levelImpl, threadName, message, ndc, mdc, exception, new LocationInfo(fileName, className, methodName, lineNumber), properties); doPost(event); } } catch (SQLException se) { se.printStackTrace(); } } } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]