主页:​写程序的小王叔叔的博客​​欢迎来访

支持:点赞SpringBoot与Log4j2用日志管理方式对话_日志管理SpringBoot与Log4j2用日志管理方式对话_Log4j2_02​收藏SpringBoot与Log4j2用日志管理方式对话_日志管理_03关注SpringBoot与Log4j2用日志管理方式对话_SpringBoot_04

一、效果

SpringBoot与Log4j2用日志管理方式对话_日志管理_05SpringBoot与Log4j2用日志管理方式对话_日志管理_06

二、对话方式

文件结构:

SpringBoot与Log4j2用日志管理方式对话_spring_07SpringBoot与Log4j2用日志管理方式对话_spring_08

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.0modelVersion>

<groupId>com.vcoo.supermarketgroupId>
<artifactId>********artifactId>
<version>V0.1version>

<packaging>warpackaging>

<name>*******name>
<description>*********description>

<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.2.RELEASEversion>
<relativePath/>
parent>

<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>

<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-loggingartifactId>
exclusion>
exclusions>
dependency>

<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
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>
dependency>


dependencies>

<build>
<plugins>
<plugin>

plugin>
plugins>
build>

project>
xml version="1.0" encoding="UTF-8"?>



<configuration status="WARN" monitorInterval="1800">
<Properties>


<property name="basePath">log日志文件的名称/建议用自己项目名字+别名property>



<property name="console_log_pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight{%-5level %l} - %m%nproperty>


<property name="log_pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %C.%M -%m%nproperty>

<property name="every_file_size">20MBproperty>

<property name="output_log_level">DEBUGproperty>




<property name="rolling_fileName">Log/******-all.log

Log/****/time-based-logs/%d{yyyy-MM-dd}-all.log.zip
property>

<property name="rolling_max">50property>




<property name="info_fileName">Log/****/info.logproperty>

<property name="info_filePattern">Log/****/time-based-logs/%d{yyyy-MM-dd}-info.log.zip
property>

<property name="info_max">10property>




<property name="warn_fileName">Log/****/warn.logproperty>

<property name="warn_filePattern">Log/****/time-based-logs/%d{yyyy-MM-dd}-warn.log.zipproperty>

<property name="warn_max">10property>


<property name="error_fileName">Log/*****/error.logproperty>

<property name="error_filePattern">Log/*****/time-based-logs/%d{yyyy-MM-dd}-error.log.zipproperty>

<property name="error_max">10property>


<property name="console_print_level">DEBUGproperty>

Properties>


<appenders>

<Console name="Console" target="SYSTEM_OUT" follow="true">

<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>

<PatternLayout pattern="${console_log_pattern}"/>
Console>


<RollingFile name="RollingFile" fileName="${rolling_fileName}"
filePattern="${rolling_filePattern}">
<PatternLayout pattern="${log_pattern}"/>
<Policies>

<OnStartupTriggeringPolicy />

<SizeBasedTriggeringPolicy size="20 MB" />

<TimeBasedTriggeringPolicy interval="1" modulate="true" />

<DefaultRolloverStrategy max="${info_max}">
<Delete basePath="Log/*******/time-based-logs/" maxDepth="2">
<IfFileName glob="*.log.zip" />




<IfLastModified age="720H" />
Delete>
DefaultRolloverStrategy>
Policies>

RollingFile>


<RollingFile name="InfoFile" fileName="${info_fileName}"
filePattern="${info_filePattern}">
<PatternLayout pattern="${log_pattern}"/>


<Filters>
<ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
Filters>
<Policies>

<OnStartupTriggeringPolicy />

<SizeBasedTriggeringPolicy size="${every_file_size}" />
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
<DefaultRolloverStrategy max="${info_max}"/>
Policies>
RollingFile>


<RollingFile name="WarnFile" fileName="${warn_fileName}"
filePattern="${warn_filePattern}">
<PatternLayout pattern="${log_pattern}"/>

<Filters>
<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
Filters>
<Policies>

<OnStartupTriggeringPolicy />

<SizeBasedTriggeringPolicy size="${every_file_size}" />
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
<DefaultRolloverStrategy max="${info_max}"/>
Policies>
RollingFile>


<RollingFile name="ErrorFile" fileName="${error_fileName}"
filePattern="${error_filePattern}">
<PatternLayout pattern="${log_pattern}"/>

<Filters>
<ThresholdFilter level="FATAL" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
Filters>
<Policies>

<OnStartupTriggeringPolicy />

<SizeBasedTriggeringPolicy size="${every_file_size}" />
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
<DefaultRolloverStrategy max="${info_max}"/>
Policies>
RollingFile>

appenders>


<loggers>


<Logger name="com.*.**" level="warn" />
<Logger name="com.*.**" level="error" />
<logger name="org.springframework.boot.actuate.autoconfigure.CrshAutoConfiguration" level="warn"/>



<logger name="java.sql" level="debug" additivity="false">
<appender-ref ref="Console"/>
logger>



<root level="${output_log_level}">
<appender-ref ref="RollingFile"/>
<appender-ref ref="Console"/>
<appender-ref ref="InfoFile"/>
<appender-ref ref="WarnFile"/>
<appender-ref ref="ErrorFile"/>
root>
loggers>
configuration>
# 日志文件名称 
#logging.config=classpath:logback-boot.xml
logging.config=classpath:log4j2-springboot.xml
##打印参数
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=trace
logging.level.root=ALL

使用方法基本log4j一样。

转载声明:本文为博主原创文章,未经博主允许不得转载

⚠️注意 ~

💯本期内容就结束了,如果内容有误,麻烦大家评论区指出!

如有疑问❓可以在评论区💬或私信💬,尽我最大能力🏃‍♀️帮大家解决👨‍🏫!

如果我的文章有帮助到您,欢迎点赞+关注✔️鼓励博主🏃,您的鼓励是我分享的动力🏃🏃🏃~