springboot 日志持久化到磁盘
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.11.2</version>
</dependency>
<!-- 兼容log4j 的输出-->
<dependency>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
<version>1.2.16</version>
</dependency>
logging:
config: classpath:log4j2-sit.xml
log4j2-sit.xml 配置如下
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<properties>
<!-- 文件输出格式 -->
<property name="PATTERN">[%d{yyyy-MM-dd HH:mm:ss:SSS z}] [%t] %5p %l: %m%n</property>
<Property name="FILE_PATH">/service/logs/app/sit/dma/data-delivery</Property>
</properties>
<appenders>
<Console name="CONSOLE" target="system_out">
<PatternLayout pattern="${PATTERN}" />
</Console>
<!-- error -->
<RollingFile name="errorRollingFile" fileName="${FILE_PATH}/eror.log"
filePattern="${FILE_PATH}/$${date:yyyy-MM}/error-%d{MM-dd-yyyy}-%i.log.gz">
<SizeBasedTriggeringPolicy size="500MB"/>
<!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了20 -->
<DefaultRolloverStrategy max="20"/>
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<JsonLayout locationinfo="true" eventEol="true" compact="true">
<KeyValuePair key="@timestamp" value="$${date:yyyy-MM-dd HH:mm:ss:SSS z}"/>
<KeyValuePair key="project_name" value="data-delivery-impl"/>
</JsonLayout>
</RollingFile>
<!-- warn -->
<RollingFile name="warnRollingFile" fileName="${FILE_PATH}/warn.log"
filePattern="${FILE_PATH}/$${date:yyyy-MM}/warn-%d{MM-dd-yyyy}-%i.log.gz">
<SizeBasedTriggeringPolicy size="500MB"/>
<!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了20 -->
<DefaultRolloverStrategy max="20"/>
<Filters>
<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<JsonLayout locationinfo="true" eventEol="true" compact="true">
<KeyValuePair key="@timestamp" value="$${date:yyyy-MM-dd HH:mm:ss:SSS z}"/>
<KeyValuePair key="project_name" value="data-delivery-impl"/>
</JsonLayout>
</RollingFile>
<!-- info -->
<RollingFile name="infoRollingFile" fileName="${FILE_PATH}/info.log"
filePattern="${FILE_PATH}/$${date:yyyy-MM}/info-%d{MM-dd-yyyy}-%i.log.gz">
<SizeBasedTriggeringPolicy size="500MB"/>
<!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了20 -->
<DefaultRolloverStrategy max="20"/>
<Filters>
<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<JsonLayout locationinfo="true" eventEol="true" compact="true">
<KeyValuePair key="@timestamp" value="$${date:yyyy-MM-dd HH:mm:ss:SSS z}"/>
<KeyValuePair key="project_name" value="data-delivery-impl"/>
</JsonLayout>
</RollingFile>
<!-- debug -->
<RollingFile name="debugRollingFile" fileName="${FILE_PATH}/debug.log"
filePattern="${FILE_PATH}/$${date:yyyy-MM}/debug-%d{MM-dd-yyyy}-%i.log.gz">
<SizeBasedTriggeringPolicy size="500MB"/>
<!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了20 -->
<DefaultRolloverStrategy max="20"/>
<Filters>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<JsonLayout locationinfo="true" eventEol="true" compact="true">
<KeyValuePair key="@timestamp" value="$${date:yyyy-MM-dd HH:mm:ss:SSS z}"/>
<KeyValuePair key="project_name" value="data-delivery-impl"/>
</JsonLayout>
</RollingFile>
</appenders>
<loggers>
<logger name="com.baozun.oc" level="info" />
<logger name="org.apache.http" level="info"></logger>
<root level="info">
<appenderref ref="CONSOLE" />
<appenderref ref="errorRollingFile" />
<appenderref ref="warnRollingFile" />
<appenderref ref="infoRollingFile"/>
<appenderref ref="debugRollingFile" />
</root>
</loggers>
</configuration>