Logging Project Information With Log4j2

JavaScriptJavaScriptBeginner
Practice Now

Introduction

In this project, you will learn how to integrate the Log4j2 logging framework into a Spring Boot application. You will configure Log4j2 to log project information in both the console and log files.

👀 Preview

Unfinished
Unfinished

🎯 Tasks

In this project, you will learn:

  • How to modify the pom.xml file to add the necessary Log4j2 dependencies
  • How to configure Log4j2 in the application.properties file
  • How to create a logger and log information using different log levels in the DemoApplication.java file
  • How to package and run the Spring Boot application to view the logged information

🏆 Achievements

After completing this project, you will be able to:

  • Use the Log4j2 logging framework in a Spring Boot application
  • Configure Log4j2 to log information in the console and log files
  • Create a logger and use different log levels to log information
  • Package and run a Spring Boot application

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/ToolsandEnvironmentGroup(["`Tools and Environment`"]) javascript/BasicConceptsGroup -.-> javascript/functions("`Functions`") javascript/ToolsandEnvironmentGroup -.-> javascript/debugging("`Debugging`") subgraph Lab Skills javascript/functions -.-> lab-300380{{"`Logging Project Information With Log4j2`"}} javascript/debugging -.-> lab-300380{{"`Logging Project Information With Log4j2`"}} end

Modify the pom.xml file

In this step, you will learn how to modify the pom.xml file to integrate the Log4j2 logging framework into the Spring Boot project.

  1. Open the pom.xml file in the springbootlog4j2 project.

  2. Add the spring-boot-starter-log4j2 dependency to the <dependencies> section:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
  1. To exclude the default Logback dependency, add the following code to the <dependencies> section:
<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>

This will ensure that only Log4j2 is used as the logging framework in the project.

Configure Log4j2 in the application.properties file

In this step, you will learn how to configure the Log4j2 logging framework in the application.properties file.

  1. Open the application.properties file in the /src/main/resources/ directory.

  2. Add the following line to the file:

logging.config=classpath:log4j2.xml

This line tells the Spring Boot application to use the log4j2.xml configuration file located in the classpath.

Modify the DemoApplication.java file

In this step, you will learn how to create a logger and log the information using different log levels in the DemoApplication.java file.

  1. Open the DemoApplication.java file in the /src/main/java/org/labex/springbootlog4j2/ directory.

  2. Create a logger object using the following code:

private static final Logger logger = LoggerFactory.getLogger(DemoApplication.class);
  1. In the sayhello() method, add the following log statements:
logger.error("This is an error message");
logger.warn("This is a warning message");
logger.info("This is an info message");
logger.debug("This is a debug message");
  1. Note that we need to import the relevant classes:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

These log statements will log the information using the corresponding log levels.

Package and run the application

In this step, you will learn how to package the Spring Boot application and run it.

  1. Open a terminal and navigate to the springbootlog4j2 project directory.

  2. Run the following command to package the application:

cd springbootlog4j2
mvn clean package

This will generate a JAR file in the target directory.

Unfinished
  1. Run the application using the following command:
mvn spring-boot:run

After starting the service, first click Web 8080 above the environment, you can see the following running effect, and then return to the console to view the recorded information.

Unfinished
  1. To view the log files, navigate to the logs directory in the project. You should see the log files created by the Log4j2 configuration.
Unfinished

Congratulations! You have successfully integrated the Log4j2 logging framework into the Spring Boot project and configured it to log the information in the console and log files.

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like