Skip to content

Commit

Permalink
change non-critical logs' level to debug in XatkitServer (fix #267)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdaniel committed Jan 24, 2020
1 parent 9a7a2c1 commit 478fafe
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The changelog format is based on [Keep a Changelog](https://keepachangelog.com/e

- `HttpHandler` now supports `HttpEntity` instances returned from `RestHandler#handle`. This allows to define handlers that directly return a valid `HttpEntity` (e.g. the content of an HTML page). In this case, the `RestHandler` implementation is responsible of the `HttpEntity` creation.
- `RestHandler` instances can now throw a `RestHandlerException` to notify the server that an error occurred when handling the request. For now this exception is used to return a *404* status code instead of *200*.
- Change log level of non-critical messages in `XatkitServer` this reduces the amount of noise in Xatkit logs.

## Removed

Expand All @@ -26,6 +27,7 @@ The changelog format is based on [Keep a Changelog](https://keepachangelog.com/e

- [#251](https://github.com/xatkit-bot-platform/xatkit-runtime/issues/251): *AdminHttpHandler should be moved to react platform*
- The `XatkitServer` now correctly returns a `404` error if there is no `RestHandler` associated to a requested URI.
- [#267](https://github.com/xatkit-bot-platform/xatkit-runtime/issues/267): *Change log level of XatkitServer logs to debug*

## [4.0.0] - 2019-12-01

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,23 @@ public void handle(final HttpRequest request, final HttpResponse response, final
String method = request.getRequestLine().getMethod().toUpperCase(Locale.ROOT);
String target = request.getRequestLine().getUri();

Log.info("{0} - Received a {1} query on {2}", this.getClass().getSimpleName(), method, target);
Log.debug("{0} - Received a {1} query on {2}", this.getClass().getSimpleName(), method, target);

/*
* Ignore the parameters, they are not used for now.
*/
if (method.equals("GET")) {
if (target.startsWith(XatkitServerUtils.PUBLIC_CONTENT_URL_FRAGMENT)) {
String filePath = target.replaceFirst(XatkitServerUtils.PUBLIC_CONTENT_URL_FRAGMENT, "");
Log.info("File Path: {0}", filePath);
Log.debug("File Path: {0}", filePath);
File file = xatkitServer.getPublicFile(filePath);
if (nonNull(file) && file.exists() && !file.isDirectory()) {
FileInputStream fis;
try {
fis = new FileInputStream(file);
} catch (IOException e) {
Log.error("Cannot retrieve the file {0}", file.getAbsolutePath());
Log.error("{0} Cannot retrieve the file {1}", this.getClass().getSimpleName(),
file.getAbsolutePath());
response.setStatusCode(HttpStatus.SC_NOT_FOUND);
return;
}
Expand All @@ -86,7 +87,7 @@ public void handle(final HttpRequest request, final HttpResponse response, final
}
}
}
Log.warn("Cannot server content {0}", target);
Log.error("Cannot server content {0}, unsupported method {1}", target, method);
response.setStatusCode(HttpStatus.SC_NOT_FOUND);
}
}
16 changes: 7 additions & 9 deletions core/src/main/java/com/xatkit/core/server/HttpHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,16 @@ public void handle(final HttpRequest request, final HttpResponse response, final
request.getRequestLine().getUri()), e);
}

Log.info("Received a {0} query on {1}", method, path);
Log.debug("Received a {0} query on {1}", method, path);

parameters.forEach(p -> Log.info("Query parameter: {0}={1}", p.getName(), p.getValue()));
parameters.forEach(p -> Log.debug("Query parameter: {0}={1}", p.getName(), p.getValue()));

List<Header> headers = Arrays.asList(request.getAllHeaders());
headers.forEach(h -> Log.info("Header {0}={1}", h.getName(), h.getValue()));
headers.forEach(h -> Log.debug("Header {0}={1}", h.getName(), h.getValue()));

Object content = null;
String contentType = null;

Log.info("Request type {0}", request.getClass().getSimpleName());

/*
* The access control headers that will be returned in the response.
*/
Expand All @@ -143,19 +141,19 @@ public void handle(final HttpRequest request, final HttpResponse response, final
Header encodingHeader = entity.getContentEncoding();
if (nonNull(encodingHeader) && encodingHeader.getElements().length > 0) {
contentEncoding = encodingHeader.getElements()[0].getName();
Log.info("Query content encoding: {0}", contentEncoding);
Log.debug("Query content encoding: {0}", contentEncoding);
} else {
Log.warn("Unknown query content encoding");
}
Header contentTypeHeader = entity.getContentType();
if (nonNull(contentTypeHeader) && contentTypeHeader.getElements().length > 0) {
contentType = contentTypeHeader.getElements()[0].getName();
Log.info("Query content type: {0}", contentType);
Log.debug("Query content type: {0}", contentType);
} else {
Log.warn("Unknown query content type");
}
Long contentLength = entity.getContentLength();
Log.info("Query content length: {0}", contentLength);
Log.debug("Query content length: {0}", contentLength);

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
Expand Down Expand Up @@ -200,7 +198,7 @@ public void handle(final HttpRequest request, final HttpResponse response, final
response.setEntity(resultEntity);
}
} else {
Log.warn("Cannot embed the provided json element {0}", result);
Log.warn("Cannot embed the handler's result {0}", result);
}
response.setHeader("Access-Control-Allow-Headers", "content-type");
response.setStatusCode(HttpStatus.SC_OK);
Expand Down
13 changes: 7 additions & 6 deletions core/src/main/java/com/xatkit/core/server/XatkitServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ public class XatkitServer {
public XatkitServer(Configuration configuration) {
checkNotNull(configuration, "Cannot start the %s with the provided %s: %s", this.getClass().getSimpleName
(), Configuration.class.getSimpleName(), configuration);
Log.info("Creating {0}", this.getClass().getSimpleName());
this.isStarted = false;
if (configuration.containsKey(XatkitServerUtils.SERVER_KEYSTORE_LOCATION_KEY)
&& !configuration.containsKey(XatkitServerUtils.SERVER_PUBLIC_URL_KEY)) {
Expand Down Expand Up @@ -164,18 +163,20 @@ public XatkitServer(Configuration configuration) {
.setSslContext(sslContext)
.setSocketConfig(socketConfig)
.setExceptionLogger(e -> {
if (e instanceof SocketTimeoutException) {
if(e instanceof SocketTimeoutException || (e instanceof IOException && e.getMessage().equals(
"Connection reset by peer"))) {
/*
* SocketTimeoutExceptions are thrown after each query, we can log them as debug to avoid
* polluting the application log.
* Connection reset by peer is thrown when a remote client closes the connection, this
* happens quite often and can be logged as debug too.
*/
Log.debug(e);
} else {
Log.error(e);
}
})
.registerHandler("/content*", new ContentHttpHandler(this))
// .registerHandler("/admin*", new AdminHttpHandler(configuration))
.registerHandler("*", new HttpHandler(this))
.create();
}
Expand All @@ -197,7 +198,7 @@ SSLContext createSSLContext(@Nonnull Configuration configuration) {
Configuration.class.getSimpleName(), configuration);
String keystorePath = configuration.getString(XatkitServerUtils.SERVER_KEYSTORE_LOCATION_KEY);
if (isNull(keystorePath)) {
Log.info("No SSL context to load");
Log.debug("No SSL context to load");
return null;
}
File keystoreFile = FileUtils.getFile(keystorePath, configuration);
Expand Down Expand Up @@ -583,7 +584,7 @@ String getPublicURL(File file) {
try {
filePath = Paths.get(file.getAbsolutePath()).toRealPath(LinkOption.NOFOLLOW_LINKS);
} catch (NoSuchFileException e) {
Log.info("Cannot retrieve the public URL for the provided file {0}, the file does not exist",
Log.error("Cannot retrieve the public URL for the provided file {0}, the file does not exist",
file.getAbsolutePath());
return null;
} catch (IOException e) {
Expand Down Expand Up @@ -645,7 +646,7 @@ File getPublicFile(String filePath) {
try {
p = p.toRealPath(LinkOption.NOFOLLOW_LINKS);
} catch (NoSuchFileException e) {
Log.warn("The public file {0} does not exist", p);
Log.error("The public file {0} does not exist", p);
return null;
} catch (IOException e) {
throw new XatkitException(e);
Expand Down

0 comments on commit 478fafe

Please sign in to comment.