-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add user identification to logging
fix: do not show jetty EofException
- Loading branch information
TatuJLund
committed
Oct 13, 2024
1 parent
e3e0268
commit 62325e8
Showing
5 changed files
with
76 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
vaadincreate-ui/src/main/java/org/vaadin/tatu/vaadincreate/LoggingFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters