Log4j Version 1.2.14: Class JDBCAppender

Log4j 1.2

Log4j 1.2.14

org.apache.log4j.jdbc Class JDBCAppender

java.lang.Object
  |
  +--org.apache.log4j.AppenderSkeleton
        |
        +--org.apache.log4j.jdbc.JDBCAppender
All Implemented Interfaces:
Appender, OptionHandler

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 getLogStatement(org.apache.log4j.spi.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.

Author:
Kevin Steppe ([email protected])

protected  ArrayList
protected  int
protected  Connection
protected  String
protected  String
protected  String
protected  ArrayList
protected  String
 
 
 
 void
 void
protected  void
protected  void
 void
 void
 int
protected  Connection
protected  String
 String
 String
 String
 String
 boolean
 void
 void
 void
 void
 void
 void
 
 
 
 

databaseURL

protected String databaseURL
URL of the DB for default connection handling

databaseUser

protected String databaseUser
User to connect as for default connection handling

databasePassword

protected String databasePassword
User to use for default connection handling

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.

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.

bufferSize

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

buffer

protected ArrayList buffer
ArrayList holding the buffer of Logging Events.

removes

protected ArrayList removes
Helper object for clearing out the buffer

JDBCAppender

public JDBCAppender()

append

public void append(LoggingEvent event)
Adds the event to the buffer. When full the buffer is flushed.
Overrides:
append in class AppenderSkeleton

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.

execute

protected void execute(String sql)
                throws SQLException
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.

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

getConnection

protected Connection getConnection()
                            throws SQLException
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.

close

public void close()
Closes the appender, flushing the buffer first then closing the default connection if it is open.
Specified by:
close in interface Appender

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!

finalize

public void finalize()
closes the appender before disposal
Overrides:
finalize in class AppenderSkeleton

requiresLayout

public boolean requiresLayout()
JDBCAppender requires a layout.
Specified by:
requiresLayout in interface Appender

setSql

public void setSql(String s)

getSql

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

setUser

public void setUser(String user)

setURL

public void setURL(String url)

setPassword

public void setPassword(String password)

setBufferSize

public void setBufferSize(int newBufferSize)

getUser

public String getUser()

getURL

public String getURL()

getPassword

public String getPassword()

getBufferSize

public int getBufferSize()

setDriver

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

Log4j 1.2.14

Copyright 2000-2005 Apache Software Foundation.