在SpringBoot-WebFlux中整合log4j2

  如果说在搭建项目时候第一事是创建项目,那么第二件事就一定是加入日志。日志在项目中有非常重要的作用,小到记录信息,大到排查问题,都要用到。
  SpringBoot整合方式的POM在最后面第四点(最后面!最后面!最后面!),其他步骤一样

1、webflux增加pom依赖

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

	
	<dependency>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-log4j2artifactId>
	dependency>
2、在resources文件夹下创建log4j2.xml文件,内容如下:

<configuration status="OFF">

    <properties>
        
        <property name="LOG_LEVEL">INFOproperty>
        
        <property name="APP_NAME" value="spring-flux-mongo"/>
        
        <property name="LOG_HOME">./logsproperty>
        
        <property name="LOG_MAX_HISTORY" value="10d"/>
        
        <property name="LOG_MAX_FILE_SIZE" value="10 MB"/>
        
        <property name="LOG_TOTAL_NUMBER_DAILY" value="10"/>
        
        <property name="ARCHIVE_FILE_SUFFIX" value="zip"/>
        
        <property name="LOG_FILE_NAME" value="${LOG_HOME}/${APP_NAME}"/>
        <property name="FILE_NAME_PATTERN" value="${LOG_HOME}/${APP_NAME}.%d{yyyy-MM-dd}"/>

        
        
        <property name="ENCODER_PATTERN_CONSOLE">%blue{%d{yyyy-MM-dd HH:mm:ss.SSS}} | %highlight{%-5level}{ERROR=Bright RED, WARN=Bright Yellow, INFO=Bright Green, DEBUG=Bright Cyan, TRACE=Bright White} | %yellow{%t} | %cyan{%l} >>>>> %white{%msg%n}property>
        
        <property name="ENCODER_PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} | %-5level | %t | %c{1.} >>>>> %msg%nproperty>
    properties>

    <Appenders>
        
        <Console name="Console" target="SYSTEM_OUT">
            
            <PatternLayout pattern="${ENCODER_PATTERN_CONSOLE}" />
        Console>
        
        <RollingFile name="RollingFileAll" fileName="${LOG_FILE_NAME}.ALL.log" filePattern="${FILE_NAME_PATTERN}.ALL.%i.log.${ARCHIVE_FILE_SUFFIX}">
            
            <ThresholdFilter level="${LOG_LEVEL}" onMatch="ACCEPT" onMismatch="DENY" />
            
            <PatternLayout pattern="${ENCODER_PATTERN}" />
            <Policies>
                
                <TimeBasedTriggeringPolicy />
                
                <SizeBasedTriggeringPolicy size="${LOG_MAX_FILE_SIZE}" />
            Policies>
            
            <DefaultRolloverStrategy compressionLevel="9" max="${LOG_TOTAL_NUMBER_DAILY}">
                <Delete basePath="${LOG_HOME}" maxDepth="1">
                    <IfFileName glob="${APP_NAME}.*.ALL.*.log.${ARCHIVE_FILE_SUFFIX}" />
                    <IfLastModified age="${LOG_MAX_HISTORY}" />
                Delete>
            DefaultRolloverStrategy>
        RollingFile>

        <RollingFile name="RollingFileDebug"
                     fileName="${LOG_FILE_NAME}.DEBUG.log"
                     filePattern="${FILE_NAME_PATTERN}.DEBUG.%i.log.${ARCHIVE_FILE_SUFFIX}">
            <Filters>
                <ThresholdFilter level="DEBUG" />
                <ThresholdFilter level="INFO" onMatch="DENY"
                                 onMismatch="NEUTRAL" />
            Filters>
            <PatternLayout pattern="${ENCODER_PATTERN}" />
            <Policies>
                
                <TimeBasedTriggeringPolicy />
                
                <SizeBasedTriggeringPolicy size="${LOG_MAX_FILE_SIZE}" />
            Policies>
            
            <DefaultRolloverStrategy compressionLevel="9"
                                     max="${LOG_TOTAL_NUMBER_DAILY}">
                <Delete basePath="${LOG_HOME}" maxDepth="1">
                    <IfFileName glob="${APP_NAME}.*.DEBUG.*.log.${ARCHIVE_FILE_SUFFIX}" />
                    <IfLastModified age="${LOG_MAX_HISTORY}" />
                Delete>
            DefaultRolloverStrategy>
        RollingFile>

        <RollingFile name="RollingFileWarn" fileName="${LOG_FILE_NAME}.WARN.log"
                     filePattern="${FILE_NAME_PATTERN}.WARN.%i.log.${ARCHIVE_FILE_SUFFIX}">
            <Filters>
                <ThresholdFilter level="WARN" />
                <ThresholdFilter level="ERROR" onMatch="DENY"
                                 onMismatch="NEUTRAL" />
            Filters>
            <PatternLayout pattern="${ENCODER_PATTERN}" />
            <Policies>
                
                <TimeBasedTriggeringPolicy />
                
                <SizeBasedTriggeringPolicy size="${LOG_MAX_FILE_SIZE}" />
            Policies>
            
            <DefaultRolloverStrategy compressionLevel="9"
                                     max="${LOG_TOTAL_NUMBER_DAILY}">
                <Delete basePath="${LOG_HOME}" maxDepth="1">
                    <IfFileName glob="${APP_NAME}.*.WARN.*.log.${ARCHIVE_FILE_SUFFIX}" />
                    <IfLastModified age="${LOG_MAX_HISTORY}" />
                Delete>
            DefaultRolloverStrategy>
        RollingFile>

        <RollingFile name="RollingFileError"
                     fileName="${LOG_FILE_NAME}.ERROR.log"
                     filePattern="${FILE_NAME_PATTERN}.ERROR.%i.log.${ARCHIVE_FILE_SUFFIX}">
            <Filters>
                <ThresholdFilter level="ERROR" />
            Filters>
            <PatternLayout pattern="${ENCODER_PATTERN}" />
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="${LOG_MAX_FILE_SIZE}" />
            Policies>
            <DefaultRolloverStrategy compressionLevel="9"   max="${LOG_TOTAL_NUMBER_DAILY}">
                <Delete basePath="${LOG_HOME}" maxDepth="1">
                    <IfFileName glob="${APP_NAME}.*.ERROR.*.log.${ARCHIVE_FILE_SUFFIX}" />
                    <IfLastModified age="${LOG_MAX_HISTORY}" />
                Delete>
            DefaultRolloverStrategy>
        RollingFile>
    Appenders>

    
    <Loggers>
        <root level="${LOG_LEVEL}">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFileAll"/>
            <appender-ref ref="RollingFileDebug"/>
            <appender-ref ref="RollingFileWarn"/>
            <appender-ref ref="RollingFileError"/>
        root>
    Loggers>
configuration>
3、在application.yml中加入
logging:
  config: classpath:log4j2.xml
  level:
    com.example.mongo: debug
# log4j2.xml 这个就是文件名称
#level:
#    com.example.mongo: debug 包名和级别,自行修改

参考文章:SpringBoot系列(12):SpringBoot集成log4j2日志配置

4、SpringBoot的pom
<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>

你可能感兴趣的:(SpringBoot系列,spring,boot,log4j,后端)