Spring Boot 2.x 实践记:log4j2.xml

Posted mickjoust

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot 2.x 实践记:log4j2.xml相关的知识,希望对你有一定的参考价值。

目录

  • Maven
  • 添加 log4j2.xml
  • 实战
  • 小结

TL;DR

我们都知道,Spring Boot 的默认日志采用的是Logback。

今天,我们通过配置log4j2.xm来使用Log4j2日志记录。

Maven

首先,排除Spring Boot 默认的日志记录依赖logback,然后添加log4j2依赖。

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions> <!-- 排除默认的logback-->
            <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>
        <version>$spring.boot.version</version>
    </dependency>
</dependencies>

添加 log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
	<Properties>
		<Property name="logDir">/web/app/demo</Property>
		<Property name="logFile">demo</Property>
	</Properties>

	<Appenders>
		<Console name="STDOUT" target="SYSTEM_OUT">
			<PatternLayout pattern="[%5p][%dyyyy-MM-dd HH:mm:ss,SSS] (%F:%L) %m%n" />
		</Console>

		<RollingFile name="RollingFile" fileName="$logDir/$logFile.log"
			filePattern="$logDir/$$date:yyyy-MM/$logFile.%dyyyy-MM-dd-%i.log.gz">

			<PatternLayout pattern="[%5p][%dyyyy-MM-dd HH:mm:ss,SSS] (%F:%L) %m%n" />

			<Policies>
				<TimeBasedTriggeringPolicy />
				<SizeBasedTriggeringPolicy size="500 MB" />
			</Policies>

			<DefaultRolloverStrategy max="100">
				<Delete basePath="$logDir" maxDepth="2">
					<IfFileName glob="*.log.gz">
						<IfLastModified age="7d">
							<IfAny>
								<!--keep <= 5GB,可自行修改-->
								<IfAccumulatedFileSize exceeds="5 GB" />
								<!--keep file count <=100 -->
								<IfAccumulatedFileCount exceeds="100" />
							</IfAny>
						</IfLastModified>
					</IfFileName>
				</Delete>
			</DefaultRolloverStrategy>
		</RollingFile>
	</Appenders>

	<Loggers>
		<logger name="org.springframework" level="WARN" />
		<logger name="org.mongodb" level="WARN" />
		<logger name="org.apache" level="WARN" />
		<logger name="org.opensymphony" level="WARN" />
		<logger name="org.mybatis" level="WARN" />
		<logger name="com.alibaba.druid" level="WARN" />
		<logger name="org.hibernate" level="WARN" />
		<logger name="httpclient.wire.content" level="WARN" />
		<logger name="httpclient.wire.header" level="WARN" />
		<logger name="org.apache.commons.httpclient" level="WARN" />
		<logger name="net.sf.ehcache" level="WARN" />
		<logger name="org.elasticsearch" level="WARN" />
		<logger name="io.netty" level="WARN" />


		<Root level="DEBUG">
			<AppenderRef ref="STDOUT" /> <!-- 如果用于生产环境,建议删除此项-->
			<AppenderRef ref="RollingFile" />
		</Root>
	</Loggers>
</configuration>

实战

  • github代码地址:
package com.mickjoust.demo.springboot2_in_action.log4j2;

import lombok.extern.log4j.Log4j2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author mickjoust
 **/
@Log4j2
@SpringBootApplication
public class AppStartLog4j2 
    public static void main(String[] args) 
        SpringApplication.run(AppStartLog4j2.class,args);

        log.info("=== info level");
        if (log.isDebugEnabled()) 
            log.debug("=== debug level");
        
        log.error("=== error level",new Throwable("test"));
        log.warn("=== warn level");

    

输出结果:

小结

到此,我们通过简单的方法成功配置log4j2的日志记录。

欢迎留言。

以上是关于Spring Boot 2.x 实践记:log4j2.xml的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 2.x 实践记:log4j2.xml

(汇总)Spring Boot 实践折腾记 & Spring Boot 2.x 实践记

Spring Boot 2.x 实践记:Gson

Spring Boot 2.x 实践记:Mail

Spring Boot 2.x 实践记:Mail

Spring Boot 2.x 实践记:Mail