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.springframeworkgroupId>
        <artifactId>spring-contextartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
        <exclusions> 
            <exclusion>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-loggingartifactId>
            exclusion>
        exclusions>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-log4j2artifactId>
        <version>${spring.boot.version}version>
    dependency>
dependencies>

添加 log4j2.xml


<configuration status="WARN">
	<Properties>
		<Property name="logDir">/web/app/demoProperty>
		<Property name="logFile">demoProperty>
	Properties>

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

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

			<PatternLayout pattern="[%5p][%d{yyyy-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>
								
								<IfAccumulatedFileSize exceeds="5 GB" />
								
								<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的日志记录。

欢迎留言。

你可能感兴趣的:(Srpng,Boot,2)