The motivation here is that I've spent just too much time trying to figure out how to add logging with log4j in spring boot in gradle. Here the gradle part was the one giving me the most grief because all the documentations I could find was for maven.
So I'm documenting my findings here so that next person who is looking for this might find my post and will be able to figure it out quickly.
configurations { all*.exclude module : 'spring-boot-starter-logging' } dependencies { ... implementation "org.springframework.boot:spring-boot-starter-log4j2" }
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="INFO"> <Appenders> <Console name="console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - Message: %msg%n"/> </Console> </Appenders> <Loggers> <Root level="trace"> <AppenderRef ref="console"/> </Root> </Loggers> </Configuration>
Your setup is done, now you can add logger from log4j and it will use this xml configuration. Notice that I've added a Message: in every message. So if you don't want it, you can just remove it. I've added it there so that I can test whether or not the project was using this xml file.
// import org.apache.logging.log4j.LogManager; // import org.apache.logging.log4j.Logger; Logger logger = LogManager.getLogger(SpringApplication.class); logger.info("Hello World!");
The logger setup should work as expected. Notice the Message: part.
You can find the project here
The above is the detailed content of Java Spring Boot use login a gradle project. For more information, please follow other related articles on the PHP Chinese website!