Skip to content

Commit b73e2c2

Browse files
committed
replace custom logger with logback.xml
1 parent ad884eb commit b73e2c2

File tree

11 files changed

+62
-110
lines changed

11 files changed

+62
-110
lines changed

backend/spring-boot/src/main/java/org/bugzkit/api/auth/oauth2/OAuth2FailureHandler.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,24 @@
55
import java.io.IOException;
66
import java.net.URLEncoder;
77
import java.nio.charset.StandardCharsets;
8-
import org.bugzkit.api.shared.logger.CustomLogger;
8+
import lombok.extern.slf4j.Slf4j;
99
import org.bugzkit.api.shared.message.service.MessageService;
1010
import org.slf4j.MDC;
1111
import org.springframework.beans.factory.annotation.Value;
1212
import org.springframework.security.core.AuthenticationException;
1313
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
1414
import org.springframework.stereotype.Component;
1515

16+
@Slf4j
1617
@Component
1718
public class OAuth2FailureHandler implements AuthenticationFailureHandler {
1819
private final MessageService messageService;
19-
private final CustomLogger customLogger;
2020

2121
@Value("${ui.url}")
2222
private String uiUrl;
2323

24-
public OAuth2FailureHandler(MessageService messageService, CustomLogger customLogger) {
24+
public OAuth2FailureHandler(MessageService messageService) {
2525
this.messageService = messageService;
26-
this.customLogger = customLogger;
2726
}
2827

2928
@Override
@@ -33,8 +32,8 @@ public void onAuthenticationFailure(
3332
final var errorCode = getCode(exception);
3433
response.sendRedirect(
3534
uiUrl + "/auth/sign-in?error=" + URLEncoder.encode(errorCode, StandardCharsets.UTF_8));
36-
customLogger.error("OAuth failed", exception);
37-
MDC.remove("REQUEST_ID");
35+
log.error("OAuth failed", exception);
36+
MDC.clear();
3837
}
3938

4039
private String getCode(AuthenticationException exception) {

backend/spring-boot/src/main/java/org/bugzkit/api/auth/oauth2/OAuth2SuccessHandler.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import jakarta.servlet.http.HttpServletResponse;
55
import java.io.IOException;
66
import java.util.stream.Collectors;
7+
import lombok.extern.slf4j.Slf4j;
78
import org.bugzkit.api.auth.service.AccessTokenService;
89
import org.bugzkit.api.auth.service.DeviceService;
910
import org.bugzkit.api.auth.service.RefreshTokenService;
1011
import org.bugzkit.api.auth.util.AuthUtil;
11-
import org.bugzkit.api.shared.logger.CustomLogger;
1212
import org.bugzkit.api.user.payload.dto.RoleDTO;
1313
import org.slf4j.MDC;
1414
import org.springframework.beans.factory.annotation.Value;
@@ -17,12 +17,12 @@
1717
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
1818
import org.springframework.stereotype.Component;
1919

20+
@Slf4j
2021
@Component
2122
public class OAuth2SuccessHandler implements AuthenticationSuccessHandler {
2223
private final AccessTokenService accessTokenService;
2324
private final RefreshTokenService refreshTokenService;
2425
private final DeviceService deviceService;
25-
private final CustomLogger customLogger;
2626

2727
@Value("${domain.name}")
2828
private String domain;
@@ -39,12 +39,10 @@ public class OAuth2SuccessHandler implements AuthenticationSuccessHandler {
3939
public OAuth2SuccessHandler(
4040
AccessTokenService accessTokenService,
4141
RefreshTokenService refreshTokenService,
42-
DeviceService deviceService,
43-
CustomLogger customLogger) {
42+
DeviceService deviceService) {
4443
this.accessTokenService = accessTokenService;
4544
this.refreshTokenService = refreshTokenService;
4645
this.deviceService = deviceService;
47-
this.customLogger = customLogger;
4846
}
4947

5048
@Override
@@ -68,7 +66,7 @@ public void onAuthenticationSuccess(
6866
response.addHeader(HttpHeaders.SET_COOKIE, accessTokenCookie.toString());
6967
response.addHeader(HttpHeaders.SET_COOKIE, refreshTokenCookie.toString());
7068
response.sendRedirect(uiUrl);
71-
customLogger.info("Finished");
72-
MDC.remove("REQUEST_ID");
69+
log.info("OAuth finished");
70+
MDC.clear();
7371
}
7472
}

backend/spring-boot/src/main/java/org/bugzkit/api/auth/oauth2/OAuth2UserService.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import java.util.Collections;
44
import java.util.UUID;
5+
import lombok.extern.slf4j.Slf4j;
56
import org.bugzkit.api.auth.security.UserPrincipal;
6-
import org.bugzkit.api.shared.logger.CustomLogger;
77
import org.bugzkit.api.user.model.Role.RoleName;
88
import org.bugzkit.api.user.model.User;
99
import org.bugzkit.api.user.repository.RoleRepository;
@@ -16,24 +16,21 @@
1616
import org.springframework.security.oauth2.core.user.OAuth2User;
1717
import org.springframework.stereotype.Service;
1818

19+
@Slf4j
1920
@Service
2021
public class OAuth2UserService extends DefaultOAuth2UserService {
2122
private final UserRepository userRepository;
2223
private final RoleRepository roleRepository;
23-
private final CustomLogger customLogger;
2424

25-
public OAuth2UserService(
26-
UserRepository userRepository, RoleRepository roleRepository, CustomLogger customLogger) {
25+
public OAuth2UserService(UserRepository userRepository, RoleRepository roleRepository) {
2726
this.userRepository = userRepository;
2827
this.roleRepository = roleRepository;
29-
this.customLogger = customLogger;
3028
}
3129

3230
@Override
3331
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
34-
final var requestId = UUID.randomUUID().toString();
35-
MDC.put("REQUEST_ID", requestId);
36-
customLogger.info("Called");
32+
MDC.put("REQUEST_ID", UUID.randomUUID().toString());
33+
log.info("OAuth called");
3734
final var oAuthUser = super.loadUser(userRequest);
3835
final String email = oAuthUser.getAttribute("email");
3936
final boolean emailVerified = Boolean.TRUE.equals(oAuthUser.getAttribute("email_verified"));

backend/spring-boot/src/main/java/org/bugzkit/api/shared/error/handling/CustomAuthenticationEntryPoint.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import jakarta.servlet.http.HttpServletResponse;
66
import java.io.IOException;
77
import java.util.UUID;
8+
import lombok.extern.slf4j.Slf4j;
89
import org.bugzkit.api.shared.error.ErrorMessage;
9-
import org.bugzkit.api.shared.logger.CustomLogger;
1010
import org.bugzkit.api.shared.message.service.MessageService;
1111
import org.slf4j.MDC;
1212
import org.springframework.http.HttpStatus;
@@ -15,14 +15,13 @@
1515
import org.springframework.security.web.AuthenticationEntryPoint;
1616
import org.springframework.stereotype.Component;
1717

18+
@Slf4j
1819
@Component
1920
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
2021
private final MessageService messageService;
21-
private final CustomLogger customLogger;
2222

23-
public CustomAuthenticationEntryPoint(MessageService messageService, CustomLogger customLogger) {
23+
public CustomAuthenticationEntryPoint(MessageService messageService) {
2424
this.messageService = messageService;
25-
this.customLogger = customLogger;
2625
}
2726

2827
@Override
@@ -31,13 +30,15 @@ public void commence(
3130
throws IOException {
3231
final var requestId = UUID.randomUUID().toString();
3332
MDC.put("REQUEST_ID", requestId);
33+
MDC.put("REQUEST_METHOD", request.getMethod());
34+
MDC.put("REQUEST_URL", request.getRequestURL().toString());
3435
final var errorMessage = new ErrorMessage(HttpStatus.UNAUTHORIZED);
3536
errorMessage.addCode(messageService.getMessage("auth.unauthorized"));
3637
response.setHeader(HttpHeaders.X_REQUEST_ID, requestId);
3738
response.setContentType(MediaType.APPLICATION_PROBLEM_JSON_VALUE);
3839
response.setStatus(HttpStatus.UNAUTHORIZED.value());
3940
response.getOutputStream().println(errorMessage.toString());
40-
customLogger.error("Auth failed", e);
41-
MDC.remove("REQUEST_ID");
41+
log.error("Auth failed", e);
42+
MDC.clear();
4243
}
4344
}

backend/spring-boot/src/main/java/org/bugzkit/api/shared/error/handling/CustomExceptionHandler.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package org.bugzkit.api.shared.error.handling;
22

33
import jakarta.annotation.Nonnull;
4+
import lombok.extern.slf4j.Slf4j;
45
import org.bugzkit.api.shared.error.ErrorMessage;
56
import org.bugzkit.api.shared.error.exception.BadRequestException;
67
import org.bugzkit.api.shared.error.exception.ConflictException;
78
import org.bugzkit.api.shared.error.exception.ResourceNotFoundException;
89
import org.bugzkit.api.shared.error.exception.TooManyRequestsException;
910
import org.bugzkit.api.shared.error.exception.UnauthorizedException;
10-
import org.bugzkit.api.shared.logger.CustomLogger;
1111
import org.bugzkit.api.shared.message.service.MessageService;
1212
import org.springframework.http.HttpHeaders;
1313
import org.springframework.http.HttpStatus;
@@ -28,14 +28,13 @@
2828
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
2929
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
3030

31+
@Slf4j
3132
@ControllerAdvice
3233
public class CustomExceptionHandler extends ResponseEntityExceptionHandler {
3334
private final MessageService messageService;
34-
private final CustomLogger customLogger;
3535

36-
public CustomExceptionHandler(MessageService messageService, CustomLogger customLogger) {
36+
public CustomExceptionHandler(MessageService messageService) {
3737
this.messageService = messageService;
38-
this.customLogger = customLogger;
3938
}
4039

4140
@Override
@@ -44,7 +43,7 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(
4443
@Nonnull HttpHeaders headers,
4544
@Nonnull HttpStatusCode statusCode,
4645
@Nonnull WebRequest request) {
47-
customLogger.error("Invalid arguments", e);
46+
log.error("Invalid arguments", e);
4847
final var status = (HttpStatus) statusCode;
4948
final var errorMessage = new ErrorMessage(status);
5049
final var result = e.getBindingResult();
@@ -67,57 +66,57 @@ private HttpHeaders setHeaders() {
6766

6867
@ExceptionHandler({BadRequestException.class})
6968
public ResponseEntity<Object> handleBadRequestException(BadRequestException e) {
70-
customLogger.error("Bad request", e);
69+
log.error("Bad request", e);
7170
return createError(e.getStatus(), messageService.getMessage(e.getMessage()));
7271
}
7372

7473
@ExceptionHandler({UnauthorizedException.class})
7574
public ResponseEntity<Object> handleUnauthorizedException(UnauthorizedException e) {
76-
customLogger.error("Unauthorized", e);
75+
log.error("Unauthorized", e);
7776
return createError(e.getStatus(), messageService.getMessage(e.getMessage()));
7877
}
7978

8079
@ExceptionHandler({ResourceNotFoundException.class})
8180
public ResponseEntity<Object> handleResourceNotFoundException(ResourceNotFoundException e) {
82-
customLogger.error("Resource not found", e);
81+
log.error("Resource not found", e);
8382
return createError(e.getStatus(), messageService.getMessage(e.getMessage()));
8483
}
8584

8685
@ExceptionHandler({ConflictException.class})
8786
public ResponseEntity<Object> handleConflictException(ConflictException e) {
88-
customLogger.error("Conflict", e);
87+
log.error("Conflict", e);
8988
return createError(e.getStatus(), messageService.getMessage(e.getMessage()));
9089
}
9190

9291
@ExceptionHandler({TooManyRequestsException.class})
9392
public ResponseEntity<Object> handleTooManyRequestsException(TooManyRequestsException e) {
94-
customLogger.error("Too many requests", e);
93+
log.error("Too many requests", e);
9594
return createError(e.getStatus(), messageService.getMessage(e.getMessage()));
9695
}
9796

9897
@ExceptionHandler({AuthenticationException.class})
9998
public ResponseEntity<Object> handleAuthenticationException(AuthenticationException e) {
10099
if (e instanceof DisabledException) {
101-
customLogger.error("User not active", e);
100+
log.error("User not active", e);
102101
return createError(HttpStatus.FORBIDDEN, messageService.getMessage("user.notActive"));
103102
} else if (e instanceof LockedException) {
104-
customLogger.error("User locked", e);
103+
log.error("User locked", e);
105104
return createError(HttpStatus.FORBIDDEN, messageService.getMessage("user.lock"));
106105
} else {
107-
customLogger.error("Auth failed", e);
106+
log.error("Auth failed", e);
108107
return createError(HttpStatus.UNAUTHORIZED, messageService.getMessage("auth.unauthorized"));
109108
}
110109
}
111110

112111
@ExceptionHandler({AuthorizationDeniedException.class})
113112
public ResponseEntity<Object> handleAuthorizationDeniedException(AuthorizationDeniedException e) {
114-
customLogger.error("Forbidden", e);
113+
log.error("Forbidden", e);
115114
return createError(HttpStatus.FORBIDDEN, messageService.getMessage("auth.forbidden"));
116115
}
117116

118117
@ExceptionHandler({Exception.class})
119118
public ResponseEntity<Object> handleGlobalException(Exception e) {
120-
customLogger.error("Exception", e);
119+
log.error("Exception", e);
121120
return createError(
122121
HttpStatus.INTERNAL_SERVER_ERROR, messageService.getMessage("server.internalError"));
123122
}
@@ -128,7 +127,7 @@ protected ResponseEntity<Object> handleMissingServletRequestParameter(
128127
@Nonnull HttpHeaders headers,
129128
@Nonnull HttpStatusCode statusCode,
130129
@Nonnull WebRequest request) {
131-
customLogger.error("Parameter missing", e);
130+
log.error("Parameter missing", e);
132131
return createError(
133132
(HttpStatus) statusCode, messageService.getMessage("request.parameterMissing"));
134133
}
@@ -139,7 +138,7 @@ protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(
139138
@Nonnull HttpHeaders headers,
140139
@Nonnull HttpStatusCode statusCode,
141140
@Nonnull WebRequest request) {
142-
customLogger.error("Method not supported", e);
141+
log.error("Method not supported", e);
143142
return createError(
144143
(HttpStatus) statusCode, messageService.getMessage("request.methodNotSupported"));
145144
}
@@ -150,15 +149,15 @@ protected ResponseEntity<Object> handleHttpMessageNotReadable(
150149
@Nonnull HttpHeaders headers,
151150
@Nonnull HttpStatusCode statusCode,
152151
@Nonnull WebRequest request) {
153-
customLogger.error("Message not readable", e);
152+
log.error("Message not readable", e);
154153
return createError(
155154
(HttpStatus) statusCode, messageService.getMessage("request.messageNotReadable"));
156155
}
157156

158157
@ExceptionHandler({MethodArgumentTypeMismatchException.class})
159158
public ResponseEntity<Object> handleMethodArgumentTypeMismatch(
160159
MethodArgumentTypeMismatchException e) {
161-
customLogger.error("Parameter type mismatch", e);
160+
log.error("Parameter type mismatch", e);
162161
return createError(
163162
HttpStatus.BAD_REQUEST, messageService.getMessage("request.parameterTypeMismatch"));
164163
}

backend/spring-boot/src/main/java/org/bugzkit/api/shared/interceptor/RequestInterceptor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import jakarta.servlet.http.HttpServletRequest;
66
import jakarta.servlet.http.HttpServletResponse;
77
import java.util.UUID;
8+
import org.bugzkit.api.auth.util.AuthUtil;
89
import org.slf4j.MDC;
910
import org.springframework.stereotype.Component;
1011
import org.springframework.web.servlet.HandlerInterceptor;
@@ -19,6 +20,9 @@ public boolean preHandle(
1920
final var requestId = UUID.randomUUID().toString();
2021
response.setHeader(HttpHeaders.X_REQUEST_ID, requestId);
2122
MDC.put("REQUEST_ID", requestId);
23+
MDC.put("USER", AuthUtil.getAuthName());
24+
MDC.put("REQUEST_METHOD", request.getMethod());
25+
MDC.put("REQUEST_URL", request.getRequestURL().toString());
2226
return true;
2327
}
2428

@@ -28,6 +32,6 @@ public void afterCompletion(
2832
@Nonnull HttpServletResponse response,
2933
@Nonnull Object handler,
3034
Exception ex) {
31-
MDC.remove("REQUEST_ID");
35+
MDC.clear();
3236
}
3337
}

backend/spring-boot/src/main/java/org/bugzkit/api/shared/logger/AspectLogger.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,20 @@
1111
@Aspect
1212
@Component
1313
public class AspectLogger {
14-
private final CustomLogger customLogger;
15-
16-
public AspectLogger(CustomLogger customLogger) {
14+
public AspectLogger() {
1715
log.info("AspectLogger Initialized");
18-
this.customLogger = customLogger;
1916
}
2017

2118
@Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
2219
public void controllerLayer() {}
2320

2421
@Before(value = "controllerLayer()")
2522
public void logBefore() {
26-
customLogger.info("Called");
23+
log.info("Called");
2724
}
2825

2926
@AfterReturning(value = "controllerLayer()")
3027
public void logAfter() {
31-
customLogger.info("Finished");
28+
log.info("Finished");
3229
}
3330
}

0 commit comments

Comments
 (0)