Skip to content

Commit

Permalink
Switch to Java Logger for cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
mtughan committed Sep 18, 2024
1 parent 1e14fc8 commit a78ba17
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.jenkinsci.plugins.sonargerrit.test_infrastructure;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.testcontainers.containers.output.BaseConsumer;
import org.testcontainers.containers.output.OutputFrame;

// based on org.testcontainers.containers.output.Slf4jLogConsumer
public class JulLogConsumer extends BaseConsumer<JulLogConsumer> {
private final Logger logger;
private boolean separateOutputStreams;
private String prefix = "";

public JulLogConsumer(Logger logger) {
this(logger, false);
}

public JulLogConsumer(Logger logger, boolean separateOutputStreams) {
this.logger = logger;
this.separateOutputStreams = separateOutputStreams;
}

public JulLogConsumer withPrefix(String prefix) {
this.prefix = "[" + prefix + "] ";
return this;
}

public JulLogConsumer withSeparateOutputStreams() {
this.separateOutputStreams = true;
return this;
}

@Override
public void accept(OutputFrame outputFrame) {
OutputFrame.OutputType outputType = outputFrame.getType();

String utf8String = outputFrame.getUtf8String().replaceAll("((\\r?\\n)|(\\r))$", "");

switch (outputType) {
case END:
break;
case STDOUT:
if (separateOutputStreams) {
logger.log(Level.INFO, () -> prefix.isEmpty() ? "" : (prefix + ": ") + utf8String);
} else {
logger.log(Level.INFO, () -> prefix + outputType + ": " + utf8String);
}
break;
case STDERR:
if (separateOutputStreams) {
logger.log(Level.SEVERE, () -> prefix.isEmpty() ? "" : (prefix + ": ") + utf8String);
} else {
logger.log(Level.INFO, () -> prefix + outputType + ": " + utf8String);
}
break;
default:
throw new IllegalArgumentException("Unexpected outputType " + outputType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Logger;
import me.redaalaoui.gerrit_rest_java_client.rest.GerritAuthData;
import me.redaalaoui.gerrit_rest_java_client.rest.GerritRestApiFactory;
import me.redaalaoui.gerrit_rest_java_client.thirdparty.com.google.gerrit.extensions.api.GerritApi;
Expand All @@ -19,16 +20,14 @@
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.jenkinsci.plugins.sonargerrit.test_infrastructure.CloseableResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.jenkinsci.plugins.sonargerrit.test_infrastructure.JulLogConsumer;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.output.Slf4jLogConsumer;

/** @author Réda Housni Alaoui */
public class GerritServer {

private static final Logger LOG = LoggerFactory.getLogger(GerritServer.class);
private static final Logger LOG = Logger.getLogger(GerritServer.class.getName());

private static final int HTTP_PORT = 8080;
private static final int SSH_PORT = 29418;
Expand Down Expand Up @@ -60,7 +59,7 @@ public void close() {
private GerritServer(Network network) {
container =
new GenericContainer<>("gerritcodereview/gerrit:3.4.1-ubuntu20")
.withLogConsumer(new Slf4jLogConsumer(LOG))
.withLogConsumer(new JulLogConsumer(LOG))
.withExposedPorts(HTTP_PORT, SSH_PORT)
.withNetwork(network)
.withNetworkAliases(NETWORK_ALIAS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.logging.Logger;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.jenkinsci.plugins.sonargerrit.test_infrastructure.CloseableResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.jenkinsci.plugins.sonargerrit.test_infrastructure.JulLogConsumer;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.images.builder.ImageFromDockerfile;

/** @author Réda Housni Alaoui */
public class SonarqubeServer {

private static final Logger LOG = LoggerFactory.getLogger(SonarqubeServer.class);
private static final Logger LOG = Logger.getLogger(SonarqubeServer.class.getName());

private static final int HTTP_PORT = 9000;

Expand Down Expand Up @@ -62,7 +61,7 @@ private SonarqubeServer(Network network) {
.withEnv(
"SONAR_CE_JAVAADDITIONALOPTS",
"-javaagent:./extensions/plugins/sonarqube-community-branch-plugin.jar=ce")
.withLogConsumer(new Slf4jLogConsumer(LOG))
.withLogConsumer(new JulLogConsumer(LOG))
.withExposedPorts(HTTP_PORT)
.withNetwork(network)
.withNetworkAliases(NETWORK_ALIAS);
Expand Down

0 comments on commit a78ba17

Please sign in to comment.