Class JDBCAppender
- All Implemented Interfaces:
Appender
,OptionHandler
JDBCAppender shipping in 1.2.18.2 produces
PreparedStatement
instances.
Thanks to the remarkable work of Vladimir Sitnikov JDBCAppender now
interprets the SQL expression on the fly and inserts new events using
PreparedStartement instances. Note that the table column types are restricted
to those types compatible with Java's String.
Versions 1.2.18.0 and prior are vulnerable to SQL injection attacks..
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 fashion.
The setSql(String sql)
sets the SQL statement to be used for
logging -- this statement is sent to a PatternLayout
(either
created automatically 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).
As mentioned earlier, the produced
SQL is translated in an additional phase to use PreparedStatement
.
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 implementcloseConnection
to handle the connection you generated. Typically this would return the connection to the pool it came from. - As of 1.2.18.1
getLogStatement(org.apache.log4j.spi.LoggingEvent)
is no longer in use.
- Author:
- Kevin Steppe (ksteppe@pacbell.net)
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ArrayList<LoggingEvent>
ArrayList holding the buffer of Logging Events.protected int
size of LoggingEvent buffer before writting to the database.protected Connection
Connection used by default.protected String
User to use for default connection handlingprotected String
URL of the DB for default connection handlingprotected String
User to connect as for default connection handlingprotected String
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").Fields inherited from class org.apache.log4j.AppenderSkeleton
closed, errorHandler, headFilter, layout, name, tailFilter, threshold
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Derived appenders should override this method if option structure requires it.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
Override this to return the connection to a pool, or to clean up the resource.protected void
Override this to provide an alertnate method of getting connections (such as caching).void
finalize()
closes the appender before disposalvoid
loops through the buffer of LoggingEvents, gets a sql string from getLogStatement() and sends it to execute().int
protected Connection
Override this to link with your connection pooling system.boolean
Gets whether the location of the logging request call should be captured.protected String
getLogStatement
(LoggingEvent event) As of 1.2.18.1getLogStatement(org.apache.log4j.spi.LoggingEvent)
is no longer in use.getSql()
Returns pre-formated statement eg: insert into LogTable (msg) values ("%m")getURL()
getUser()
boolean
JDBCAppender requires a layout.void
setBufferSize
(int newBufferSize) void
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
void
void
Methods inherited from class org.apache.log4j.AppenderSkeleton
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 Details
-
databaseURL
URL of the DB for default connection handling -
databaseUser
User to connect as for default connection handling -
databasePassword
User to use for default connection handling -
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 thegetConnection
andcloseConnection
methods. -
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 bufferSizesize of LoggingEvent buffer before writting to the database. Default is 1. -
buffer
ArrayList holding the buffer of Logging Events.
-
-
Constructor Details
-
JDBCAppender
public JDBCAppender()
-
-
Method Details
-
activateOptions
public void activateOptions()Description copied from class:AppenderSkeleton
Derived appenders should override this method if option structure requires it.- Specified by:
activateOptions
in interfaceOptionHandler
- Overrides:
activateOptions
in classAppenderSkeleton
-
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
Adds the event to the buffer. When full the buffer is flushed.- Specified by:
append
in classAppenderSkeleton
-
getLogStatement
As of 1.2.18.1
getLogStatement(org.apache.log4j.spi.LoggingEvent)
is no longer in use.- Parameters:
event
-
-
execute
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
-
closeConnection
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
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
-
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:
finalize
in classAppenderSkeleton
-
requiresLayout
public boolean requiresLayout()JDBCAppender requires a layout.- Specified by:
requiresLayout
in interfaceAppender
-
setSql
-
getSql
Returns pre-formated statement eg: insert into LogTable (msg) values ("%m") -
setUser
-
setURL
-
setPassword
-
setBufferSize
public void setBufferSize(int newBufferSize) -
getUser
-
getURL
-
getPassword
-
getBufferSize
public int getBufferSize() -
setDriver
Ensures that the given driver class has been loaded for sql connection creation.
-