javaapachespring-bootloggingapache-commons-logging

Logging doesnt work with Spring boot 2.1.1 and JDK 11 upgrade


I had recently upgraded my application to use Spring Boot 2.1.1.RELEASE and JDK 11 with tomcat 9.0.14. Since then the logging is not working. All was good before. Below is a simple logging I am trying to get to work.

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoggingController {
private static final Logger logger = LogManager.getLogger(LoggingController.class);

@RequestMapping("/")
public String index() {
    logger.info("This is an INFO message.");
    logger.warn("This is a WARN message.");
    logger.error("You guessed it, an ERROR message.");
    logger.trace("This is a TRACE message.");
    logger.debug("This is a DEBUG message.");

    return "Welcome to Spring Logging! Check the console to see the log messages.";
}
}

As per the link, I have added the below in my application.properties

logging.level.web=debug

Has anything else been changed? I can see that logging does work with slf4j imports

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

But I would not like to update this all over my application.


Solution

  • Replacing the imports

    import org.apache.log4j.LogManager;
    import org.apache.log4j.Logger;
    

    with the below solved the issue.

    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    

    Not sure if there is any better fix rather than replacing them. Will be good to know if anyone faced the similar issue or has a different solution.