[logback-dev] svn commit: r606 - in logback/trunk/logback-classic/src: main/java/ch/qos/logback/classic/db main/java/ch/qos/logback/classic/db/dialect test/java/ch/qos/logback/classic/db
noreply.seb at qos.ch
noreply.seb at qos.ch
Tue Sep 26 18:14:39 CEST 2006
Author: seb
Date: Tue Sep 26 18:14:39 2006
New Revision: 606
Removed:
logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/db/ConnectionSource.java
logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/db/ConnectionSourceBase.java
logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/db/DataSourceConnectionSource.java
logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/db/DriverManagerConnectionSource.java
logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/db/JNDIConnectionSource.java
logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/db/dialect/
Modified:
logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/db/DBAppender.java
logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/db/DBHelper.java
logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/db/DBAppenderTest.java
Log:
- Created DBAppenderBase, and modified DBAppender accordingly
- Moved classes to Core module when possible.
Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/db/DBAppender.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/db/DBAppender.java (original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/db/DBAppender.java Tue Sep 26 18:14:39 2006
@@ -10,25 +10,16 @@
package ch.qos.logback.classic.db;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
-import java.sql.ResultSet;
import java.sql.SQLException;
-import java.sql.Statement;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.Map;
-import java.util.Set;
-import ch.qos.logback.classic.db.dialect.DBUtil;
-import ch.qos.logback.classic.db.dialect.SQLDialect;
import ch.qos.logback.classic.spi.CallerData;
import ch.qos.logback.classic.spi.LoggingEvent;
-import ch.qos.logback.classic.spi.ThrowableInformation;
-import ch.qos.logback.core.AppenderBase;
-import ch.qos.logback.core.Layout;
+import ch.qos.logback.core.db.DBAppenderBase;
+import ch.qos.logback.core.db.dialect.SQLDialect;
/**
* The DBAppender inserts loggin events into three database tables in a format
@@ -109,126 +100,32 @@
* @author Ray DeCampo
* @author Sébastien Pennec
*/
-public class DBAppender extends AppenderBase {
- static final String insertPropertiesSQL = "INSERT INTO logging_event_property (event_id, mapped_key, mapped_value) VALUES (?, ?, ?)";
- static final String insertExceptionSQL = "INSERT INTO logging_event_exception (event_id, i, trace_line) VALUES (?, ?, ?)";
- static final String insertSQL;
- private static final Method GET_GENERATED_KEYS_METHOD;
-
- static {
- StringBuffer sql = new StringBuffer();
- sql.append("INSERT INTO logging_event (");
- sql.append("timestmp, ");
- sql.append("formatted_message, ");
- sql.append("logger_name, ");
- sql.append("level_string, ");
- sql.append("thread_name, ");
- sql.append("reference_flag, ");
- sql.append("caller_filename, ");
- sql.append("caller_class, ");
- sql.append("caller_method, ");
- sql.append("caller_line) ");
- sql.append(" VALUES (?, ?, ? ,?, ?, ?, ?, ?, ?,?)");
- insertSQL = sql.toString();
- //
- // PreparedStatement.getGeneratedKeys added in JDK 1.4
- //
- Method getGeneratedKeysMethod;
- try {
- getGeneratedKeysMethod = PreparedStatement.class.getMethod(
- "getGeneratedKeys", (Class[]) null);
- } catch (Exception ex) {
- getGeneratedKeysMethod = null;
- }
- GET_GENERATED_KEYS_METHOD = getGeneratedKeysMethod;
- }
-
- ConnectionSource connectionSource;
- boolean cnxSupportsGetGeneratedKeys = false;
- boolean cnxSupportsBatchUpdates = false;
- SQLDialect sqlDialect;
+public class DBAppender extends DBAppenderBase {
public DBAppender() {
}
@Override
- public void start() {
+ protected void subAppend(Object eventObject, Connection connection,
+ PreparedStatement insertStatement) throws Throwable {
+ LoggingEvent event = (LoggingEvent) eventObject;
- if (connectionSource == null) {
- throw new IllegalStateException(
- "DBAppender cannot function without a connection source");
- }
+ addLoggingEvent(insertStatement, event);
+ // This is very expensive... should we do it every time?
+ addCallerData(insertStatement, event.getCallerData());
- sqlDialect = DBUtil
- .getDialectFromCode(connectionSource.getSQLDialectCode());
- if (GET_GENERATED_KEYS_METHOD != null) {
- cnxSupportsGetGeneratedKeys = connectionSource.supportsGetGeneratedKeys();
- } else {
- cnxSupportsGetGeneratedKeys = false;
+ int updateCount = insertStatement.executeUpdate();
+ if (updateCount != 1) {
+ addWarn("Failed to insert loggingEvent");
}
- cnxSupportsBatchUpdates = connectionSource.supportsBatchUpdates();
- if (!cnxSupportsGetGeneratedKeys && (sqlDialect == null)) {
- throw new IllegalStateException(
- "DBAppender cannot function if the JDBC driver does not support getGeneratedKeys method *and* without a specific SQL dialect");
- }
-
- // all nice and dandy on the eastern front
- super.start();
- }
- /**
- * @return Returns the connectionSource.
- */
- public ConnectionSource getConnectionSource() {
- return connectionSource;
- }
+ int eventId = getEventId(insertStatement, connection);
- /**
- * @param connectionSource
- * The connectionSource to set.
- */
- public void setConnectionSource(ConnectionSource connectionSource) {
- this.connectionSource = connectionSource;
- }
+ Map<String, String> mergedMap = mergePropertyMaps(event);
+ insertProperties(mergedMap, connection, eventId);
- @Override
- protected void append(Object eventObject) {
- LoggingEvent event = (LoggingEvent) eventObject;
- Connection connection = null;
- try {
- connection = connectionSource.getConnection();
- connection.setAutoCommit(false);
-
- PreparedStatement insertStatement = connection
- .prepareStatement(insertSQL);
-
- addLoggingEvent(insertStatement, event);
- // This is very expensive... should we do it every time?
- addCallerData(insertStatement, event.getCallerData());
-
- int updateCount = insertStatement.executeUpdate();
- if (updateCount != 1) {
- addWarn("Failed to insert loggingEvent");
- }
-
- int eventId = getEventId(insertStatement, connection);
-
- // we no longer need the insertStatement
- if (insertStatement != null) {
- insertStatement.close();
- insertStatement = null;
- }
-
- Map<String, String> mergedMap = mergePropertyMaps(event);
- insertProperties(mergedMap, connection, eventId);
-
- insertThrowable(event.getThrowableInformation(), connection, eventId);
-
- connection.commit();
- } catch (Throwable sqle) {
- addError("problem appending event", sqle);
- } finally {
- DBHelper.closeConnection(connection);
+ if (event.getThrowableInformation() != null) {
+ insertThrowable(event.getThrowableInformation().getThrowableStrRep(), connection, eventId);
}
}
@@ -253,54 +150,6 @@
}
}
- int getEventId(PreparedStatement insertStatement, Connection connection)
- throws SQLException, InvocationTargetException {
- ResultSet rs = null;
- Statement idStatement = null;
- boolean gotGeneratedKeys = false;
- if (cnxSupportsGetGeneratedKeys) {
- try {
- rs = (ResultSet) GET_GENERATED_KEYS_METHOD.invoke(insertStatement,
- (Object[]) null);
- gotGeneratedKeys = true;
- } catch (InvocationTargetException ex) {
- Throwable target = ex.getTargetException();
- if (target instanceof SQLException) {
- throw (SQLException) target;
- }
- throw ex;
- } catch (IllegalAccessException ex) {
- addWarn(
- "IllegalAccessException invoking PreparedStatement.getGeneratedKeys",
- ex);
- }
- }
-
- if (!gotGeneratedKeys) {
- insertStatement.close();
- insertStatement = null;
-
- idStatement = connection.createStatement();
- idStatement.setMaxRows(1);
- rs = idStatement.executeQuery(sqlDialect.getSelectInsertId());
- }
-
- // A ResultSet cursor is initially positioned before the first row;
- // the
- // first call to the method next makes the first row the current row
- rs.next();
- int eventId = rs.getInt(1);
-
- rs.close();
-
- if (idStatement != null) {
- idStatement.close();
- idStatement = null;
- }
-
- return eventId;
- }
-
Map<String, String> mergePropertyMaps(LoggingEvent event) {
Map<String, String> mergedMap = new HashMap<String, String>();
// we add the context properties first, then the event properties, since
@@ -319,75 +168,4 @@
return mergedMap;
}
-
- void insertProperties(Map<String, String> mergedMap, Connection connection,
- int eventId) throws SQLException {
- Set propertiesKeys = mergedMap.keySet();
- if (propertiesKeys.size() > 0) {
- PreparedStatement insertPropertiesStatement = connection
- .prepareStatement(insertPropertiesSQL);
-
- for (Iterator i = propertiesKeys.iterator(); i.hasNext();) {
- String key = (String) i.next();
- String value = (String) mergedMap.get(key);
-
- insertPropertiesStatement.setInt(1, eventId);
- insertPropertiesStatement.setString(2, key);
- insertPropertiesStatement.setString(3, value);
-
- if (cnxSupportsBatchUpdates) {
- insertPropertiesStatement.addBatch();
- } else {
- insertPropertiesStatement.execute();
- }
- }
-
- if (cnxSupportsBatchUpdates) {
- insertPropertiesStatement.executeBatch();
- }
-
- insertPropertiesStatement.close();
- insertPropertiesStatement = null;
- }
- }
-
- void insertThrowable(ThrowableInformation ti, Connection connection,
- int eventId) throws SQLException {
- String[] strRep = null;
- if (ti != null) {
- strRep = ti.getThrowableStrRep();
-
- PreparedStatement insertExceptionStatement = connection
- .prepareStatement(insertExceptionSQL);
-
- for (short i = 0; i < strRep.length; i++) {
- insertExceptionStatement.setInt(1, eventId);
- insertExceptionStatement.setShort(2, i);
- insertExceptionStatement.setString(3, strRep[i]);
- if (cnxSupportsBatchUpdates) {
- insertExceptionStatement.addBatch();
- } else {
- insertExceptionStatement.execute();
- }
- }
- if (cnxSupportsBatchUpdates) {
- insertExceptionStatement.executeBatch();
- }
- insertExceptionStatement.close();
- insertExceptionStatement = null;
- }
- }
-
- @Override
- public void stop() {
- super.stop();
- }
-
- public Layout getLayout() {
- return null;
- }
-
- public void setLayout(Layout layout) {
- }
-
}
Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/db/DBHelper.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/db/DBHelper.java (original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/db/DBHelper.java Tue Sep 26 18:14:39 2006
@@ -10,10 +10,6 @@
package ch.qos.logback.classic.db;
-import java.sql.Connection;
-import java.sql.SQLException;
-import java.sql.Statement;
-
import ch.qos.logback.classic.spi.LoggingEvent;
/**
@@ -49,24 +45,4 @@
}
return mask;
}
-
- static public void closeConnection(Connection connection) {
- if (connection != null) {
- try {
- connection.close();
- } catch (SQLException sqle) {
- // static utility classes should not log without an explicit repository
- // reference
- }
- }
- }
-
- public static void closeStatement(Statement statement) {
- if (statement != null) {
- try {
- statement.close();
- } catch (SQLException sqle) {
- }
- }
- }
}
Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/db/DBAppenderTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/db/DBAppenderTest.java (original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/db/DBAppenderTest.java Tue Sep 26 18:14:39 2006
@@ -10,6 +10,7 @@
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.CallerData;
import ch.qos.logback.classic.spi.LoggingEvent;
+import ch.qos.logback.core.db.DriverManagerConnectionSource;
public class DBAppenderTest extends DBAppenderTestBase {
More information about the logback-dev
mailing list