org.apache.log4j.jdbc

Class JDBCAppender

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:

Author: Kevin Steppe (ksteppe@pacbell.net)

Field Summary
protected ArrayListbuffer
ArrayList holding the buffer of Logging Events.
protected intbufferSize
size of LoggingEvent buffer before writting to the database.
protected Connectionconnection
Connection used by default.
protected StringdatabasePassword
User to use for default connection handling
protected StringdatabaseURL
URL of the DB for default connection handling
protected StringdatabaseUser
User to connect as for default connection handling
protected ArrayListremoves
Helper object for clearing out the buffer
protected StringsqlStatement
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
voidappend(LoggingEvent event)
Adds the event to the buffer.
voidclose()
Closes the appender, flushing the buffer first then closing the default connection if it is open.
protected voidcloseConnection(Connection con)
Override this to return the connection to a pool, or to clean up the resource.
protected voidexecute(String sql)
Override this to provide an alertnate method of getting connections (such as caching).
voidfinalize()
closes the appender before disposal
voidflushBuffer()
loops through the buffer of LoggingEvents, gets a sql string from getLogStatement() and sends it to execute().
intgetBufferSize()
protected ConnectiongetConnection()
Override this to link with your connection pooling system.
protected StringgetLogStatement(LoggingEvent event)
By default getLogStatement sends the event to the required Layout object.
StringgetPassword()
StringgetSql()
Returns pre-formated statement eg: insert into LogTable (msg) values ("%m")
StringgetURL()
StringgetUser()
booleanrequiresLayout()
JDBCAppender requires a layout.
voidsetBufferSize(int newBufferSize)
voidsetDriver(String driverClass)
Ensures that the given driver class has been loaded for sql connection creation.
voidsetPassword(String password)
voidsetSql(String s)
voidsetURL(String url)
voidsetUser(String user)

Field Detail

buffer

protected ArrayList buffer
ArrayList holding the buffer of Logging Events.

bufferSize

protected int bufferSize
size of LoggingEvent buffer before writting to the database. Default is 1.

connection

protected Connection connection
Connection 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.

databasePassword

protected String databasePassword
User to use for default connection handling

databaseURL

protected String databaseURL
URL of the DB for default connection handling

databaseUser

protected String databaseUser
User to connect as for default connection handling

removes

protected ArrayList removes
Helper object for clearing out the buffer

sqlStatement

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"). Be careful of quotes in your messages! Also see PatternLayout.

Constructor Detail

JDBCAppender

public JDBCAppender()

Method Detail

append

public void append(LoggingEvent event)
Adds the event to the buffer. When full the buffer is flushed.

close

public void close()
Closes the appender, flushing the buffer first then closing the default connection if it is open.

closeConnection

protected void closeConnection(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).

execute

protected void execute(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.

finalize

public void finalize()
closes the appender before disposal

flushBuffer

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!

getBufferSize

public int getBufferSize()

getConnection

protected 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.

getLogStatement

protected String getLogStatement(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.

getPassword

public String getPassword()

getSql

public String getSql()
Returns pre-formated statement eg: insert into LogTable (msg) values ("%m")

getURL

public String getURL()

getUser

public String getUser()

requiresLayout

public boolean requiresLayout()
JDBCAppender requires a layout.

setBufferSize

public void setBufferSize(int newBufferSize)

setDriver

public void setDriver(String driverClass)
Ensures that the given driver class has been loaded for sql connection creation.

setPassword

public void setPassword(String password)

setSql

public void setSql(String s)

setURL

public void setURL(String url)

setUser

public void setUser(String user)
Copyright 2000-2005 Apache Software Foundation.