Skip to content

Commit 475e3a1

Browse files
committed
re-factor to decouple logging better.
1 parent 7f0c1a6 commit 475e3a1

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

src/main/java/io/fusionauth/http/server/DefaultHTTPUnexpectedExceptionHandler.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@
2424
* @author Daniel DeGroff
2525
*/
2626
public class DefaultHTTPUnexpectedExceptionHandler implements HTTPUnexpectedExceptionHandler {
27+
private final Logger logger;
28+
29+
public DefaultHTTPUnexpectedExceptionHandler(LoggerFactory loggerFactory) {
30+
this.logger = loggerFactory.getLogger(DefaultHTTPUnexpectedExceptionHandler.class);
31+
}
2732

2833
@Override
29-
public int handle(LoggerFactory loggerFactory, Throwable t) {
34+
public int handle(Throwable t) {
3035
int internalServerError = 500;
31-
Logger logger = loggerFactory.getLogger(DefaultHTTPUnexpectedExceptionHandler.class);
3236
logger.error(String.format("[%s] Closing socket with status [%d]. An HTTP worker threw an exception while processing a request.", Thread.currentThread().threadId(), internalServerError), t);
3337
return internalServerError;
3438
}

src/main/java/io/fusionauth/http/server/HTTPServerConfiguration.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public class HTTPServerConfiguration implements Configurable<HTTPServerConfigura
7979

8080
private Duration shutdownDuration = Duration.ofSeconds(10);
8181

82-
private HTTPUnexpectedExceptionHandler unexpectedExceptionHandler = new DefaultHTTPUnexpectedExceptionHandler();
82+
private HTTPUnexpectedExceptionHandler unexpectedExceptionHandler;
8383

8484
private Duration writeThroughputCalculationDelayDuration = Duration.ofSeconds(5);
8585

@@ -281,6 +281,11 @@ public Duration getShutdownDuration() {
281281
* @return The HTTP unexpected exception handler for this server. Never null.
282282
*/
283283
public HTTPUnexpectedExceptionHandler getUnexpectedExceptionHandler() {
284+
// Lazily construct a default so we can use the configured loggerFactory.
285+
if (unexpectedExceptionHandler == null) {
286+
unexpectedExceptionHandler = new DefaultHTTPUnexpectedExceptionHandler(loggerFactory);
287+
}
288+
284289
return unexpectedExceptionHandler;
285290
}
286291

src/main/java/io/fusionauth/http/server/HTTPUnexpectedExceptionHandler.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package io.fusionauth.http.server;
1717

18-
import io.fusionauth.http.log.LoggerFactory;
19-
2018
/**
2119
* An interface defining the HTTP unexpected exception handler contract.
2220
*
@@ -30,9 +28,8 @@ public interface HTTPUnexpectedExceptionHandler {
3028
* The intent is that this provides additional flexibility on the status code and the logging behavior when an unexpected exception
3129
* caught.
3230
*
33-
* @param loggerFactory the configured logger factory.
34-
* @param t the unexpected exception to handle.
31+
* @param t the unexpected exception to handle.
3532
* @return the desired HTTP status code. Note that if the response has already been committed this will be ignored.
3633
*/
37-
int handle(LoggerFactory loggerFactory, Throwable t);
34+
int handle(Throwable t);
3835
}

src/main/java/io/fusionauth/http/server/internal/HTTPWorker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public void run() {
270270
} catch (Throwable e) {
271271
var status = Status.InternalServerError;
272272
try {
273-
status = configuration.getUnexpectedExceptionHandler().handle(logger, e);
273+
status = configuration.getUnexpectedExceptionHandler().handle(e);
274274
} catch (Throwable ignore) {
275275
}
276276

0 commit comments

Comments
 (0)