public class JDBCAppender extends AppenderSkeleton implements Appender
WARNING: This version of JDBCAppender is very likely to be completely replaced in the future. Moreoever, it does not log exceptions.
Each append call adds to an ArrayList buffer. When the buffer is
filled each log event is placed in a sql statement (configurable) and
executed.
BufferSize, db URL, User, & Password are
configurable options in the standard log4j ways.
The setSql(String sql) sets the SQL statement to be used for
logging -- this statement is sent to a PatternLayout (either
created automatically by the appender or added by the user). Therefore by
default all the conversion patterns in PatternLayout can be used
inside of the statement. (see the test cases for examples)
Overriding the getLogStatement(org.apache.log4j.spi.LoggingEvent) method allows more explicit control
of the statement used for logging.
For use as a base class:
getConnection() to pass any connection you want.
Typically this is used to enable application wide connection pooling.
closeConnection(Connection con) -- if you override
getConnection make sure to implement closeConnection to handle
the connection you generated. Typically this would return the connection to
the pool it came from.
getLogStatement(LoggingEvent event) to produce
specialized or dynamic statements. The default uses the sql option value.
| Modifier and Type | Field and Description |
|---|---|
protected java.util.ArrayList<LoggingEvent> |
buffer
ArrayList holding the buffer of Logging Events.
|
protected int |
bufferSize
size of LoggingEvent buffer before writting to the database.
|
protected java.sql.Connection |
connection
Connection used by default.
|
protected java.lang.String |
databasePassword
User to use for default connection handling
|
protected java.lang.String |
databaseURL
URL of the DB for default connection handling
|
protected java.lang.String |
databaseUser
User to connect as for default connection handling
|
private static java.lang.IllegalArgumentException |
ILLEGAL_PATTERN_FOR_SECURE_EXEC |
private boolean |
isActive |
private boolean |
locationInfo |
private JdbcPatternParser |
preparedStatementParser |
protected java.lang.String |
sqlStatement
Stores the string given to the pattern layout for conversion into a SQL
statement, eg: insert into LogTable (Thread, Class, Message) values ("%t",
"%c", "%m").
|
closed, errorHandler, headFilter, layout, name, tailFilter, threshold| Constructor and Description |
|---|
JDBCAppender() |
| Modifier and Type | Method and Description |
|---|---|
void |
activateOptions()
Derived appenders should override this method if option structure
requires it.
|
void |
append(LoggingEvent event)
Adds the event to the buffer.
|
void |
close()
Closes the appender, flushing the buffer first then closing the default
connection if it is open.
|
protected void |
closeConnection(java.sql.Connection con)
Override this to return the connection to a pool, or to clean up the
resource.
|
protected void |
execute(java.lang.String sql)
Override this to provide an alertnate method of getting connections (such as
caching).
|
void |
finalize()
closes the appender before disposal
|
void |
flushBuffer()
loops through the buffer of LoggingEvents, gets a sql string from
getLogStatement() and sends it to execute().
|
private void |
flushBufferSecure() |
int |
getBufferSize() |
protected java.sql.Connection |
getConnection()
Override this to link with your connection pooling system.
|
boolean |
getLocationInfo()
Gets whether the location of the logging request call should be captured.
|
protected java.lang.String |
getLogStatement(LoggingEvent event)
By default getLogStatement sends the event to the required Layout object.
|
java.lang.String |
getPassword() |
java.lang.String |
getSql()
Returns pre-formated statement eg: insert into LogTable (msg) values ("%m")
|
java.lang.String |
getURL() |
java.lang.String |
getUser() |
boolean |
requiresLayout()
JDBCAppender requires a layout.
|
private void |
safelyInsertIntoDB(java.util.ArrayList<LoggingEvent> removes,
boolean useBatch,
java.sql.PreparedStatement ps) |
void |
setBufferSize(int newBufferSize) |
void |
setDriver(java.lang.String driverClass)
Ensures that the given driver class has been loaded for sql connection
creation.
|
void |
setLocationInfo(boolean flag)
The LocationInfo option takes a boolean value.
|
void |
setPassword(java.lang.String password) |
void |
setSql(java.lang.String s) |
void |
setURL(java.lang.String url) |
void |
setUser(java.lang.String user) |
addFilter, clearFilters, doAppend, getErrorHandler, getFilter, getFirstFilter, getLayout, getName, getThreshold, isAsSevereAsThreshold, setErrorHandler, setLayout, setName, setThresholdclone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitaddFilter, clearFilters, doAppend, getErrorHandler, getFilter, getLayout, getName, setErrorHandler, setLayout, setNameprivate static final java.lang.IllegalArgumentException ILLEGAL_PATTERN_FOR_SECURE_EXEC
protected java.lang.String databaseURL
protected java.lang.String databaseUser
protected java.lang.String databasePassword
protected java.sql.Connection connection
getConnection and closeConnection
methods.protected java.lang.String sqlStatement
private JdbcPatternParser preparedStatementParser
protected int bufferSize
protected java.util.ArrayList<LoggingEvent> buffer
private boolean locationInfo
private boolean isActive
public void activateOptions()
AppenderSkeletonactivateOptions in interface OptionHandleractivateOptions in class AppenderSkeletonpublic boolean getLocationInfo()
public void setLocationInfo(boolean flag)
flag - true if location information should be extracted.public void append(LoggingEvent event)
append in class AppenderSkeletonprotected java.lang.String getLogStatement(LoggingEvent event)
protected void execute(java.lang.String sql)
throws java.sql.SQLException
java.sql.SQLExceptionprotected void closeConnection(java.sql.Connection con)
protected java.sql.Connection getConnection()
throws java.sql.SQLException
java.sql.SQLExceptionpublic void close()
public void flushBuffer()
private void flushBufferSecure()
private void safelyInsertIntoDB(java.util.ArrayList<LoggingEvent> removes, boolean useBatch, java.sql.PreparedStatement ps) throws java.sql.SQLException
java.sql.SQLExceptionpublic void finalize()
finalize in class AppenderSkeletonpublic boolean requiresLayout()
requiresLayout in interface Appenderpublic void setSql(java.lang.String s)
public java.lang.String getSql()
public void setUser(java.lang.String user)
public void setURL(java.lang.String url)
public void setPassword(java.lang.String password)
public void setBufferSize(int newBufferSize)
public java.lang.String getUser()
public java.lang.String getURL()
public java.lang.String getPassword()
public int getBufferSize()
public void setDriver(java.lang.String driverClass)