-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding identity_id from keycloak to the logs through MDC Context
Signed-off-by: Sun Tan <sutan@redhat.com>
- Loading branch information
Showing
5 changed files
with
74 additions
and
0 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
56 changes: 56 additions & 0 deletions
56
...-logback/src/main/java/org/eclipse/che/commons/logback/filter/IdentityIdLoggerFilter.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,56 @@ | ||
/* | ||
* Copyright (c) 2012-2017 Red Hat, Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.che.commons.logback.filter; | ||
|
||
import java.io.IOException; | ||
import javax.inject.Singleton; | ||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.FilterConfig; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpSession; | ||
import org.eclipse.che.commons.subject.Subject; | ||
import org.slf4j.MDC; | ||
|
||
/** | ||
* A servlet filter that retrieves the identity_id from the servlet context and put it in the MDC | ||
* context. Logback can be configured to display this value in each log message when available. MDC | ||
* property name is `identity_id`. | ||
*/ | ||
@Singleton | ||
public class IdentityIdLoggerFilter implements Filter { | ||
|
||
private static final String IDENTITY_ID_MDC_KEY = "identity_id"; | ||
|
||
@Override | ||
public void init(FilterConfig filterConfig) throws ServletException {} | ||
|
||
@Override | ||
public final void doFilter( | ||
ServletRequest request, ServletResponse response, FilterChain filterChain) | ||
throws IOException, ServletException { | ||
final HttpServletRequest httpRequest = (HttpServletRequest) request; | ||
final HttpSession session = httpRequest.getSession(); | ||
Subject subject = (Subject) session.getAttribute("che_subject"); | ||
|
||
if (subject != null && subject.getUserId() != null) { | ||
MDC.put(IDENTITY_ID_MDC_KEY, subject.getUserId()); | ||
} | ||
|
||
filterChain.doFilter(request, response); | ||
} | ||
|
||
@Override | ||
public void destroy() {} | ||
} |
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
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