Log4j的日志记录功能已经在Java项目被广泛使用,不但可以记录在文件中,而且通过org.apache.log4j.jdbc.JDBCAppender记录在数据库中。但是有时候我们还需要从Message串中提取不同的信息,存到相应的数据库栏位中。如,我在HeJian项目中需要Message信息中包括了操作人员ID和执行的动作,我需要把两个信息分解开来存到不同的栏位,这个时候就需要扩展JDBCAppender.
例子如下:
import java.io.UnsupportedEncodingException;import java.net.URLDecoder;import java.net.URLEncoder;import java.sql.SQLException;import java.util.Iterator;
import org.apache.log4j.jdbc.JDBCAppender;import org.apache.log4j.spi.ErrorCode;import org.apache.log4j.spi.LoggingEvent;
public class ExtendsLog4j extends JDBCAppender {
//处理默认SQL语句,使之成为你需要的SQL语句public String FormatSql(String s) { System.out.println(s); String tmpStr = s.substring(0, s.lastIndexOf("$")); tmpStr = tmpStr.substring(tmpStr.lastIndexOf("$") + 1); String array[] = tmpStr.split(" - "); System.out.println(array.length + " " + array[1]); if (array.length > 1) { s = replaceFirst(s, tmpStr, array[0]); s = replaceFirst(s, "$" + tmpStr + "$", array[1].replaceAll("'", "''")); } else { s = replaceFirst(s, tmpStr, ""); s = replaceFirst(s, "$" + tmpStr + "$", array[0].replaceAll("'", "''")); } System.out.println(s); return s;}
public void flushBuffer() { removes.ensureCapacity(buffer.size()); for (Iterator i = buffer.iterator(); i.hasNext();) { try { LoggingEvent logEvent = (LoggingEvent) i.next(); String sql = getLogStatement(logEvent); sql = FormatSql(sql); if (!sql.equals("")) { 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();}
public String[] split(String source, String div) { int arynum = 0; int intIdx = 0; int intIdex = 0; int div_length = div.length(); if (source.compareTo("") != 0) { if (source.indexOf(div) != -1) { intIdx = source.indexOf(div); int intCount = 1; do { if (source.indexOf(div, intIdx + div_length) != -1) { intIdx = source.indexOf(div, intIdx + div_length); arynum = intCount; } else { arynum += 2; break; } intCount++; } while (true); } else { arynum = 1; } } else { arynum = 0; } intIdx = 0; intIdex = 0; String returnStr[] = new String[arynum]; if (source.compareTo("") != 0) { if (source.indexOf(div) != -1) { intIdx = source.indexOf(div); returnStr[0] = source.substring(0, intIdx); int intCount = 1; do { if (source.indexOf(div, intIdx + div_length) != -1) { intIdex = source.indexOf(div, intIdx + div_length); returnStr[intCount] = source.substring(intIdx + div_length, intIdex); intIdx = source.indexOf(div, intIdx + div_length); } else { returnStr[intCount] = source.substring(intIdx + div_length, source.length()); break; } intCount++; } while (true); } else { returnStr[0] = source.substring(0, source.length()); return returnStr; } } else { return returnStr; } return returnStr;}
public String dealNull(String str) { String returnstr = null; if (str == null) returnstr = ""; else returnstr = str; return returnstr;}
public String replace(String str, String substr, String restr) { String tmp[] = split(str, substr); String returnstr = null; if (tmp.length != 0) { returnstr = tmp[0]; for (int i = 0; i < tmp.length - 1; i++) returnstr = dealNull(returnstr) + restr + tmp[i + 1];
} return dealNull(returnstr);}
public String replaceFirst(String str, String substr, String restr) { String tmp[] = split(str, substr); String returnstr = null; if (tmp.length != 0) { returnstr = tmp[0]; for (int i = 0; i < tmp.length - 1; i++) if (i == 0) { returnstr = dealNull(returnstr) + restr + tmp[i + 1]; } else { returnstr = dealNull(returnstr) + substr + tmp[i + 1]; }
} return dealNull(returnstr);}}
配置文件:
log4j.appender.A3 = com.hejian.ExtendsLog4jlog4j.appender.A3.BufferSize = 1log4j.appender.A3.Driver = com.microsoft.jdbc.sqlserver.SQLServerDriverlog4j.appender.A3.URL = jdbc:microsoft:sqlserver://C01PC00443:1433;DatabaseName=NorthWindlog4j.appender.A3.User = salog4j.appender.A3.Password = log4j.appender.A3.layout = org.apache.log4j.PatternLayoutlog4j.appender.A3.layout.ConversionPattern = INSERT INTO log4j (createDate, thread, priority, category,opuser, message) values( getdate(), '%t', '%-5p', '%c', '%m' ,'$%m$')
import org.apache.log4j.jdbc.JDBCAppender;import org.apache.log4j.spi.ErrorCode;import org.apache.log4j.spi.LoggingEvent;
public class ExtendsLog4j extends JDBCAppender {
//处理默认SQL语句,使之成为你需要的SQL语句public String FormatSql(String s) { System.out.println(s); String tmpStr = s.substring(0, s.lastIndexOf("$")); tmpStr = tmpStr.substring(tmpStr.lastIndexOf("$") + 1); String array[] = tmpStr.split(" - "); System.out.println(array.length + " " + array[1]); if (array.length > 1) { s = replaceFirst(s, tmpStr, array[0]); s = replaceFirst(s, "$" + tmpStr + "$", array[1].replaceAll("'", "''")); } else { s = replaceFirst(s, tmpStr, ""); s = replaceFirst(s, "$" + tmpStr + "$", array[0].replaceAll("'", "''")); } System.out.println(s); return s;}
public void flushBuffer() { removes.ensureCapacity(buffer.size()); for (Iterator i = buffer.iterator(); i.hasNext();) { try { LoggingEvent logEvent = (LoggingEvent) i.next(); String sql = getLogStatement(logEvent); sql = FormatSql(sql); if (!sql.equals("")) { 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();}
public String[] split(String source, String div) { int arynum = 0; int intIdx = 0; int intIdex = 0; int div_length = div.length(); if (source.compareTo("") != 0) { if (source.indexOf(div) != -1) { intIdx = source.indexOf(div); int intCount = 1; do { if (source.indexOf(div, intIdx + div_length) != -1) { intIdx = source.indexOf(div, intIdx + div_length); arynum = intCount; } else { arynum += 2; break; } intCount++; } while (true); } else { arynum = 1; } } else { arynum = 0; } intIdx = 0; intIdex = 0; String returnStr[] = new String[arynum]; if (source.compareTo("") != 0) { if (source.indexOf(div) != -1) { intIdx = source.indexOf(div); returnStr[0] = source.substring(0, intIdx); int intCount = 1; do { if (source.indexOf(div, intIdx + div_length) != -1) { intIdex = source.indexOf(div, intIdx + div_length); returnStr[intCount] = source.substring(intIdx + div_length, intIdex); intIdx = source.indexOf(div, intIdx + div_length); } else { returnStr[intCount] = source.substring(intIdx + div_length, source.length()); break; } intCount++; } while (true); } else { returnStr[0] = source.substring(0, source.length()); return returnStr; } } else { return returnStr; } return returnStr;}
public String dealNull(String str) { String returnstr = null; if (str == null) returnstr = ""; else returnstr = str; return returnstr;}
public String replace(String str, String substr, String restr) { String tmp[] = split(str, substr); String returnstr = null; if (tmp.length != 0) { returnstr = tmp[0]; for (int i = 0; i < tmp.length - 1; i++) returnstr = dealNull(returnstr) + restr + tmp[i + 1];
} return dealNull(returnstr);}
public String replaceFirst(String str, String substr, String restr) { String tmp[] = split(str, substr); String returnstr = null; if (tmp.length != 0) { returnstr = tmp[0]; for (int i = 0; i < tmp.length - 1; i++) if (i == 0) { returnstr = dealNull(returnstr) + restr + tmp[i + 1]; } else { returnstr = dealNull(returnstr) + substr + tmp[i + 1]; }
} return dealNull(returnstr);}}
