Spring Boot Series--how to integrate LOG4J2

Source: Internet
Author: User
Tags java util log4j

The Spring boot series, log configuration, describes how spring boot logs are configured, using the spring boot default logback for the log system.

In fact, in addition to using the default logback,spring boot, you can use log4j, Log42, and so on as your own log system. Let's take a log4j2 today to illustrate how spring boot integrates with other log systems.

Add a Jar package dependency

As mentioned in the previous article, Spring boot uses logback by default, but we don't see the jar packages that show dependencies, because the jar packages are spring-boot-starter-logging spring-boot-starter-web part of or dependent on them spring-boot-starter .

If you want to use log4j2 here, you need to remove the dependencies from the, and spring-boot-starter-web spring-boot-starter-logging also show the dependency jar package that declares the use of log4j2, as follows

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</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-log4j2</artifactId></dependency>

Here by the way, in the dependencies above, we see that the version is not declared because I introduced dependencymanagement in the project's parent pom file.

Roughly speaking, our common dependency tags are used to introduce jars that need to be relied upon. While Dependencymanagement does not play the same role, its role can be used to declare version specifications. When the parent Pom declares a version of the dependency, if the child Pom project is not used, it is not dependent on the declaration of the jar package, need to be active in the child Pom to add the dependency to take effect, this parent pom dependencymanagement is used to do a unified version.

See the parent Pom file configuration in Rome project

<?xml version= "1.0" encoding= "UTF-8"?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0/http Maven.apache.org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupId>    Com.jackie</groupid> <artifactId>rome</artifactId> <version>1.0-SNAPSHOT</version> <name>rome</name> <packaging>pom</packaging> <modules> <module>springboo t</module> <module>wowjava</module> </modules> <properties> <lombok.v Ersion>1.16.18</lombok.version> <spring.boot.version>2.0.4.RELEASE</spring.boot.version> &L                t;/properties> <dependencyManagement> <dependencies> <dependency>                <groupId>org.projectlombok</groupId><artifactId>lombok</artifactId> <version>${lombok.version}</version> &lt                ;/dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${spring.boot.versio n}</version> </dependency> <dependency> &LT;GROUPID&GT;ORG.SPRINGFR                Amework.boot</groupid> <artifactId>spring-boot-starter-log4j2</artifactId>                <version>${spring.boot.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-test </artifactId> <scope>test</scope> <version>${spring.boot.version}&lt           ;/version> </dependency> </dependencies> </dependencyManagement></project> 

The denpendency in Denpendencymanagement here declares the version, so that when the child Pom inherits the parent Pom, such as this spring-boot-starter-log4j2 can be inherited from the parent Pom declared 2.0.4.RELEASE , no need to write the version label. This is a convenient version of the project to unify.

Add configuration file Log4j2.xml

Create a new Log4j2.xml file under the resources directory.

<?xml version="1.0" encoding="UTF-8"?> <configuration>    <Appenders>        <Console name="CONSOLE" target="SYSTEM_OUT">            <PatternLayout charset="UTF-8" pattern="[%-5p] %d %c - %m%n" />        </Console>        <File name="File" fileName="/Users/jackie/workspace/rome/springboot.log">            <PatternLayout pattern="%m%n" />        </File>    </Appenders>    <Loggers>        <root level="info">            <AppenderRef ref="CONSOLE" />            <AppenderRef ref="File" />        </root>    </Loggers></configuration>

Note: The XML tag here is similar to the one described in the previous article, which defines the log output source and the definition of the log format.

But that's not enough, Spring boot doesn't know what log4j2.xml is doing and needs to be done by displaying the declaration in the application.properties file.

logging.config= classpath:log4j2.xml

Run Springbootdemoapplication

But if we comment out the application.properties logging.config= classpath:log4j2.xml , run springbootdemoapplication

It can be seen that there is no association, so the Log4j2.xml configuration is not in effect, and the boot log for spring boot is not printed to the console.

Note that there is a "unspoken rule" here. You can do this if you want to be able to read the information about the configuration file in the context of commenting out the application.properties and configuring the file relationship.

Rename the Log4j2.xml to Log4j2-spring.xml so that running springbootdemoapplication is also able to print the log information as configured as normal.

Custom log Configuration

Depending on the log system, you can organize the configuration file name according to the following rules, it can be loaded correctly:

    • Logback:logback-spring.xml, Logback-spring.groovy, Logback.xml, Logback.groovy

    • Log4j:log4j-spring.properties, Log4j-spring.xml, Log4j.properties, Log4j.xml

    • Log4j2:log4j2-spring.xml, Log4j2.xml

    • JDK (Java Util Logging): logging.properties

Ok,spring Boot the introduction of the log configuration is over, there is a message under the question discussed together.

Project Code Address

Github.com/dminerjackie/rome

Please keep this address in mind, and the project code for many articles will be focused on this project later.

Named Rome, from the proverb Rome was wasn't builtin one day. Translated into Chinese is my personal favorite "do not accumulate kuibu to thousands of miles, do not accumulate small flow without to become jianghai."

None of the commits can be one step built in Rome, but it is the reality that is getting closer to the dream!

The catalog of the project uses the "big project" structure introduced in "No big project, but I will build big project", in each module will have readme.md, it mainly records some good reference on the net and problems encountered in the Project module theme, convenient follow-up.

If you feel that reading this article is helpful to you, please click " recommend " button, your " recommendation " will be my biggest writing motivation! If you want to keep an eye on my article, please scan the QR code, follow Jackiezheng's public number, I will push my article to you and share with you the high-quality articles I have read every day.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.