Fields Summary |
---|
protected String | databaseURLURL of the DB for default connection handling |
protected String | databaseUserUser to connect as for default connection handling |
protected String | databasePasswordUser to use for default connection handling |
protected Connection | connectionConnection used by default. The connection is opened the first time it
is needed and then held open until the appender is closed (usually at
garbage collection). This behavior is best modified by creating a
sub-class and overriding the getConnection and
closeConnection methods. |
protected String | sqlStatementStores the string given to the pattern layout for conversion into a SQL
statement, eg: insert into LogTable (Thread, Class, Message) values
("%t", "%c", "%m").
Be careful of quotes in your messages!
Also see PatternLayout. |
protected int | bufferSizesize of LoggingEvent buffer before writting to the database.
Default is 1. |
protected ArrayList | bufferArrayList holding the buffer of Logging Events. |
protected ArrayList | removesHelper object for clearing out the buffer |
Methods Summary |
---|
public void | append(org.apache.log4j.spi.LoggingEvent event)Adds the event to the buffer. When full the buffer is flushed.
buffer.add(event);
if (buffer.size() >= bufferSize)
flushBuffer();
|
public void | close()Closes the appender, flushing the buffer first then closing the default
connection if it is open.
flushBuffer();
try {
if (connection != null && !connection.isClosed())
connection.close();
} catch (SQLException e) {
errorHandler.error("Error closing connection", e, ErrorCode.GENERIC_FAILURE);
}
this.closed = true;
|
protected void | closeConnection(java.sql.Connection con)Override this to return the connection to a pool, or to clean up the
resource.
The default behavior holds a single connection open until the appender
is closed (typically when garbage collected).
|
protected void | execute(java.lang.String sql)Override this to provide an alertnate method of getting
connections (such as caching). One method to fix this is to open
connections at the start of flushBuffer() and close them at the
end. I use a connection pool outside of JDBCAppender which is
accessed in an override of this method.
Connection con = null;
Statement stmt = null;
try {
con = getConnection();
stmt = con.createStatement();
stmt.executeUpdate(sql);
} catch (SQLException e) {
if (stmt != null)
stmt.close();
throw e;
}
stmt.close();
closeConnection(con);
//System.out.println("Execute: " + sql);
|
public void | finalize()closes the appender before disposal
close();
|
public void | flushBuffer()loops through the buffer of LoggingEvents, gets a
sql string from getLogStatement() and sends it to execute().
Errors are sent to the errorHandler.
If a statement fails the LoggingEvent stays in the buffer!
//Do the actual logging
removes.ensureCapacity(buffer.size());
for (Iterator i = buffer.iterator(); i.hasNext();) {
try {
LoggingEvent logEvent = (LoggingEvent)i.next();
String sql = getLogStatement(logEvent);
execute(sql);
removes.add(logEvent);
}
catch (SQLException e) {
errorHandler.error("Failed to excute sql", e,
ErrorCode.FLUSH_FAILURE);
}
}
// remove from the buffer any events that were reported
buffer.removeAll(removes);
// clear the buffer of reported events
removes.clear();
|
public int | getBufferSize()
return bufferSize;
|
protected java.sql.Connection | getConnection()Override this to link with your connection pooling system.
By default this creates a single connection which is held open
until the object is garbage collected.
if (!DriverManager.getDrivers().hasMoreElements())
setDriver("sun.jdbc.odbc.JdbcOdbcDriver");
if (connection == null) {
connection = DriverManager.getConnection(databaseURL, databaseUser,
databasePassword);
}
return connection;
|
protected java.lang.String | getLogStatement(org.apache.log4j.spi.LoggingEvent event)By default getLogStatement sends the event to the required Layout object.
The layout will format the given pattern into a workable SQL string.
Overriding this provides direct access to the LoggingEvent
when constructing the logging statement.
return getLayout().format(event);
|
public java.lang.String | getPassword()
return databasePassword;
|
public java.lang.String | getSql()Returns pre-formated statement eg: insert into LogTable (msg) values ("%m")
return sqlStatement;
|
public java.lang.String | getURL()
return databaseURL;
|
public java.lang.String | getUser()
return databaseUser;
|
public boolean | requiresLayout()JDBCAppender requires a layout.
return true;
|
public void | setBufferSize(int newBufferSize)
bufferSize = newBufferSize;
buffer.ensureCapacity(bufferSize);
removes.ensureCapacity(bufferSize);
|
public void | setDriver(java.lang.String driverClass)Ensures that the given driver class has been loaded for sql connection
creation.
try {
Class.forName(driverClass);
} catch (Exception e) {
errorHandler.error("Failed to load driver", e,
ErrorCode.GENERIC_FAILURE);
}
|
public void | setPassword(java.lang.String password)
databasePassword = password;
|
public void | setSql(java.lang.String s)
sqlStatement = s;
if (getLayout() == null) {
this.setLayout(new PatternLayout(s));
}
else {
((PatternLayout)getLayout()).setConversionPattern(s);
}
|
public void | setURL(java.lang.String url)
databaseURL = url;
|
public void | setUser(java.lang.String user)
databaseUser = user;
|