Data | database | Database connection We all know that log4j is an excellent open source logging project, we can not only customize the format of the log output, but also define the destination of the log output, such as: screen, text file, database, even through the socket output.
Now let's configure the log output to the database
I do, but did not run successfully, and this method is to use the traditional database connection method, for the database management and inefficient, in the current era of the connection pool, why we can not give the log4j with the connection pool, let log4j use the data connection pool connection and database communication. Now look at the log4j API and find Jdbcappender This class has the following paragraphs: Warning:this version of Jdbcappender is very likely to being completely in the Future. Moreoever, it does not log exceptions. The Jdbcappender provides for sending log events to a database.
For use as a base class:
Override getconnection () to the pass no connection you want. Typically this are used to enable application wide connection pooling.
Override 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.
Override getlogstatement (Loggingevent event) to produce specialized or dynamic statements. The default uses the SQL option value.
Originally log4j recommended that we use the jdbcappender provided as a base class, and then override the method of three parent classes: getconnection (), CloseConnection (Connection con) and Getlogstatement (Loggingevent event).
So, write a subclass of Jdbcpoolappender to replace this jdbcappender.
The Jdbcpoolappender code and its associated code are as follows:
public class Jdbcpoolappender extends Org.apache.log4j.jdbc.JDBCAppender {
Private MyDB MyDB = null;
Protected String sqlname= ""; Increase the properties of a database Jndiname
protected Connection Connection = null;
protected String SQLStatement = "";
/**
* Size of loggingevent buffer before writting to the database.
* Default is 1.
*/
protected int buffersize = 1;
Public Jdbcpoolappender () {
Super ();
}
/**
* ArrayList holding the buffer of Logging Events.
*/
public void Append (Loggingevent event) {
Buffer.add (event);
if (Buffer.size () >= buffersize)
Flushbuffer ();
}
/**
* By default Getlogstatement sends the event to the required Layout object.
* The layout would format the given into a workable SQL string.
*
* Overriding this provides direct access to the Loggingevent
* When constructing the logging statement.
*
*/
Protected String Getlogstatement (Loggingevent event) {
Return GetLayout (). Format (event);
}
/**
*
* Override this to provide a alertnate method of getting
* Connections (such as caching). One method to the fix is to open
* Connections at the "start of" Flushbuffer () and close them at the
* End. I use a connection pool outside the Jdbcappender which is
* Accessed in a override of this method.
* */
protected void execute (String sql) throws SQLException {
Connection con = null;
Statement stmt = null;
try {
con = getconnection ();
stmt = Con.createstatement ();
Stmt.executeupdate (SQL);
catch (SQLException e) {
if (stmt!= null)
Stmt.close ();
Throw e;
}
Stmt.close ();
CloseConnection (con);
System.out.println ("Execute:" + sql);
}
/**
* Override this to return the connection to a pool
* Resource.
*
* The default behavior holds a single connection open until the Appender
* is closed (typically when garbage collected).
*/
protected void CloseConnection (Connection con) {
Mydb=null;
try {
if (connection!= null &&!connection.isclosed ())
Connection.close ();
catch (SQLException e) {
Errorhandler.error ("Error closing connection", E,
Errorcode.generic_failure);
}
}
/**
* Override This function to return a Connetion object using the connection pool
*
*/
Protected Connection getconnection () throws SQLException {
try {
MyDB = Generaldb.getinstance (sqlname);
Connection = Mydb.getconnection ();
catch (Exception e) {
Errorhandler.error ("Error opening connection", E, errorcode.generic_failure);
}
return connection;
}
/**
* Closes the Appender, flushing the buffer-a-then closing the default
* Connection if it is open.
*/
public void Close () {
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!
*/
public void Flushbuffer () {
Do the actual logging
Removes.ensurecapacity (Buffer.size ());
for (Iterator i = Buffer.iterator (); I.hasnext ();) {
try {
Loggingevent LogEvent = (loggingevent) i.next ();
String sql = getlogstatement (logevent);
Execute (SQL);
Removes.add (LogEvent);
catch (SQLException e) {
Errorhandler.error ("Failed to Excute SQL", E,
Errorcode.flush_failure);
}
}
Remove from the ' buffer any events ' that were reported
Buffer.removeall (removes);
Clear the buffer of reported events
Removes.clear ();
}
/** closes the appender before disposal * *
public void Finalize () {
Close ();
}
/**
* Jdbcappender requires a layout.
* */
public Boolean requireslayout () {
return true;
}
Run successfully! For Jdbcpoolappender properties (such as the Sqlname attribute) we can add it by using the log4j reflection mechanism, as long as the configuration file attaches a value to it and the original parent class has some properties (username what) and its get, The set method is deleted because it is not required in the connection pool. And in the Jdbcpoolappender class, I just override the Getconnection method, in which we can generate our connection objects as needed, and the other two ways you can decide how to override according to your needs. :)
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.