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.getWebserverPort(),
webServerConfig.getWebserverMaxConnections(), webServerConfig.getWebserverMaxConnections(),
webServerConfig.getWebserverBindAdress(), webServerConfig.getWebserverBindAdress(),
requestHandler requestHandler,
false
); );
webServer.start(); webServer.start();
} }

View File

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

View File

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