import java.sql.SQLException;
import java.util.Date;
import java.util.Iterator;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.jdbc.JDBCAppender;
import org.apache.log4j.spi.ErrorCode;
import org.apache.log4j.spi.LoggingEvent;
import com.choice.core.beans.AccessLogs;
import com.choice.core.util.DateUtils;
public class MysqlAppender extends JDBCAppender {
private static String sqlHeader = "insert into access_log " + "(eventid,jigoubh,shenfenzh,method,inxml,outxml,"
+ "state,comment,intime,outtime,consume) values ";
public MysqlAppender() {
super();
super.setBufferSize(10);#设置缓存的大小,数组大小达到这个size再进行日志的提交
}
public void flushBuffer() {
removes.ensureCapacity(buffer.size());
int count = 0;
String sql = "";
for (Iterator i = buffer.iterator(); i.hasNext();) {
try {
LoggingEvent logEvent = (LoggingEvent) i.next();
if (logEvent.getLoggerName().contains("com.choice.core.controller")) {
Object obj = logEvent.getMessage();
AccessLogs al = (AccessLogs) logEvent.getMessage();
String comment = "";
if (StringUtils.isNotBlank(al.getComment())) {
comment = al.getComment();
}
String eventId = "";
if (StringUtils.isNotBlank(al.getEventid())) {
eventId = al.getEventid();
}
String inXml = "";
if (StringUtils.isNotBlank(al.getInxml())) {
inXml = al.getInxml();
}
String jigoubh = "";
if (StringUtils.isNotBlank(al.getJigoubh())) {
jigoubh = al.getJigoubh();
}
String method = "";
if (StringUtils.isNotBlank(al.getMethod())) {
method = al.getMethod();
}
String outxml = "";
if (StringUtils.isNotBlank(al.getOutxml())) {
outxml = al.getOutxml();
}
String shenfenzh = "";
if (StringUtils.isNotBlank(al.getShenfenzh())) {
shenfenzh = al.getShenfenzh();
}
int state = 0;
if (al.getState() != 0) {
state = al.getState();
}
long consume = 0;
if (al.getConsume() != 0) {
consume = al.getConsume();
}
Date inDate = null;
if (al.getInDate() == null) {
inDate = new Date();
} else {
inDate = al.getInDate();
}
Date outDate = null;
if (al.getOutDate() == null) {
outDate = new Date();
} else {
outDate = al.getOutDate();
}
if (count == 0) {
sql = sqlHeader + "('" + eventId + "','" + jigoubh + "','" + shenfenzh + "','" + method + "','"
+ inXml + "','" + outxml + "','" + state + "','" + comment + "','"
+ DateUtils.format(inDate, "yyyy-MM-dd HH:mm:ss") + "','"
+ DateUtils.format(outDate, "yyyy-MM-dd HH:mm:ss") + "','" + consume + "')";
} else {
sql = sql + ",('" + eventId + "','" + jigoubh + "','" + shenfenzh + "','" + method + "','"
+ inXml + "','" + outxml + "','" + state + "','" + comment + "','"
+ DateUtils.format(inDate, "yyyy-MM-dd HH:mm:ss") + "','"
+ DateUtils.format(outDate, "yyyy-MM-dd HH:mm:ss") + "','" + consume + "')";
}
if (!i.hasNext()) {
System.out.println(sql);
execute(sql);
}
count++;
removes.add(logEvent);
}
} catch (SQLException e) {
errorHandler.error("Failed to excute sql", e, ErrorCode.FLUSH_FAILURE);
}
}
buffer.removeAll(removes);
removes.clear();
}
}
=============================================================================================
log4j.properties文件配置如下:
log4j.rootLogger = DATABASE
log4j.appender.DATABASE = com.choice.core.appender.MysqlAppender#重点,指向重写的类
log4j.appender.DATABASE.Driver = com.mysql.jdbc.Driver
log4j.appender.DATABASE.URL = jdbc:mysql://127.0.0.1:3306/rhit?useUnicode\=true&characterEncoding\=utf-8&zeroDateTimeBehavior\=convertToNull&transformedBitIsBoolean\=true&allowMultiQueries\=true&autoReconnect\=true
log4j.appender.DATABASE.User = rhit
log4j.appender.DATABASE.Password = rhit123
log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout
log4j.appender.DATABASE.layout.ConversionPattern=%m