Skip to content

Commit d93b033

Browse files
committed
task(area/logging) : Include userId in log messages by using MDC
Related links: - http://logback.qos.ch/manual/mdc.html - http://docs.spring.io/spring-boot/docs/1.5.x/reference/html/boot-features-logging.html Part of #388 Fix #1102
1 parent d553f22 commit d93b033

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

src/main/java/ru/mystamps/web/support/spring/security/SecurityConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,9 @@ public FilterRegistrationBean getResetLocaleFilter(
203203
return bean;
204204
}
205205

206+
@Bean
207+
public UserMdcLoggingFilter userMdcLoggingFilter() {
208+
return new UserMdcLoggingFilter();
209+
}
210+
206211
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (C) 2009-2019 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.support.spring.security;
19+
20+
import org.slf4j.MDC;
21+
import org.springframework.web.filter.OncePerRequestFilter;
22+
23+
import javax.servlet.FilterChain;
24+
import javax.servlet.ServletException;
25+
import javax.servlet.http.HttpServletRequest;
26+
import javax.servlet.http.HttpServletResponse;
27+
import java.io.IOException;
28+
29+
public class UserMdcLoggingFilter extends OncePerRequestFilter {
30+
private static final String USER_ID="userId";
31+
32+
@Override
33+
protected void doFilterInternal(
34+
HttpServletRequest request,
35+
HttpServletResponse response,
36+
FilterChain filterChain)
37+
throws ServletException, IOException {
38+
Integer userId = SecurityContextUtils.getUserId();
39+
if (userId != null) {
40+
MDC.put(USER_ID, userId.toString());
41+
}
42+
try {
43+
filterChain.doFilter(request, response);
44+
} finally {
45+
MDC.remove(USER_ID);
46+
}
47+
}
48+
49+
}

src/main/resources/application.properties

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ server.compression.min-response-size: 512
3636

3737
security.filter-dispatcher-types: REQUEST, ERROR
3838

39+
# See for details:
40+
# https://docs.spring.io/spring-boot/docs/1.5.x/reference/html/boot-features-logging.html#boot-features-custom-log-configuration
41+
# https://logback.qos.ch/manual/layouts.html
42+
logging.pattern.level: [user:%X{userId}] %5p
43+
3944
app.mail.admin.email: slava.semushin@gmail.com
4045
app.mail.admin.lang: ru
4146
app.mail.robot.email: dont-reply@my-stamps.ru

0 commit comments

Comments
 (0)