How to log the custom messages into a database using Log4j?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Mahesh_Puvvada
Posted On:   Friday, November 15, 2002 03:55 PM

I am using Log4j to log the messages to a database in my application. The database has the columns, say, Message, Login Time, Logout Time etc. Is it possible to construct the sql in config file such that the logger takes all the user inputs to insert into different columns of the database. for ex, usually, the config file will have a line log4j.appender.MyAppender.sql=INSERT INTO TEST (MESSAGE) VALUES ('%m') but, i want to have something like this log4j.appender.MyAppender.sql=INSERT INTO TESTLOG (MESSAGE, LOGINTIME, LOGOUTTIME) VALUES ('%m', Login time, Logout time) where Login time and logout time are to be passed through program. (I dont want t   More>>


I am using Log4j to log the messages to a database in my application. The database has the columns, say, Message, Login Time, Logout Time etc.


Is it possible to construct the sql in config file such that the logger takes all the user inputs to insert into different columns of the database.


for ex, usually, the config file will have a line


log4j.appender.MyAppender.sql=INSERT INTO TEST (MESSAGE) VALUES ('%m')


but, i want to have something like this


log4j.appender.MyAppender.sql=INSERT INTO TESTLOG (MESSAGE, LOGINTIME, LOGOUTTIME) VALUES ('%m', Login time, Logout time)


where Login time and logout time are to be passed through program. (I dont want to use setSql() through the program)


Is this possible?


   <<Less

Re: How to log the custom messages into a database using Log4j?

Posted By:   big_BADguy  
Posted On:   Wednesday, December 4, 2002 03:04 AM

Make your own JDBCAppender.
Attached a fully functional JDBCAppender, work well for me :)


package Base;

import org.apache.log4j.*;
import org.apache.log4j.spi.*;

import java.util.*;

import java.sql.*;



public class JDBCAppender extends org.apache.log4j.AppenderSkeleton implements org.apache.log4j.Appender {

protected Connection conn = null;
protected PreparedStatement ps = null;



public JDBCAppender() {
super();

if (layout == null)
layout = new PatternLayout("%m");
}



public void append(org.apache.log4j.spi.LoggingEvent event)
{
try
{
if (conn == null)
return;

ps = conn.prepareStatement("INSERT INTO audit_trail (login_id, log_type, log_msg) VALUES (?,?,?)");

String msg = getLogStatement(event);

if (msg == null || msg.length() < 3)
return;

int j = msg.indexOf(",", 2);
String catId = String.valueOf(msg.charAt(0));
String loginId = msg.substring(2, j);
String logMsg = msg.substring(j + 1);

ps.setString(1, loginId);
ps.setString(2, catId);
ps.setString(3, logMsg);
ps.executeUpdate();
}
catch (Exception e) {
errorHandler.error("Failed to excute sql", e, ErrorCode.FLUSH_FAILURE);
}
}



protected String getLogStatement(LoggingEvent event) {
return layout.format(event);
}



public void close()
{
try
{
if (ps != null)
ps.close();

if (conn != null && !conn.isClosed())
conn.close();
}
catch (SQLException e)
{
errorHandler.error("Error closing connection", e, ErrorCode.GENERIC_FAILURE);
}
this.closed = true;
}



public void finalize() {
close();
}



public boolean requiresLayout() {
return false;
}



public void setConnection(Connection c)
{
conn = c;
}



public Connection getConnection()
{
return conn;
}

}
About | Sitemap | Contact