samshers
samshers

Reputation: 3660

Spring Boot logging - log4j2 logging not working?

There are many post on internet advising on how to move from logback to log4j2 for spring boot logging.
Referring to this sof post -Spring-Boot logging with log4j2 - i have configured my project to use log4j2. And then I added some basic log4j2.xml and log4j2.properties file to test.

But my project is not able to do any logging after these changes. Can someone help me find the fault?

The project code is available on github - https://github.com/ramshers/spring-security-jwt/tree/log4j2_configuration on branch: log4j2_configuration and commit-id: 5e65e7d

Here is the project pom.xml - https://github.com/ramshers/spring-security-jwt/blob/log4j2_configuration/pom.xml#L26
And here are the log4j2 properties file -
https://github.com/ramshers/spring-security-jwt/blob/log4j2_configuration/src/main/resources/log4j2.properties
https://github.com/ramshers/spring-security-jwt/blob/log4j2_configuration/src/main/resources/log4j2.xml

Upvotes: 0

Views: 584

Answers (1)

kosmasd
kosmasd

Reputation: 334

I had a look at the repository code, and made some changes to the properties file. You can use the below as a starting point (note: the pattern is different here, just modify it to your wishes). Also note: I had to remove the properties from your application.properties file, as they are now being picked up from the log4j2.properties file.

log4j2 properties file

rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

appenders = console
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern =%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

Modify main class to test it is working

@SpringBootApplication
public class SpringSecurityJwtApplication {

    private  static final Logger logger = LoggerFactory.getLogger(SpringSecurityJwtApplication.class);
    
    public static void main(String[] args) {
        
        SpringApplication.run(SpringSecurityJwtApplication.class, args);
        
        logger.info("Running..., info level");
        logger.debug("Running..., debug level");
    }
}

Upvotes: 1

Related Questions