Skip to content

Commit

Permalink
feat: Add user identification to logging
Browse files Browse the repository at this point in the history
fix: do not show jetty EofException
  • Loading branch information
TatuJLund committed Oct 13, 2024
1 parent e3e0268 commit 62325e8
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private ProductDataServiceImpl() {
logger.info("Generated mock product data");
}

public static ProductDataService getInstance() {
public static synchronized ProductDataService getInstance() {
if (instance == null) {
instance = new ProductDataServiceImpl();
}
Expand Down
5 changes: 5 additions & 0 deletions vaadincreate-ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-io</artifactId>
<version>${jetty.plugin.version}</version>
</dependency>

<dependency>
<groupId>com.vaadin</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.vaadin.tatu.vaadincreate;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.MDC;
import org.vaadin.tatu.vaadincreate.auth.CurrentUser;
import org.vaadin.tatu.vaadincreate.backend.data.User;

import java.io.IOException;

@WebFilter("/*")
public class LoggingFilter implements Filter {

@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpSession session = httpRequest.getSession(false);
if (session != null) {
// Add user id to log messages if the user is logged in
var user = (User) session.getAttribute(
CurrentUser.CURRENT_USER_SESSION_ATTRIBUTE_KEY);
if (user != null) {
var userId = String.format("[%s/%s]", user.getRole().toString(),
user.getName());
MDC.put("userId", userId);
}
}
// Pass the request along the filter chain
chain.doFilter(request, response);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;

import org.eclipse.jetty.io.EofException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.vaadin.tatu.vaadincreate.admin.AdminView;
import org.vaadin.tatu.vaadincreate.auth.AccessControl;
import org.vaadin.tatu.vaadincreate.auth.BasicAccessControl;
import org.vaadin.tatu.vaadincreate.auth.CurrentUser;
import org.vaadin.tatu.vaadincreate.backend.AppDataService;
import org.vaadin.tatu.vaadincreate.backend.ProductDataService;
import org.vaadin.tatu.vaadincreate.backend.UserService;
import org.vaadin.tatu.vaadincreate.backend.data.Message;
import org.vaadin.tatu.vaadincreate.backend.data.Product;
import org.vaadin.tatu.vaadincreate.backend.data.User;
import org.vaadin.tatu.vaadincreate.crud.BooksView;
import org.vaadin.tatu.vaadincreate.eventbus.EventBus;
import org.vaadin.tatu.vaadincreate.eventbus.EventBus.EventBusListener;
Expand Down Expand Up @@ -228,6 +232,16 @@ private EventBus getEventBus() {
public ExecutorService getExecutor() {
if (executor == null) {
executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
// Add user id to MDC for logging
var user = (User) getSession().getSession().getAttribute(
CurrentUser.CURRENT_USER_SESSION_ATTRIBUTE_KEY);
if (user != null) {
var userId = String.format("[%s/%s]",
user.getRole().toString(), user.getName());
MDC.put("userId", userId);
}
});
}
return executor;
}
Expand Down Expand Up @@ -261,16 +275,20 @@ protected void servletInitialized() {
session.addRequestHandler(this::handleRequest);
session.setAttribute(WrappedSession.class,
session.getSession());
// Set error handler for the session to catch all exceptions
// Set error handler for the session to login all exceptions
// happening in the session and show them to the user in the UI
session.setErrorHandler(errorHandler -> {
var message = errorHandler.getThrowable()
.getLocalizedMessage();
session.getUIs()
.forEach(ui -> ui.access(() -> ((VaadinCreateUI) ui)
.showInternalError(message)));
logger.error("Exception happened",
errorHandler.getThrowable());
var throwable = errorHandler.getThrowable();
// It is Jetty vs. Firefox issue, hence not showing to user
// https://github.com/jetty/jetty.project/issues/9763
if (!(throwable instanceof EofException)) {
var message = throwable.getLocalizedMessage();
session.getUIs().forEach(
ui -> ui.access(() -> ((VaadinCreateUI) ui)
.showInternalError(message)));
logger.error("Exception happened",
errorHandler.getThrowable());
}
});
});
getService().addSessionDestroyListener(event -> {
Expand Down
5 changes: 4 additions & 1 deletion vaadincreate-ui/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg %X{userId} %n</pattern>
</encoder>
</appender>

<root level="debug">
<appender-ref ref="STDOUT"/>
</root>

<logger name="org.eclipse.jetty" level="info" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="org.hibernate" level="info" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
Expand Down

0 comments on commit 62325e8

Please sign in to comment.