繁体   English   中英

@Service 中的 Spring Boot slf4j 记录器未记录

[英]Spring Boot slf4j Logger in @Service not logging

我似乎无法在这里找到解决方案。 我在服务 bean 中有一个简单的 Logger,它应该记录,但没有。

下面是我对服务的实现,以及使用该服务的控制器。

@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);
    }
}

我在这里缺少一些配置吗? 真的不知道为什么我在控制台中看不到日志。

根据文档

默认日志配置在写入消息时将消息回显到控制台。 默认情况下,会记录 ERROR 级别、WARN 级别和 INFO 级别的消息。 您还可以通过使用 --debug 标志启动应用程序来启用“调试”模式。

$ java -jar myapp.jar --debug

您还可以在 application.properties 中指定 debug=true。

此外,如果您只想为您的包调试日志记录,您也可以在application.properties文件中使用logging.level.your.package.name=DEBUG或所有使用logging.level.root=DEBUG启用它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM