2008-03-25
答复: 用log4j把日志异步写入数据库中
关键字: log4j
http://www.javaeye.com/topic/172125
BufferSize没什么作用,JDBCAppender
只是把用户输出的log现在一个ArrayList中保存,当其数量达到了BufferSize,才启动写日志。而且JDBCAppender还是每次都生成Connection,每一个Log还是单独写入,不是批量写入。
Log4J的数据库写入方式就是一个鸡肋,如果需要提高性能,首先,自己需要实现一个连接池。而且其还不支持addBatch,赫赫,如下是log4j源代码(JDBCAppender.java)
########################
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();
}
###########################
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);
}
BufferSize没什么作用,JDBCAppender
只是把用户输出的log现在一个ArrayList中保存,当其数量达到了BufferSize,才启动写日志。而且JDBCAppender还是每次都生成Connection,每一个Log还是单独写入,不是批量写入。
Log4J的数据库写入方式就是一个鸡肋,如果需要提高性能,首先,自己需要实现一个连接池。而且其还不支持addBatch,赫赫,如下是log4j源代码(JDBCAppender.java)
########################
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();
}
###########################
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);
}
发表评论
- 浏览: 33041 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
最近加入圈子
链接
最新评论
-
一位台湾校长的震动所有中 ...
如果这样的校长能占到中国所有学校的千分之一,今日的中国就不会是现在的这种样子了。
-- by bnmcvzx -
关于memcachefs
是采用memcached作为存储介质的一种文件系统。memcached可以运行于 ...
-- by masterkey -
关于memcachefs
和普通基于内存的文件系统相比有什么特点?
-- by 都别装了 -
nginx 0.7.1 release
我发现discuz已经在用nginx7.0,那说明这个版本已经完全可以运用于商业 ...
-- by Arden -
nginx 0.7.1 release
这是开发版
-- by 剑 事






评论排行榜