import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
-import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.process.ProcessId;
import org.sonar.server.log.ServerLogging;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.MediaTypes;
import static java.util.Arrays.stream;
+import static java.util.stream.Collectors.toList;
public class LogsAction implements SystemWsAction {
private static final String PROCESS_PROPERTY = "process";
+ private static final String ACCESS_LOG = "access";
private final UserSession userSession;
private final ServerLogging serverLogging;
@Override
public void define(WebService.NewController controller) {
+ var values = stream(ProcessId.values()).map(ProcessId::getKey).collect(toList());
+ values.add(ACCESS_LOG);
+ values.sort(String::compareTo);
+
WebService.NewAction action = controller.createAction("logs")
.setDescription("Get system logs in plain-text format. Requires system administration permission.")
.setResponseExample(getClass().getResource("logs-example.log"))
action
.createParam(PROCESS_PROPERTY)
- .setPossibleValues(stream(ProcessId.values())
- .map(ProcessId::getKey)
- .sorted()
- .collect(MoreCollectors.toList(ProcessId.values().length)))
+ .setPossibleValues(values)
.setDefaultValue(ProcessId.APP.getKey())
.setSince("6.2")
.setDescription("Process to get logs from");
userSession.checkIsSystemAdministrator();
String processKey = wsRequest.mandatoryParam(PROCESS_PROPERTY);
- ProcessId processId = ProcessId.fromKey(processKey);
+ String filePrefix = ACCESS_LOG.equals(processKey) ? ACCESS_LOG : ProcessId.fromKey(processKey).getLogFilenamePrefix();
File logsDir = serverLogging.getLogsDir();
try (Stream<Path> stream = Files.list(Paths.get(logsDir.getPath()))) {
Optional<Path> path = stream
- .filter(p -> p.getFileName().toString().contains(processId.getLogFilenamePrefix())
+ .filter(p -> p.getFileName().toString().contains(filePrefix)
&& p.getFileName().toString().endsWith(".log"))
.max(Comparator.comparing(Path::toString));
import org.sonar.server.ws.WsActionTester;
import org.sonarqube.ws.MediaTypes;
+import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public void values_of_process_parameter_are_names_of_processes() {
Set<String> values = actionTester.getDef().param("process").possibleValues();
// values are lower-case and alphabetically ordered
- assertThat(values).containsExactly("app", "ce", "es", "web");
+ assertThat(values).containsExactly("access", "app", "ce", "es", "web");
}
@Test
}
@Test
- public void get_ce_logs() throws IOException {
+ public void download_logs() throws IOException {
logInAsSystemAdministrator();
createAllLogsFiles();
- TestResponse response = actionTester.newRequest()
- .setParam("process", "ce")
- .execute();
- assertThat(response.getMediaType()).isEqualTo(MediaTypes.TXT);
- assertThat(response.getInput()).isEqualTo("{ce}");
- }
-
- @Test
- public void get_es_logs() throws IOException {
- logInAsSystemAdministrator();
-
- createAllLogsFiles();
-
- TestResponse response = actionTester.newRequest()
- .setParam("process", "es")
- .execute();
- assertThat(response.getMediaType()).isEqualTo(MediaTypes.TXT);
- assertThat(response.getInput()).isEqualTo("{es}");
- }
-
- @Test
- public void get_web_logs() throws IOException {
- logInAsSystemAdministrator();
-
- createAllLogsFiles();
-
- TestResponse response = actionTester.newRequest()
- .setParam("process", "web")
- .execute();
- assertThat(response.getMediaType()).isEqualTo(MediaTypes.TXT);
- assertThat(response.getInput()).isEqualTo("{web}");
+ asList("ce", "es", "web", "access").forEach(process -> {
+ TestResponse response = actionTester.newRequest()
+ .setParam("process", process)
+ .execute();
+ assertThat(response.getMediaType()).isEqualTo(MediaTypes.TXT);
+ assertThat(response.getInput()).isEqualTo("{" + process + "}");
+ });
}
@Test
private File createAllLogsFiles() throws IOException {
File dir = createLogsDir();
+ FileUtils.write(new File(dir, "access.log"), "{access}");
FileUtils.write(new File(dir, "sonar.log"), "{app}");
FileUtils.write(new File(dir, "ce.log"), "{ce}");
FileUtils.write(new File(dir, "es.log"), "{es}");