public class

JDBCAppender

extends AppenderSkeleton
implements Appender
java.lang.Object
   ↳ org.apache.log4j.AppenderSkeleton
     ↳ org.apache.log4j.jdbc.JDBCAppender

Class Overview

The JDBCAppender provides for sending log events to a database.

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 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 getLogStatement(LoggingEvent) method allows more explicit control of the statement used for logging.

For use as a base class:

  • Override getConnection() to pass any connection you want. Typically this is used to enable application wide connection pooling.
  • Override 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.
  • Override getLogStatement(LoggingEvent event) to produce specialized or dynamic statements. The default uses the sql option value.

Summary

Fields
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").
[Expand]
Inherited Fields
From class org.apache.log4j.AppenderSkeleton
Public Constructors
JDBCAppender()
Public Methods
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.
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()
boolean getLocationInfo()
Gets whether the location of the logging request call should be captured.
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 setLocationInfo(boolean flag)
The LocationInfo option takes a boolean value.
void setPassword(String password)
void setSql(String s)
void setURL(String url)
void setUser(String user)
Protected Methods
void closeConnection(Connection con)
Override this to return the connection to a pool, or to clean up the resource.
void execute(String sql)
Override this to provide an alertnate method of getting connections (such as caching).
Connection getConnection()
Override this to link with your connection pooling system.
String getLogStatement(LoggingEvent event)
By default getLogStatement sends the event to the required Layout object.
[Expand]
Inherited Methods
From class org.apache.log4j.AppenderSkeleton
From class java.lang.Object
From interface org.apache.log4j.Appender
From interface org.apache.log4j.spi.OptionHandler

Fields

protected ArrayList buffer

ArrayList holding the buffer of Logging Events.

protected int bufferSize

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

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.

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

Public Constructors

public JDBCAppender ()

Public Methods

public void append (LoggingEvent event)

Adds the event to the buffer. When full the buffer is flushed.

public void close ()

Closes the appender, flushing the buffer first then closing the default connection if it is open.

public void finalize ()

closes the appender before disposal

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!

public int getBufferSize ()

public boolean getLocationInfo ()

Gets whether the location of the logging request call should be captured.

Returns
  • the current value of the LocationInfo option.

public String getPassword ()

public String getSql ()

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

public String getURL ()

public String getUser ()

public boolean requiresLayout ()

JDBCAppender requires a layout.

public void setBufferSize (int newBufferSize)

public void setDriver (String driverClass)

Ensures that the given driver class has been loaded for sql connection creation.

public void setLocationInfo (boolean flag)

The LocationInfo option takes a boolean value. By default, it is set to false which means there will be no effort to extract the location information related to the event. As a result, the event that will be ultimately logged will likely to contain the wrong location information (if present in the log format).

Location information extraction is comparatively very slow and should be avoided unless performance is not a concern.

Parameters
flag true if location information should be extracted.

public void setPassword (String password)

public void setSql (String s)

public void setURL (String url)

public void setUser (String user)

Protected Methods

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

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.

Throws
SQLException

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.

Throws
SQLException

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.