com.antelmann.util.logging
Class JDBCLogWriter
java.lang.Object
com.antelmann.util.logging.AbstractLogWriter<Object[]>
com.antelmann.util.logging.JDBCLogWriter
- All Implemented Interfaces:
- Filter<LogEntry>, LogWriter
public class JDBCLogWriter
- extends AbstractLogWriter<Object[]>
JDBCLogWriter provides a LogWriter that writes to a java.sql.Connection.
It requires a LogEntryFormatter that maps the fields from the LogEntry object
to the fields of a table. The required LogEntryFormatter's
format(LogEntry) would return an Object[] that contains the values
that are to be inserted into the table.
This implementation is only suitable for those implementations where it is
sufficient to store the LogEntry data into a single row of a single table.
Below is some sample code that demonstrates how a LogEntryFormatter could be
implemented to be used with this class.
public Object format (LogEntry entry) {
Object[] value = new Object[10];
value[0] = new Timestamp(entry.getTime());
value[1] = entry.getLevel().toString();
value[2] = entry.getMessage();
value[3] = entry.getSourceClassName();
value[4] = entry.getThreadName();
value[5] = (entry.getThrown() == null)? null : entry.getThrown().getMessage();
value[6] = (entry.getThrown() == null)? null : entry.getThrown().getClass().getName();
if (entry.getParameters() != null) {
if (entry.getParameters().length > 0) value[7] = (entry.getParameters()[0] == null)? null : entry.getParameters()[0].toString();
if (entry.getParameters().length > 1) value[8] = (entry.getParameters()[1] == null)? null : entry.getParameters()[1].toString();
if (entry.getParameters().length > 2) value[9] = (entry.getParameters()[2] == null)? null : entry.getParameters()[2].toString();
}
for (int i = 0; i < value.length; i++) {
if (value[i] == null) value[i] = ""; // ensuring no null values
}
return value;
}
The above code example would work with a table that contains 10 data fields
that would take the above returned array accordingly.
- Author:
- Holger Antelmann
- See Also:
ConnectionHelper.insertRow(String, Object[])
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
JDBCLogWriter
public JDBCLogWriter(Connection con,
String tableName,
LogEntryFormatter<Object[]> formatter)
- requires a formatter that will split log entries in an object array
which corresponds to the fields in the given table.
- See Also:
ConnectionHelper.insertRow(String, Object[])
writeLogPattern
public void writeLogPattern(Object[] pattern)
throws LogException
- the entry is split into an object array by the formatter which is
then inserted into the table.
For more sophisticated storage that requires more than one table
(if e.g. a variable number of parameters are to be stored in a separate table)
a different implementation would be required.
- Specified by:
writeLogPattern in class AbstractLogWriter<Object[]>
- Throws:
LogException- See Also:
ConnectionHelper.insertRow(String, Object[])
getConnection
public Connection getConnection()
(c) 2001-2006 Holger Antelmann - all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads