-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathRequestLoggerImpl.java
More file actions
175 lines (151 loc) · 5.07 KB
/
RequestLoggerImpl.java
File metadata and controls
175 lines (151 loc) · 5.07 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* Copyright (c) restSQL Project Contributors. Licensed under MIT. */
package org.restsql.core.impl;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.restsql.core.Config;
import org.restsql.core.Factory;
import org.restsql.core.HttpRequestAttributes;
import org.restsql.core.Request;
/**
* Logs request for troubleshooting applications. The implementation logs requests to access, error and trace logs.
*
* @author Mark Sawers
*/
public class RequestLoggerImpl implements org.restsql.core.RequestLogger {
private static final Log accessLogger = LogFactory.getLog(Config.NAME_LOGGER_ACCESS);
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
private static final Log errorLogger = LogFactory.getLog(Config.NAME_LOGGER_ERROR);
private static final Log traceLogger = LogFactory.getLog(Config.NAME_LOGGER_TRACE);
private List<String> sqls;
private final GregorianCalendar startTime;
private HttpRequestAttributes httpAttributes;
public RequestLoggerImpl() {
startTime = new GregorianCalendar();
httpAttributes = Factory.getHttpRequestAttributes("?", "?", "?", null, null, null);
}
/**
* Adds a SQL statement generated during request processing. Used by the framework.
*/
@Override
public void addSql(final String sql) {
if (sqls == null) {
sqls = new ArrayList<String>(httpAttributes.getRequestBody() == null ? 1 : 16);
}
sqls.add(sql);
}
/**
* Returns list of SQL statements generated during request processing. Intended for Java API clients.
*/
@Override
public List<String> getSql() {
return sqls;
}
/**
* Logs exceptional response without an exception. Used by the service or Java API client.
*/
@Override
public void log(final int responseCode) {
log(responseCode, null, null);
}
/**
* Logs exceptional response with an exception. Used by the service or Java API client.
*/
@Override
public void log(final int responseCode, final Exception exception) {
log(responseCode, null, exception);
}
/**
* Logs normal response. Used by the service or Java API client.
*/
@Override
public void log(final String responseBody) {
log(200, responseBody, null);
}
/**
* Sets attributes of an HTTP request. Used by service when request is unauthorized prior to restSQL {@link Request}
* creation.
*/
public void setHttpRequestAttributes(final HttpRequestAttributes httpAttributes) {
this.httpAttributes = httpAttributes;
}
/**
* Sets request. Used by {@link Request} implementation.
*/
@Override
public void setRequest(final Request request) {
setHttpRequestAttributes(request.getHttpRequestAttributes());
}
// Private utils
private String getAccess(final int responseCode) {
final StringBuffer string = new StringBuffer(300);
// Client
string.append(httpAttributes.getClient());
// Timestamp
string.append(' ');
string.append(DATE_FORMAT.format(startTime.getTime()));
// Method
string.append(' ');
string.append(httpAttributes.getMethod());
// URI
string.append(' ');
string.append(httpAttributes.getUri());
// Response Code
string.append(' ');
string.append(String.valueOf(responseCode));
// Elapsed time
string.append(' ');
string.append(String.valueOf(System.currentTimeMillis() - startTime.getTimeInMillis()));
string.append("ms");
return string.toString();
}
private String getBriefError(final int responseCode, final Exception exception) {
final StringBuffer string = new StringBuffer(300);
string.append(" ");
string.append(String.valueOf(responseCode));
string.append(": ");
string.append(exception.getMessage());
return string.toString();
}
private void log(final int responseCode, final String responseBody, final Exception exception) {
if (accessLogger.isInfoEnabled() || errorLogger.isInfoEnabled() || traceLogger.isInfoEnabled()) {
final String access = getAccess(responseCode);
if (accessLogger.isInfoEnabled()) {
accessLogger.info(access);
if (responseCode != 200 && exception != null) {
accessLogger.info(getBriefError(responseCode, exception));
}
}
if (errorLogger.isInfoEnabled() && exception != null) {
logComplete(errorLogger, access, responseBody, exception);
}
if (traceLogger.isInfoEnabled()) {
logComplete(traceLogger, access, responseBody, exception);
}
}
}
private void logComplete(final Log logger, final String access, final String responseBody,
final Exception exception) {
logger.info(access);
if (httpAttributes.getRequestBody() != null) {
logger.info(" request:");
logger.info(httpAttributes.getRequestBody());
}
if (sqls != null && sqls.size() > 0) {
logger.info(" sql:");
for (final String sql : sqls) {
logger.info(sql);
}
}
logger.info(" response:");
if (responseBody != null) {
logger.info(responseBody);
} else if (exception != null) { // should always be null at this point
logger.info(exception.getMessage());
}
logger.info("---------------------");
}
}