Spring @Service 中的引导 slf4j 记录器未记录
Spring Boot slf4j Logger in @Service not logging
我似乎无法在这里找到解决方案。我在应该记录但没有记录的服务 bean 中有一个简单的记录器。
下面是我的服务实现,以及使用该服务的控制器。
@Service
public class AuthService {
private static final Logger LOGGER = LoggerFactory.getLogger(AuthService.class);
...
public ResponseEntity<AbstractResponse> loginUser(final LoginDto loginDto) {
LOGGER.debug("New login request.");
final Optional<User> user = this.userRepository.findByEmail(loginDto.email());
// Check if user exists
if (user.isPresent()) {
final User userObj = user.get();
// Check if user has confirmed is account registration
if (userObj.isEnabled()) {
// Authenticate
final Authentication authentication = this.authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(loginDto.email(), loginDto.password()));
// Tell spring
SecurityContextHolder.getContext().setAuthentication(authentication);
// Create set of jwt
final String refreshJwt = this.jwtUtil.generateJwt(authentication, JwtType.REFRESH);
final String accessJwt = this.jwtUtil.generateJwt(authentication, JwtType.ACCESS);
// Build cookie
final ResponseCookie refreshCookie = this.createRefreshJwtCookie(refreshJwt);
final AbstractResponse abstractResponse = new AbstractResponse(200, null, accessJwt);
LOGGER.debug("User successfully authenticated.");
return ResponseEntity.ok().header(HttpHeaders.SET_COOKIE, refreshCookie.toString()).body(abstractResponse);
} else {
final AbstractResponse abstractResponse = new AbstractResponse(403, "Error: Email not confirmed.", null);
LOGGER.debug("Email for user " + loginDto.email() + " not confirmed.");
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(abstractResponse);
}
} else {
final AbstractResponse abstractResponse = new AbstractResponse(400, "Error: No user found.", null);
LOGGER.debug("No user with mail " + loginDto.email() + " found.");
return ResponseEntity.badRequest().body(abstractResponse);
}
}
控制器
@RestController
@RequestMapping(value = "/auth")
public class AuthController {
private static final Logger LOGGER = LoggerFactory.getLogger(AuthController.class);
private final AuthService authService;
@Autowired
public AuthController(final AuthService authService) {
this.authService = authService;
}
@PostMapping(value = "/login", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<AbstractResponse> login(@Valid @RequestBody final LoginDto loginDto) {
LOGGER.debug("Login request.");
return this.authService.loginUser(loginDto);
}
@PostMapping(value = "/register", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<AbstractResponse> register(@Valid @RequestBody final RegisterDto registerDto) {
return this.authService.registerUser(registerDto);
}
@PostMapping(value = "/register/confirm")
public ResponseEntity<AbstractResponse> confirmRegistration(@RequestParam(name = "ct") final UUID id,
@Valid @RequestBody final ConfirmationCodeDto confirmationCodeDto) {
return this.authService.confirmRegistration(id, confirmationCodeDto);
}
@PostMapping(value = "/logout")
public ResponseEntity<?> logout() {
return this.authService.logout();
}
@PostMapping(value = "/refresh")
public ResponseEntity<AbstractResponse> refresh(@CookieValue(name = "_jid") final String _jid) {
return this.authService.refresh(_jid);
}
}
我是不是遗漏了一些配置?真的不知道为什么我在控制台中看不到日志。
The default log configuration echoes messages to the console as they are written. By default, ERROR-level, WARN-level, and INFO-level messages are logged. You can also enable a “debug” mode by starting your application with a --debug flag.
$ java -jar myapp.jar --debug
You can also specify debug=true in your application.properties.
此外,如果您只想为您的包调试日志记录,您可以在 application.properties
文件中启用它,也可以使用 logging.level.your.package.name=DEBUG
或使用 logging.level.root=DEBUG
[=15= 启用它]
我似乎无法在这里找到解决方案。我在应该记录但没有记录的服务 bean 中有一个简单的记录器。
下面是我的服务实现,以及使用该服务的控制器。
@Service
public class AuthService {
private static final Logger LOGGER = LoggerFactory.getLogger(AuthService.class);
...
public ResponseEntity<AbstractResponse> loginUser(final LoginDto loginDto) {
LOGGER.debug("New login request.");
final Optional<User> user = this.userRepository.findByEmail(loginDto.email());
// Check if user exists
if (user.isPresent()) {
final User userObj = user.get();
// Check if user has confirmed is account registration
if (userObj.isEnabled()) {
// Authenticate
final Authentication authentication = this.authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(loginDto.email(), loginDto.password()));
// Tell spring
SecurityContextHolder.getContext().setAuthentication(authentication);
// Create set of jwt
final String refreshJwt = this.jwtUtil.generateJwt(authentication, JwtType.REFRESH);
final String accessJwt = this.jwtUtil.generateJwt(authentication, JwtType.ACCESS);
// Build cookie
final ResponseCookie refreshCookie = this.createRefreshJwtCookie(refreshJwt);
final AbstractResponse abstractResponse = new AbstractResponse(200, null, accessJwt);
LOGGER.debug("User successfully authenticated.");
return ResponseEntity.ok().header(HttpHeaders.SET_COOKIE, refreshCookie.toString()).body(abstractResponse);
} else {
final AbstractResponse abstractResponse = new AbstractResponse(403, "Error: Email not confirmed.", null);
LOGGER.debug("Email for user " + loginDto.email() + " not confirmed.");
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(abstractResponse);
}
} else {
final AbstractResponse abstractResponse = new AbstractResponse(400, "Error: No user found.", null);
LOGGER.debug("No user with mail " + loginDto.email() + " found.");
return ResponseEntity.badRequest().body(abstractResponse);
}
}
控制器
@RestController
@RequestMapping(value = "/auth")
public class AuthController {
private static final Logger LOGGER = LoggerFactory.getLogger(AuthController.class);
private final AuthService authService;
@Autowired
public AuthController(final AuthService authService) {
this.authService = authService;
}
@PostMapping(value = "/login", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<AbstractResponse> login(@Valid @RequestBody final LoginDto loginDto) {
LOGGER.debug("Login request.");
return this.authService.loginUser(loginDto);
}
@PostMapping(value = "/register", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<AbstractResponse> register(@Valid @RequestBody final RegisterDto registerDto) {
return this.authService.registerUser(registerDto);
}
@PostMapping(value = "/register/confirm")
public ResponseEntity<AbstractResponse> confirmRegistration(@RequestParam(name = "ct") final UUID id,
@Valid @RequestBody final ConfirmationCodeDto confirmationCodeDto) {
return this.authService.confirmRegistration(id, confirmationCodeDto);
}
@PostMapping(value = "/logout")
public ResponseEntity<?> logout() {
return this.authService.logout();
}
@PostMapping(value = "/refresh")
public ResponseEntity<AbstractResponse> refresh(@CookieValue(name = "_jid") final String _jid) {
return this.authService.refresh(_jid);
}
}
我是不是遗漏了一些配置?真的不知道为什么我在控制台中看不到日志。
The default log configuration echoes messages to the console as they are written. By default, ERROR-level, WARN-level, and INFO-level messages are logged. You can also enable a “debug” mode by starting your application with a --debug flag.
$ java -jar myapp.jar --debug
You can also specify debug=true in your application.properties.
此外,如果您只想为您的包调试日志记录,您可以在 application.properties
文件中启用它,也可以使用 logging.level.your.package.name=DEBUG
或使用 logging.level.root=DEBUG
[=15= 启用它]