1
votes

Per the spring boot documentation ( latest ), its mentioned that the spring boot uses logback internally.

I have used log4j starter as mentioned below excluding the logback

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
        </dependency>
    </dependencies>

and e.g. if below command is run in debug mode ( supposing sboot1 is my uber jar )

java -jar target\sboot1-1.0.jar --debug

I am not getting the debug logs which are generated earlier using logback which displays the "exclusions" and "inclusions" and other necessary info.

How to also get the --debug logs when using other logging frameworks like log4j?

1
It works for me. Have you remembered to pass the args in your main method into your SpringApplication? Like this: SpringApplication.run(YourApplication.class, args); - Andy Wilkinson
Thank you for the response @AndyWilkinson, but yes args are passed as parameter. Uploaded the sample code here. Try running the command java -jar target\sboot1-1.0.jar --debug after uncommenting the log4j starter dependencies from pom.xml, those logs wont be printed and try the other way by commenting, the logs will be printed with logback dependency. - Kartik Narayana Maringanti

1 Answers

2
votes

When you start your application you'll see three warning messages from Log4j:

log4j:WARN No appenders could be found for logger (org.springframework.boot.SpringApplication).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

They're telling you that you haven't configured Log4j correctly.

Looking at your log4j-spring.properties file, you haven't configured log4j.rootCategory and the only logger that you have configured is log4j.logger.com.mnkartik. This means that any logging performed by code outside of your com.mnkartik package doesn't have any appenders configured, hence the first of the three warning messages.

Add the following to your log4j-spring.properties file:

logging.rootCategory=INFO, consoleAppender, fileAppender

This means that all loggers outside of com.mnkartik will log at INFO level and will write to both the console and file appenders. As a result, --debug should now have the desired effect as code in org.springframework.boot.* now has somewhere to log to.

As an aside, it probably also worth pointing out that Apache have announced the end-of-life for Log4j and, as a result, it's deprecated in Spring Boot 1.3 and will be removed in 1.4. You should consider migrating to Logback of Log4j 2.