Restrict logging of web requests to the CLI when run with --verbose or -b

This commit is contained in:
Ryan Aird 2020-10-01 14:38:02 -06:00
parent f646c1dedb
commit 73e8da9c37
3 changed files with 24 additions and 17 deletions

View File

@ -130,7 +130,8 @@ public void load() throws IOException, ParseResourceException {
webServerConfig.getWebserverPort(),
webServerConfig.getWebserverMaxConnections(),
webServerConfig.getWebserverBindAdress(),
requestHandler
requestHandler,
false
);
webServer.start();
}

View File

@ -46,11 +46,14 @@ public class HttpConnection implements Runnable {
private Socket connection;
private InputStream in;
private OutputStream out;
private final boolean verbose;
public HttpConnection(ServerSocket server, Socket connection, HttpRequestHandler handler, int timeout, TimeUnit timeoutUnit) throws IOException {
public HttpConnection(ServerSocket server, Socket connection, HttpRequestHandler handler, int timeout, TimeUnit timeoutUnit, boolean verbose) throws IOException {
this.server = server;
this.connection = connection;
this.handler = handler;
this.verbose = verbose;
if (isClosed()){
throw new IOException("Socket already closed!");
@ -69,19 +72,20 @@ public void run() {
HttpRequest request = acceptRequest();
HttpResponse response = handler.handle(request);
sendResponse(response);
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
Logger.global.logInfo(
connection.getInetAddress().toString() +
" [ " +
dateFormat.format(date) +
" ] \"" +
request.getMethod() +
" " + request.getPath() +
" " + request.getVersion() +
"\" " +
response.getStatusCode().toString());
if (verbose) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
Logger.global.logInfo(
connection.getInetAddress().toString() +
" [ " +
dateFormat.format(date) +
" ] \"" +
request.getMethod() +
" " + request.getPath() +
" " + request.getVersion() +
"\" " +
response.getStatusCode().toString());
}
} catch (InvalidRequestException e){
try {
sendResponse(new HttpResponse(HttpStatusCode.BAD_REQUEST));

View File

@ -41,6 +41,7 @@ public class WebServer extends Thread {
private final int port;
private final int maxConnections;
private final InetAddress bindAdress;
private final boolean verbose;
private HttpRequestHandler handler;
@ -48,10 +49,11 @@ public class WebServer extends Thread {
private ServerSocket server;
public WebServer(int port, int maxConnections, InetAddress bindAdress, HttpRequestHandler handler) {
public WebServer(int port, int maxConnections, InetAddress bindAdress, HttpRequestHandler handler, boolean verbose) {
this.port = port;
this.maxConnections = maxConnections;
this.bindAdress = bindAdress;
this.verbose = verbose;
this.handler = handler;
@ -81,7 +83,7 @@ public void run(){
Socket connection = server.accept();
try {
connectionThreads.execute(new HttpConnection(server, connection, handler, 10, TimeUnit.SECONDS));
connectionThreads.execute(new HttpConnection(server, connection, handler, 10, TimeUnit.SECONDS, verbose));
} catch (RejectedExecutionException e){
connection.close();
Logger.global.logWarning("Dropped an incoming HttpConnection! (Too many connections?)");