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
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
ArrayListbuffer. 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 aPatternLayout(either created automaticly by the appender or added by the user). Therefore by default all the conversion patterns inPatternLayoutcan 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 implementcloseConnectionto 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 (ksteppe@pacbell.net)
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.ArrayListbufferArrayList holding the buffer of Logging Events.protected intbufferSizesize of LoggingEvent buffer before writting to the database.protected java.sql.ConnectionconnectionConnection used by default.protected java.lang.StringdatabasePasswordUser to use for default connection handlingprotected java.lang.StringdatabaseURLURL of the DB for default connection handlingprotected java.lang.StringdatabaseUserUser to connect as for default connection handlingprotected java.util.ArrayListremovesHelper object for clearing out the bufferprotected java.lang.StringsqlStatementStores the string given to the pattern layout for conversion into a SQL statement, eg: insert into LogTable (Thread, Class, Message) values ("%t", "%c", "%m").-
Fields inherited from class org.apache.log4j.AppenderSkeleton
closed, errorHandler, headFilter, layout, name, tailFilter, threshold
-
-
Constructor Summary
Constructors Constructor Description JDBCAppender()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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(java.sql.Connection con)Override this to return the connection to a pool, or to clean up the resource.protected voidexecute(java.lang.String sql)Override this to provide an alertnate method of getting connections (such as caching).voidfinalize()closes the appender before disposalvoidflushBuffer()loops through the buffer of LoggingEvents, gets a sql string from getLogStatement() and sends it to execute().intgetBufferSize()protected java.sql.ConnectiongetConnection()Override this to link with your connection pooling system.booleangetLocationInfo()Gets whether the location of the logging request call should be captured.protected java.lang.StringgetLogStatement(LoggingEvent event)By default getLogStatement sends the event to the required Layout object.java.lang.StringgetPassword()java.lang.StringgetSql()Returns pre-formated statement eg: insert into LogTable (msg) values ("%m")java.lang.StringgetURL()java.lang.StringgetUser()booleanrequiresLayout()JDBCAppender requires a layout.voidsetBufferSize(int newBufferSize)voidsetDriver(java.lang.String driverClass)Ensures that the given driver class has been loaded for sql connection creation.voidsetLocationInfo(boolean flag)The LocationInfo option takes a boolean value.voidsetPassword(java.lang.String password)voidsetSql(java.lang.String s)voidsetURL(java.lang.String url)voidsetUser(java.lang.String user)-
Methods inherited from class org.apache.log4j.AppenderSkeleton
activateOptions, addFilter, clearFilters, doAppend, getErrorHandler, getFilter, getFirstFilter, getLayout, getName, getThreshold, isAsSevereAsThreshold, setErrorHandler, setLayout, setName, setThreshold
-
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.log4j.Appender
addFilter, clearFilters, doAppend, getErrorHandler, getFilter, getLayout, getName, setErrorHandler, setLayout, setName
-
-
-
-
Field Detail
-
databaseURL
protected java.lang.String databaseURL
URL of the DB for default connection handling
-
databaseUser
protected java.lang.String databaseUser
User to connect as for default connection handling
-
databasePassword
protected java.lang.String databasePassword
User to use for default connection handling
-
connection
protected java.sql.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 thegetConnectionandcloseConnectionmethods.
-
sqlStatement
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"). 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 java.util.ArrayList buffer
ArrayList holding the buffer of Logging Events.
-
removes
protected java.util.ArrayList removes
Helper object for clearing out the buffer
-
-
Method Detail
-
getLocationInfo
public boolean getLocationInfo()
Gets whether the location of the logging request call should be captured.- Returns:
- the current value of the LocationInfo option.
- Since:
- 1.2.16
-
setLocationInfo
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.- Since:
- 1.2.16
-
append
public void append(LoggingEvent event)
Adds the event to the buffer. When full the buffer is flushed.- Specified by:
appendin classAppenderSkeleton
-
getLogStatement
protected java.lang.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(java.lang.String sql) throws java.sql.SQLExceptionOverride 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:
java.sql.SQLException
-
closeConnection
protected void closeConnection(java.sql.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 java.sql.Connection getConnection() throws java.sql.SQLExceptionOverride 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:
java.sql.SQLException
-
close
public void close()
Closes the appender, flushing the buffer first then closing the default connection if it is open.
-
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:
finalizein classAppenderSkeleton
-
requiresLayout
public boolean requiresLayout()
JDBCAppender requires a layout.- Specified by:
requiresLayoutin interfaceAppender
-
setSql
public void setSql(java.lang.String s)
-
getSql
public java.lang.String getSql()
Returns pre-formated statement eg: insert into LogTable (msg) values ("%m")
-
setUser
public void setUser(java.lang.String user)
-
setURL
public void setURL(java.lang.String url)
-
setPassword
public void setPassword(java.lang.String password)
-
setBufferSize
public void setBufferSize(int newBufferSize)
-
getUser
public java.lang.String getUser()
-
getURL
public java.lang.String getURL()
-
getPassword
public java.lang.String getPassword()
-
getBufferSize
public int getBufferSize()
-
setDriver
public void setDriver(java.lang.String driverClass)
Ensures that the given driver class has been loaded for sql connection creation.
-
-