Skip to content

Commit b8aa660

Browse files
committed
Minor change to ConnectionHandler to generate and set connection ID, and LoggingFilter to use the logger, ensuring connection ID is included in the logs
1 parent f8c3809 commit b8aa660

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

src/main/java/org/juv25d/ConnectionHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public ConnectionHandler(Socket socket, HttpParser httpParser, Logger logger, Pi
2525

2626
@Override
2727
public void run() {
28+
String connectionId = java.util.UUID.randomUUID().toString().substring(0, 8);
29+
org.juv25d.logging.LogContext.setConnectionId(connectionId);
2830
try (socket) {
2931
var in = socket.getInputStream();
3032
var out = socket.getOutputStream();
@@ -56,6 +58,8 @@ public void run() {
5658

5759
} catch (IOException e) {
5860
logger.log(Level.SEVERE, "Error while handling request", e);
61+
} finally {
62+
org.juv25d.logging.LogContext.clear();
5963
}
6064
}
6165
}

src/main/java/org/juv25d/filter/LoggingFilter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
import org.juv25d.http.HttpRequest;
44
import org.juv25d.http.HttpResponse;
5+
import org.juv25d.logging.ServerLogging;
56

67
import java.io.IOException;
8+
import java.util.logging.Logger;
79

810
public class LoggingFilter implements Filter {
11+
private static final Logger logger = ServerLogging.getLogger();
912

1013
@Override
1114
public void doFilter(HttpRequest req, HttpResponse res, FilterChain chain) throws IOException {
12-
System.out.println(req.method() + " " + req.path());
15+
logger.info(req.method() + " " + req.path());
1316
chain.doFilter(req, res);
1417
}
1518
}

0 commit comments

Comments
 (0)