简体   繁体   中英

Spring Boot slf4j Logger in @Service not logging

i cant seem to find the solution here. I have a simple Logger in a service bean which should log, but doesnt.

Below is my implementation of the service, and the controller in which the service is used.

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

Controller

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

Am i missing some configuration here? Really clueless on why i dont see logs in my console.

As per documentation :

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.

Furthermore, if you want debug logging only for your package, you can enable it in the application.properties file as well with logging.level.your.package.name=DEBUG or for all with logging.level.root=DEBUG

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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