Skip to content

task : include userId in the log messages by using MDC. #1103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,13 @@ public FilterRegistrationBean getResetLocaleFilter(
return bean;
}

@Bean
public FilterRegistrationBean userMdcLoggingFilter() {
FilterRegistrationBean bean = new FilterRegistrationBean(
new UserMdcLoggingFilter()
);
bean.addUrlPatterns("/*");
return bean;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2009-2019 Slava Semushin <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package ru.mystamps.web.support.spring.security;

import org.slf4j.MDC;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserMdcLoggingFilter extends OncePerRequestFilter {
private static final String USER_ID = "userId";

@Override
protected void doFilterInternal(
HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain)
throws ServletException, IOException {

Integer userId = SecurityContextUtils.getUserId();
if (userId != null) {
MDC.put(USER_ID, userId.toString());
}

try {
filterChain.doFilter(request, response);
} finally {
MDC.remove(USER_ID);
}
}

}
5 changes: 5 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ server.compression.min-response-size: 512

security.filter-dispatcher-types: REQUEST, ERROR

# See for details:
# https://docs.spring.io/spring-boot/docs/1.5.x/reference/html/boot-features-logging.html#boot-features-custom-log-configuration
# https://logback.qos.ch/manual/layouts.html
logging.pattern.level: [user:%-2X{userId}] %5p

app.mail.admin.email: [email protected]
app.mail.admin.lang: ru
app.mail.robot.email: [email protected]
Expand Down