public class LogsTest {
- public static final String ACCESS_LOGS_PATTERN = "\"%reqAttribute{LOGIN}\" \"%r\" %s";
+ public static final String ACCESS_LOGS_PATTERN = "\"%reqAttribute{ID}\" \"%reqAttribute{LOGIN}\" \"%r\" %s";
private static final String PATH = "/called/from/LogsTest";
@ClassRule
* SONAR-7581
*/
@Test
- public void test_access_logs() throws Exception {
+ public void verify_login_in_access_logs() throws Exception {
// log "-" for anonymous
sendHttpRequest(ItUtils.newWsClient(orchestrator), PATH);
assertThat(accessLogsFile()).isFile().exists();
verifyLastAccessLogLine("admin", PATH, 404);
}
+ @Test
+ public void verify_request_id_in_access_logs() throws IOException {
+ sendHttpRequest(ItUtils.newWsClient(orchestrator), PATH);
+ String lastAccessLog = readLastAccessLog();
+ assertThat(lastAccessLog).doesNotStartWith("\"\"").startsWith("\"");
+ int firstQuote = lastAccessLog.indexOf('"');
+ String requestId = lastAccessLog.substring(firstQuote + 1, lastAccessLog.indexOf('"', firstQuote + 1));
+ assertThat(requestId.length()).isGreaterThanOrEqualTo(20);
+ }
+
private void verifyLastAccessLogLine(String login, String path, int status) throws IOException {
- assertThat(readLastAccessLog()).isEqualTo(format("\"%s\" \"GET %s HTTP/1.1\" %d", login, path, status));
+ assertThat(readLastAccessLog()).endsWith(format("\"%s\" \"GET %s HTTP/1.1\" %d", login, path, status));
}
private String readLastAccessLog() throws IOException {
# - else a custom pattern. See http://logback.qos.ch/manual/layouts.html#AccessPatternLayout.
# The login of authenticated user is not implemented with "%u" but with "%reqAttribute{LOGIN}" (since version 6.1).
# The value displayed for anonymous users is "-".
+# The SonarQube's HTTP request ID can be added to the pattern with "%reqAttribute{ID}" (since version 6.2).
# If SonarQube is behind a reverse proxy, then the following value allows to display the correct remote IP address:
#sonar.web.accessLogs.pattern=%i{X-Forwarded-For} %l %u [%t] "%r" %s %b "%i{Referer}" "%i{User-Agent}"
# Default value is: