-
-
Notifications
You must be signed in to change notification settings - Fork 172
/
DBAppender.java
146 lines (125 loc) 路 4.47 KB
/
DBAppender.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2013, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/
package ch.qos.logback.access.db;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Enumeration;
import ch.qos.logback.access.spi.IAccessEvent;
import ch.qos.logback.core.db.DBAppenderBase;
/**
* The DBAppender inserts access events into three database tables in a format
* independent of the Java programming language.
*
* For more information about this appender, please refer to the online manual at
* http://logback.qos.ch/manual/appenders.html#AccessDBAppender
*
* @author Ceki Gülcü
* @author Ray DeCampo
* @author Sébastien Pennec
*/
public class DBAppender extends DBAppenderBase<IAccessEvent> {
protected static final String insertSQL;
protected final String insertHeaderSQL = "INSERT INTO access_event_header (event_id, header_key, header_value) VALUES (?, ?, ?)";
protected static final Method GET_GENERATED_KEYS_METHOD;
private boolean insertHeaders = false;
static {
StringBuffer sql = new StringBuffer();
sql.append("INSERT INTO access_event (");
sql.append("timestmp, ");
sql.append("requestURI, ");
sql.append("requestURL, ");
sql.append("remoteHost, ");
sql.append("remoteUser, ");
sql.append("remoteAddr, ");
sql.append("protocol, ");
sql.append("method, ");
sql.append("serverName, ");
sql.append("postContent) ");
sql.append(" VALUES (?, ?, ? ,?, ?, ?, ?, ?, ?, ?)");
insertSQL = sql.toString();
Method getGeneratedKeysMethod;
try {
getGeneratedKeysMethod = PreparedStatement.class.getMethod(
"getGeneratedKeys", (Class[]) null);
} catch (Exception ex) {
getGeneratedKeysMethod = null;
}
GET_GENERATED_KEYS_METHOD = getGeneratedKeysMethod;
}
@Override
protected void subAppend(IAccessEvent event, Connection connection,
PreparedStatement insertStatement) throws Throwable {
addAccessEvent(insertStatement, event);
int updateCount = insertStatement.executeUpdate();
if (updateCount != 1) {
addWarn("Failed to insert access event");
}
}
protected void secondarySubAppend(IAccessEvent event, Connection connection,
long eventId) throws Throwable {
if (insertHeaders) {
addRequestHeaders(event, connection, eventId);
}
}
void addAccessEvent(PreparedStatement stmt, IAccessEvent event)
throws SQLException {
stmt.setLong(1, event.getTimeStamp());
stmt.setString(2, event.getRequestURI());
stmt.setString(3, event.getRequestURL());
stmt.setString(4, event.getRemoteHost());
stmt.setString(5, event.getRemoteUser());
stmt.setString(6, event.getRemoteAddr());
stmt.setString(7, event.getProtocol());
stmt.setString(8, event.getMethod());
stmt.setString(9, event.getServerName());
stmt.setString(10, event.getRequestContent());
}
void addRequestHeaders(IAccessEvent event,
Connection connection, long eventId) throws SQLException {
Enumeration names = event.getRequestHeaderNames();
if (names.hasMoreElements()) {
PreparedStatement insertHeaderStatement = connection
.prepareStatement(insertHeaderSQL);
while (names.hasMoreElements()) {
String key = (String) names.nextElement();
String value = (String) event.getRequestHeader(key);
insertHeaderStatement.setLong(1, eventId);
insertHeaderStatement.setString(2, key);
insertHeaderStatement.setString(3, value);
if (cnxSupportsBatchUpdates) {
insertHeaderStatement.addBatch();
} else {
insertHeaderStatement.execute();
}
}
if (cnxSupportsBatchUpdates) {
insertHeaderStatement.executeBatch();
}
insertHeaderStatement.close();
}
}
@Override
protected Method getGeneratedKeysMethod() {
return GET_GENERATED_KEYS_METHOD;
}
@Override
protected String getInsertSQL() {
return insertSQL;
}
public void setInsertHeaders(boolean insertHeaders) {
this.insertHeaders = insertHeaders;
}
}