4 protected Connection connection = null;
5 protected String sqlStatement = "";
6 protected int bufferSize = 1;
7 protected ArrayList buffer;
8 protected ArrayList removes;
9 private boolean locationInfo = false;
10
11 public void append(LoggingEvent event) {
12event.getNDC();
13event.getThreadName();
14event.getMDCCopy();
15if (locationInfo) {
16event.getLocationInformation();
17}
18event.getRenderedMessage();
19event.getThrowableStrRep();
20buffer.add(event);
21if (buffer.size() >= bufferSize)
22flushBuffer();
23 }
24 public void flushBuffer() {
25removes.ensureCapacity(buffer.size());
26for (Iterator i = buffer.iterator(); i.hasNext();) {
27try {
28LoggingEvent logEvent = (LoggingEvent) i.next();
29String sql = getLogStatement(logEvent);
30execute(sql);
31removes.add(logEvent);
32} catch (SQLException e) {
33errorHandler.error("Failed to excute sql", e,
34ErrorCode.FLUSH_FAILURE);
35}
36}
37buffer.removeAll(removes);
38removes.clear();
39 }
40 protected String getLogStatement(LoggingEvent event) {
41return getLayout().format(event);
42 }
43 protected void execute(String sql) throws SQLException {
44Connection con = null;
45Statement stmt = null;
46try {
47con = getConnection();
48stmt = con.createStatement();
49stmt.executeUpdate(sql);
50} catch (SQLException e) {
51if (stmt != null)
52stmt.close();
53throw e;
54}
55stmt.close();
56closeConnection(con);
57 }
58 protected Connection getConnection() throws SQLException {
59if (!DriverManager.getDrivers().hasMoreElements())
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-29497-30.html
一定能达到德艺双馨的境界