Examples of custom log4j2 log files in spring boot and log4j2

Source: Internet
Author: User

Examples of custom log4j2 log files in spring boot and log4j2

Background: BecauseLog4j2 is required for versions starting with spring boot 1.4.The supported formats are json and xml. This practice mainly uses the xml format to define the log description.

Follow these steps to introduce log4j2 to spring boot 1.5.8.RELEASE:

1. First remove spring-boot-starter-web and spring-boot-starter from the package, and then introduce the spring-boot-starter-log4j2 package.

<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> <!--https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-log4j2--><dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-log4j2</artifactId>  <version>1.5.8.RELEASE</version></dependency>

2. Set the file resource location in pom to modify the variables in the file through properties in pom.

<finalName>${project.name}</finalName><sourceDirectory>src/main/java</sourceDirectory><testSourceDirectory>src/test/java</testSourceDirectory><resources>  <resource>   <directory>src/main/resources</directory>   <filtering>true</filtering>   <includes>     <include>**/*</include>   </includes>  </resource></resources>

Set the variable replacement attribute

<properties>  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  <java.version>1.8</java.version>  <log4j2.level>debug</log4j2.level>  <log4j2.root.path>/opt/appstack/apache-tomcat/logs/${project.name}</log4j2.root.path>  <log4j2.error.path>/opt/appstack/apache-tomcat/logs/${project.name}-error</log4j2.error.path>  <log4j2.package.path>/opt/appstack/apache-tomcat/logs/${project.name}-kk</log4j2.package.path></properties>

3. Because spring boot protects the configuration file yml or properties, you must add a plug-in to the pom variable to replace the variable in the configuration file.

<PluginManagement> <plugins> <plugin> <groupId> org. springframework. boot </groupId> <artifactId> spring-boot-maven-plugin </artifactId> </plugin> <! -- In order to protect application. yml and application. properties, spring-boot modified the default placeholder $ {...} to @... @ --> <! -- To replace the yml and properties files of spring boot with maven variables, use the $ {} placeholder --> <plugin> <artifactId> maven-resources-plugin </artifactId> <configuration> <encoding> UTF-8 </encoding> <usedefadelimiters> true </usedefadeldelimiters> </configuration> </plugin> </plugins> </pluginManagement>

4. The yml configuration of spring boot is used in this example. Configure the log4j2 configuration file in application. yml.

Logging:
Config: classpath: log4j2. xml

If you want to configure different log4j2 configuration files for different environments, you can set the config value in the corresponding application. yml.

5. Configure the log4j2. xml configuration file

<? Xml version = "1.0" encoding = "UTF-8"?> <Configuration status = "WARN"> <! -- Global parameter --> <Properties> <Property name = "pattern"> % d {yyyy-MM-dd HH: mm: ss, SSS} % 5 p % c {1 }: % L-% m % n </Property> </Properties> <Loggers> <Root level = "DEBUG"> <AppenderRef ref = "console"> </AppenderRef> <AppenderRef ref = "rolling_file"> </AppenderRef> </Root> <! -- Only com. kk. springboot. the demo is written to the file --> <Logger name = "com. kk. springboot. demo "level =" ${log4j2. level} "> <AppenderRef ref =" file "> </AppenderRef> </Logger> </Loggers> <Appenders> <Console name =" console "target =" SYSTEM_OUT "follow = "true"> <! -- The console only outputs level and above --> <ThresholdFilter level = "INFO" onMatch = "ACCEPT" onMismatch = "DENY"/> <PatternLayout> <Pattern >$ {pattern} </Pattern> </PatternLayout> </Console> <! -- Appender of the same source can define multiple rollingfiles and store logs by day --> <RollingFile name = "rolling_file" fileName = "$ {log4j2. root. path }. log "filePattern =" ${log4j2. root. path }_% d {yyyy-MM-dd }. log "> <ThresholdFilter level =" INFO "onMatch =" ACCEPT "onMismatch =" DENY "/> <PatternLayout> <Pattern >$ {pattern} </Pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval = "1"/> <! -- <SizeBasedTriggeringPolicy size = "1 KB"/> --> </Policies> </RollingFile> <File name = "file" fileName = "$ {log4j2. package. path }. log "> <! -- <! -The console only outputs information at the level and above (onMatch), while others directly reject (onMismatch) -> --> <ThresholdFilter level = "DEBUG" onMatch = "ACCEPT" onMismatch = "DENY"/> <PatternLayout> <Pattern >$ {pattern} </Pattern> </PatternLayout> </File> </Appenders> </configuration>

6. Use log4j2 logs in Java code.

private static Logger log = LoggerFactory.getLogger(UserController.class);log.info("enter in entityParam");......

7. By changing the system time, test log storage configuration by day.

The examples of the above spring boot custom log4j2 log file are all the content that I have shared with you. I hope you can give me a reference and support me a lot.

Related Article

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.