log4j2.properties file need to save in spring boot application
Create a new file named
log4j2.properties
in thesrc/main/resources
directory of your Spring Boot application.Open the
log4j2.properties
file and configure the desired logging settings using the Log4j 2 properties syntax.
Example configuration: ```properties status = error name = PropertiesConfig
property.filename = logs/app.log
appenders = console, file
appender.console.type = Console appender.console.name = STDOUT appender.console.layout.type = PatternLayout appender.console.layout.pattern = [%d{HH:mm:ss.SSS}] [%t] %-5level %logger{36} - %msg%n
appender.file.type = File appender.file.name = File appender.file.fileName = ${filename} appender.file.layout.type = PatternLayout appender.file.layout.pattern = [%d{HH:mm:ss.SSS}] [%t] %-5level %logger{36} - %msg%n
loggers = fileLogger
logger.fileLogger.name = com.example logger.fileLogger.level = debug logger.fileLogger.appenderRef.file.ref = File ```
Adjust the configuration based on your specific requirements.
Save the
log4j2.properties
file.Add the required dependencies to your
pom.xml
file to include Log4j 2 in your Spring Boot application.
xml
<dependencies>
<!-- Other dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- Other dependencies -->
</dependencies>
Make sure to exclude the default logging dependencies if needed.
Ensure that your Spring Boot application is configured to use the
log4j2.properties
file for logging.If you are using Spring Boot 2.x, no additional configuration is required.
For Spring Boot 1.x, you may need to specify the location of the
log4j2.properties
file in yourapplication.properties
orapplication.yml
:properties logging.config=classpath:log4j2.properties
Run your Spring Boot application, and Log4j 2 will use the specified
log4j2.properties
configuration for logging.