public class JDBCAppender extends AppenderSkeleton implements Appender
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:
getConnection()
to pass any connection you want.
Typically this is used to enable application wide connection pooling.
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.
getLogStatement(org.apache.log4j.spi.LoggingEvent)
is no longer in use.
Modifier and Type | Field and Description |
---|---|
protected ArrayList<LoggingEvent> |
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 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").
|
closed, errorHandler, headFilter, layout, name, tailFilter, threshold
Constructor and Description |
---|
JDBCAppender() |
Modifier and Type | Method and Description |
---|---|
void |
activateOptions()
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 |
closeConnection(Connection con)
Override this to return the connection to a pool, or to clean up the resource.
|
protected void |
execute(String sql)
Override this to provide an alertnate method of getting connections (such as caching).
|
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() |
protected Connection |
getConnection()
Override this to link with your connection pooling system.
|
boolean |
getLocationInfo()
Gets whether the location of the logging request call should be captured.
|
protected String |
getLogStatement(LoggingEvent event)
As of 1.2.18.1
getLogStatement(org.apache.log4j.spi.LoggingEvent) is no longer in use. |
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) |
addFilter, clearFilters, doAppend, getErrorHandler, getFilter, getFirstFilter, getLayout, getName, getThreshold, isAsSevereAsThreshold, setErrorHandler, setLayout, setName, setThreshold
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
addFilter, clearFilters, doAppend, getErrorHandler, getFilter, getLayout, getName, setErrorHandler, setLayout, setName
protected String databaseURL
protected String databaseUser
protected String databasePassword
protected Connection connection
getConnection
and closeConnection
methods.protected String sqlStatement
Be careful of quotes in your messages!
Also see PatternLayout.
protected int bufferSize
protected ArrayList<LoggingEvent> buffer
public void activateOptions()
AppenderSkeleton
activateOptions
in interface OptionHandler
activateOptions
in class AppenderSkeleton
public boolean getLocationInfo()
public void setLocationInfo(boolean flag)
flag
- true if location information should be extracted.public void append(LoggingEvent event)
append
in class AppenderSkeleton
protected String getLogStatement(LoggingEvent event)
As of 1.2.18.1 getLogStatement(org.apache.log4j.spi.LoggingEvent)
is no longer in use.
event
- protected void execute(String sql) throws SQLException
SQLException
protected void closeConnection(Connection con)
The default behavior holds a single connection open until the appender is closed (typically when garbage collected).
protected Connection getConnection() throws SQLException
By default this creates a single connection which is held open until the object is garbage collected.
SQLException
public void close()
public void flushBuffer()
If a statement fails the LoggingEvent stays in the buffer!
public void finalize()
finalize
in class AppenderSkeleton
public boolean requiresLayout()
requiresLayout
in interface Appender
public void setSql(String s)
public String getSql()
public void setUser(String user)
public void setURL(String url)
public void setPassword(String password)
public void setBufferSize(int newBufferSize)
public String getUser()
public String getURL()
public String getPassword()
public int getBufferSize()
public void setDriver(String driverClass)
Copyright © 1999–2023 QOS.CH Sarl (Switzerland). All rights reserved.