|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.apache.log4j.AppenderSkeleton | +--org.apache.log4j.jdbcplus.JDBCAppender
The JDBCAppender writes messages into a database via JDBC. Multiple configuration options and parameters are supported.
Configuration by file/code
The JDBCAppender is configurable at runtime by setting options in two alternatives :
Here is a another code-example ( SourceConfigOracleTest.java ) to configure the JDBCAppender without a configuration-file.
Configuration parameters
Generally there are just a few basic things to get the JDBCAppender running :
We need a database connection anyway ! There are 3 ways to to this :
a) Specify the URL, password and username (use the url-param, username-param and password-param or the setUrl(), setUsername() and setPassword()-methods) This will create a static connection by JDBCAppender.
b) Specify a JDBCConnectionHandler (use the connector-param or the setConnector()-method) This handler will be called to return a connection. When its a JDBCPoolConnectionHandler, the connection is checked in and out everytime needing it.
c) Specify URL, password and username AND a JDBCConnectionHandler, then the handler returns the desired connection.
We need to know the statement which should be inserted into the database ! There are also 5 ways to to this :
a) Specify a JDBCSqlHandler (use the sqlhandler-param or the setSqlhandler()-method) This handler will be invoked with every message which has to be logged and must return a SQL-statement. It may return null or empty string if nothing should be logged.
b) Specify a static sql-statement (use the sql-param or the setSql()-method) This statement which will be performed with every occuring message-event. Use wildcards @INC@, @PRIO@, @IPRIO@, @CAT@, @THREAD@, @MSG@, @LAYOUT@, @LAYOUT:x@, @TIMESTAMP@, @THROWABLE@, @NDC@, @MDC:key@ for dynamic replacement. See further description below.
c) Specify the table and describe the important columns. (use the table-param and column-param or the setTable() and setColumn()-methods) Not nullable columns are mandatory to describe ! The sql-statement will be created automatically. We need to know exactly the column-name, the logical columns-logtype and in dependency of that the value. The constants of JDBCLogType are described below. This is the most dynamic variant to build the sql-statement, because you can apply JDBCIDHandler and JDBCColumnHandler. Note: This configuration uses the "updatable ResultSets". This feature is not implemented for some databases/drivers. As of 2004, it causes errors on MySQL JDBC Connector ("Result Set not updateable"), Firebird JCA-JDBC Driver ("not yet implemented"), Postgres. Use other configuration options instead if any problems occur.
d) Same as 2c) but using PreparedStatements. Example: log4j_test_prepstmt.xml
e) Same as 2c) but using Stored Procedure (CallableStatement). Specify procedure name parameter instead of table name. Requires J2SDK 1.4+. Example: log4j_test_storedproc.xml
The class JDBCLogType provides you several possibilities to describe a columns logtype/wildcard :
Field Summary |
Fields inherited from class org.apache.log4j.AppenderSkeleton |
closed, errorHandler, headFilter, layout, name, tailFilter, threshold |
Constructor Summary | |
JDBCAppender()
Constructor for the JDBCAppender object |
|
JDBCAppender(Layout layout)
Constructor for the JDBCAppender object |
Method Summary | |
void |
append(LoggingEvent event)
Internal method. |
void |
close()
Internal method. |
protected boolean |
configure()
Internal method. |
protected void |
connect()
Internal method. |
void |
finalize()
If program terminates close the database-connection and flush the buffer |
void |
flush_buffer()
Internal method. |
String |
getBuffer()
Gets the Buffer attribute of the JDBCAppender object |
boolean |
getCommit()
Gets the Commit attribute of the JDBCAppender object |
JDBCConnectionHandler |
getConnectionHandler()
Gets the ConnectionHandler attribute of the JDBCAppender object |
String |
getConnector()
Gets the Connector attribute of the JDBCAppender object |
String |
getDbclass()
Gets the class loader string |
String |
getLayoutPartsDelimiter()
character to separate parts of layout if more than one part is to be used |
String |
getPassword()
Gets the Password attribute of the JDBCAppender object |
String |
getProcedure()
Returns the procedure. |
String |
getSql()
Gets the Sql attribute of the JDBCAppender object |
String |
getTable()
Gets the Table attribute of the JDBCAppender object |
int |
getThrowableMaxChars()
Returns the maximum number of characters in throwable/exception stack trace. |
String |
getUrl()
Gets the Url attribute of the JDBCAppender object |
String |
getUsername()
Gets the Username attribute of the JDBCAppender object |
boolean |
isQuoteReplace()
Define whether to replace single quotes (') by 2 single quotes ('') in sql parameters. |
boolean |
isUsePreparedStatements()
|
boolean |
ready()
Internal method. |
boolean |
requiresLayout()
Internal method. |
void |
setBuffer(String value)
Defines how many messages will be buffered until they will be updated to the database. |
void |
setColumn(String value)
Defines one colum in the format column~logtype~value~typeName~sqlType, e.g. |
void |
setColumn(String column,
int logtype,
Object value,
String type,
int sqlType)
Sets the Column attribute of the JDBCAppender object. |
void |
setCommit(boolean value)
Defines whether updated messages should be committed to the database. |
void |
setConnectionHandler(JDBCConnectionHandler connectionHandler)
Specify your own JDBCConnectionHandler / JDBCPoolConnectionHandler for connecting to the database. |
void |
setConnector(String value)
Specify your own JDBCConnectionHandler / JDBCPoolConnectionHandler for connecting to the database. |
void |
setDbclass(String value)
Specify a database class loader string. |
void |
setLayout(Layout layout)
Sets the Layout attribute of the JDBCAppender object |
void |
setLayoutPartsDelimiter(String c)
character to separate parts of layout if more than one part is to be used |
void |
setPassword(String value)
Sets the database password |
void |
setProcedure(String procedure)
Stores the procedure which will be called. |
void |
setQuoteReplace(boolean b)
Define whether to replace single quotes (') by 2 single quotes ('') in sql parameters. |
void |
setSql(String value)
Specify an sql-statement. |
void |
setSqlHandler(JDBCSqlHandler sqlHandler)
Specify your own JDBCSqlHandler to let him create dynamic sql-statements. |
void |
setSqlhandler(String value)
Specify your own JDBCSqlHandler to let him create dynamic sql-statements. |
void |
setTable(String value)
Specify the table, when you also describe all columns by setColumn() |
void |
setThrowableMaxChars(int throwableMaxChars)
Sets the maximum number of characters in throwable/exception stack trace. |
void |
setUrl(String value)
Sets database url of the form jdbc:subprotocol:subname |
void |
setUsePreparedStatements(boolean usePreparedStatements)
Defines whether to use Prepared Statements instead of updateable result sets (default false). |
void |
setUsername(String value)
Sets the database user |
Methods inherited from class org.apache.log4j.AppenderSkeleton |
activateOptions, addFilter, clearFilters, doAppend, getErrorHandler, getFilter, getFirstFilter, getLayout, getName, getThreshold, isAsSevereAsThreshold, setErrorHandler, setName, setThreshold |
Methods inherited from class java.lang.Object |
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public JDBCAppender()
public JDBCAppender(Layout layout)
layout
- Allows you to set your Layout-instanceMethod Detail |
public void setLayout(Layout layout)
setLayout
in interface Appender
setLayout
in class AppenderSkeleton
layout
- The new Layout valuepublic void setConnector(String value)
value
- The new Connector valuepublic void setConnectionHandler(JDBCConnectionHandler connectionHandler)
public void setSqlhandler(String value)
value
- The new Sqlhandler valuepublic void setSqlHandler(JDBCSqlHandler sqlHandler) throws Exception
Exception
public void setUrl(String value)
value
- The new Url valuepublic void setUsername(String value)
value
- The new Username valuepublic void setPassword(String value)
value
- The new Password valuepublic void setDbclass(String value)
value
- The new database driver classpublic void setSql(String value)
value
- The new Sql valuepublic void setTable(String value)
value
- The new Table valuepublic void setColumn(String column, int logtype, Object value, String type, int sqlType)
column
- The columns namelogtype
- Constant of class JDBCLogTypevalue
- The columns value depending by the logtypetype
- Column type namesqlType
- Column SQL typepublic void setColumn(String value)
value
- Concatenated string column~logtype~value~typeNamepublic void setBuffer(String value)
value
- The new Buffer valuepublic void setCommit(boolean value)
value
- The new Commit valuepublic JDBCConnectionHandler getConnectionHandler()
public String getConnector()
public String getUrl()
public String getUsername()
public String getPassword()
public String getSql()
public String getDbclass()
public String getTable()
public String getBuffer()
public boolean getCommit()
public void finalize()
finalize
in class AppenderSkeleton
public boolean requiresLayout()
public void close()
public void append(LoggingEvent event)
append
in class AppenderSkeleton
event
- Description of Parameterpublic void flush_buffer()
public boolean ready()
protected void connect() throws Exception
Exception
- Description of Exceptionprotected boolean configure()
public boolean isQuoteReplace()
public void setQuoteReplace(boolean b)
b
- public String getLayoutPartsDelimiter()
public void setLayoutPartsDelimiter(String c)
c
- public boolean isUsePreparedStatements()
public void setUsePreparedStatements(boolean usePreparedStatements)
usePreparedStatements
- The usePreparedStatements to set.public String getProcedure()
public void setProcedure(String procedure)
procedure
- The procedure to set.public int getThrowableMaxChars()
public void setThrowableMaxChars(int throwableMaxChars)
throwableMaxChars
- The throwableMaxChars to set.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |