java.lang.Object | ||
↳ | org.apache.log4j.AppenderSkeleton | |
↳ | org.apache.log4j.jdbc.JDBCAppender |
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:
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(LoggingEvent event)
to
produce specialized or dynamic statements. The default uses the
sql option value.
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
buffer | ArrayList holding the buffer of Logging Events. | ||||||||||
bufferSize | size of LoggingEvent buffer before writting to the database. | ||||||||||
connection | Connection used by default. | ||||||||||
databasePassword | User to use for default connection handling | ||||||||||
databaseURL | URL of the DB for default connection handling | ||||||||||
databaseUser | User to connect as for default connection handling | ||||||||||
removes | Helper object for clearing out the buffer | ||||||||||
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 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Adds the event to the buffer.
| |||||||||||
Closes the appender, flushing the buffer first then closing the default
connection if it is open.
| |||||||||||
closes the appender before disposal
| |||||||||||
loops through the buffer of LoggingEvents, gets a
sql string from getLogStatement() and sends it to execute().
| |||||||||||
Gets whether the location of the logging request call
should be captured.
| |||||||||||
Returns pre-formated statement eg: insert into LogTable (msg) values ("%m")
| |||||||||||
JDBCAppender requires a layout.
| |||||||||||
Ensures that the given driver class has been loaded for sql connection
creation.
| |||||||||||
The LocationInfo option takes a boolean value.
| |||||||||||
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Override this to return the connection to a pool, or to clean up the
resource.
| |||||||||||
Override this to provide an alertnate method of getting
connections (such as caching).
| |||||||||||
Override this to link with your connection pooling system.
| |||||||||||
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
|
ArrayList holding the buffer of Logging Events.
size of LoggingEvent buffer before writting to the database. Default is 1.
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.
User to use for default connection handling
URL of the DB for default connection handling
User to connect as for default connection handling
Helper object for clearing out the buffer
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.
Adds the event to the buffer. When full the buffer is flushed.
Closes the appender, flushing the buffer first then closing the default connection if it is open.
closes the appender before disposal
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!
Gets whether the location of the logging request call should be captured.
Returns pre-formated statement eg: insert into LogTable (msg) values ("%m")
JDBCAppender requires a layout.
Ensures that the given driver class has been loaded for sql connection creation.
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.flag | true if location information should be extracted. |
---|
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).
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.
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.
SQLException |
---|
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.