org.apache.log4j.jdbc
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. The JDBCAppender provides for sending log events to a database.
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 automaticly 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 JDBCAppender 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.
Field Summary | |
---|---|
protected ArrayList | buffer
ArrayList holding the buffer of Logging Events. |
protected int | bufferSize
size of LoggingEvent buffer before writting to the database.
|
protected Connection | connection
Connection used by default. |
protected String | databasePassword
User to use for default connection handling |
protected String | databaseURL
URL of the DB for default connection handling |
protected String | databaseUser
User to connect as for default connection handling |
protected ArrayList | removes
Helper object for clearing out the buffer |
protected 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").
|
Constructor Summary | |
---|---|
JDBCAppender() |
Method Summary | |
---|---|
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(Connection con)
Override this to return the connection to a pool, or to clean up the
resource.
|
protected void | execute(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().
|
int | getBufferSize() |
protected Connection | getConnection()
Override this to link with your connection pooling system.
|
protected String | getLogStatement(LoggingEvent event)
By default getLogStatement sends the event to the required Layout object.
|
String | getPassword() |
String | getSql()
Returns pre-formated statement eg: insert into LogTable (msg) values ("%m") |
String | getURL() |
String | getUser() |
boolean | requiresLayout()
JDBCAppender requires a layout.
|
void | setBufferSize(int newBufferSize) |
void | setDriver(String driverClass)
Ensures that the given driver class has been loaded for sql connection
creation. |
void | setPassword(String password) |
void | setSql(String s) |
void | setURL(String url) |
void | setUser(String user) |
getConnection
and
closeConnection
methods.