Skip to content

Commit

Permalink
Merge pull request clusterbench#531 from rhusar/397-logger-servlet
Browse files Browse the repository at this point in the history
chore: expand LoggerServlet to accept a log level defaulting to INFO, add a default message 'ping', fix formatter, expand test, update readme (resolves clusterbench#397)
  • Loading branch information
rhusar authored Nov 28, 2024
2 parents 58e5fb5 + ae29117 commit a1af647
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 32 deletions.
9 changes: 9 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ http://localhost:8080/clusterbench/jboss-node-name[/clusterbench/jboss-node-name

Servlet which prints out the node name as a value of the `jboss.node.name` system property.

==== LoggerServlet

http://localhost:8080/clusterbench/log?level=WARN&msg=Example%20warning.[/clusterbench/log?level=WARN&msg=Example%20warning.]

Servlet which logs the provided message in the `msg` parameter to the server log.
If no message is provided, a simple `ping` message is logged.
The log message level can be optionally provided in the `level` parameter.
Logs at `INFO` level by default.

== Server Configurations

The `scripts` directory at the root of the repository contains CLI scripts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

package org.jboss.test.clusterbench.common;

/**
* @author Radoslav Husar
*/
public interface ClusterBenchConstants {

String CREATE = "create";
Expand All @@ -14,4 +17,9 @@ public interface ClusterBenchConstants {
String INVALIDATE = "invalidate";

String CARGOKB = "cargokb";

/**
* Standard plain text HTTP response body.
*/
String SUCCESS = "Success";
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.jboss.test.clusterbench.common.ClusterBenchConstants;

/**
* @author Tommasso Borgato
* @author Tommaso Borgato
* @author Radoslav Husar
*/
@WebServlet(name = "JBossNodeNameServlet", urlPatterns = { "/jboss-node-name" })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,56 @@
/*
* Copyright The ClusterBench Authors
* SPDX-License-Identifier: Apache-2.0
*/

package org.jboss.test.clusterbench.web.debug;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.logging.Logger;
import org.jboss.test.clusterbench.common.ClusterBenchConstants;

@WebServlet("/log")
/**
* Logging servlet that logs provided message in 'msg' parameter.
* Can also be called without parameters as a 'ping' mechanism which
* Optionally, log level can be provided in the 'level' parameter.
* The default level is INFO.
*
* @author Tommaso Borgato
* @author Radoslav Husar
*/
@WebServlet(name = "LoggerServlet", urlPatterns = {"/log"})
public class LoggerServlet extends HttpServlet {
private static final Logger LOG = Logger.getLogger(LoggerServlet.class.getCanonicalName());

private void logMarker(String msg) {
LOG.info(msg);
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
String msg = request.getParameter("msg");
logMarker(msg);
response.getWriter().print("Success");
}

// Servlet parameters and their default values
public static final String MESSAGE_PARAM = "msg";
public static final String DEFAULT_MESSAGE = "ping";
public static final String LEVEL_PARAM = "level";
public static final Level DEFAULT_LEVEL = Level.INFO;

private static final Logger LOG = Logger.getLogger(LoggerServlet.class.getCanonicalName());

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String requestLevel = request.getParameter(LEVEL_PARAM);
String message = request.getParameter(MESSAGE_PARAM);

Level level = null;
if (requestLevel != null) {
try {
level = Level.parse(requestLevel);
} catch (Exception ignored) {
// Ignore silently and use default.
}
}

LOG.log(level == null ? DEFAULT_LEVEL : level, message == null ? DEFAULT_MESSAGE : message);

response.getWriter().print(ClusterBenchConstants.SUCCESS);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
/**
* @author Tommaso Borgato
* @author Radoslav Husar
*/
package org.jboss.test.clusterbench.it.wildfly;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
Expand All @@ -10,24 +16,43 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* @author Tommaso Borgato
* @author Radoslav Husar
*/
@ExtendWith(ArquillianExtension.class)
@RunAsClient
public class LoggerServletIT extends AbstractWildFlyIT {
/**
* Verifies log servlet is deployed
*/
@Test
public void test() throws Exception {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet("http://localhost:8080/clusterbench/log");
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
assertEquals(200, response.getStatusLine().getStatusCode());
String responseBody = EntityUtils.toString(response.getEntity());
assertEquals("Success", responseBody);
}
}

}
/**
* Verifies log servlet is deployed
*/
@Test
public void test() throws Exception {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet("http://localhost:8080/clusterbench/log?msg=Basic%20info.");
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
assertEquals(200, response.getStatusLine().getStatusCode());
String responseBody = EntityUtils.toString(response.getEntity());
assertEquals("Success", responseBody);
}

// Test providing a level
httpGet = new HttpGet("http://localhost:8080/clusterbench/log?level=WARN&msg=Test%20warning.");
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
assertEquals(200, response.getStatusLine().getStatusCode());
String responseBody = EntityUtils.toString(response.getEntity());
assertEquals("Success", responseBody);
}

// Test providing non-existent level and no message should default to "INFO: ping".
httpGet = new HttpGet("http://localhost:8080/clusterbench/log?level=WEIRD");
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
assertEquals(200, response.getStatusLine().getStatusCode());
String responseBody = EntityUtils.toString(response.getEntity());
assertEquals("Success", responseBody);
}
}

}
}

0 comments on commit a1af647

Please sign in to comment.