亲宝软件园·资讯

展开

springboot整合log4g2 聊聊springboot中整合log4g2的问题

木木-布丁 人气:0
想了解聊聊springboot中整合log4g2的问题的相关内容吗,木木-布丁在本文为您仔细讲解springboot整合log4g2的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:springboot整合log4g2,springboot,log4g2,下面大家一起来学习吧。

1.导入jar

springboot默认是用logback的日志框架的,所以需要排除logback,不然会出现jar依赖冲突的报错。

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions><!-- 去掉springboot默认配置 -->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency> <!-- 引入log4j2依赖 -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

2.配置文件

1.如果自定义了文件名,需要在application.yml中配置

2.默认名log4j2-spring.xml,就省下了在application.yml中配置
在applicaiton.yaml中添加配置

在这里插入图片描述

logging:
  #日志文件
  config: classpath:log4g2.xml
  level:
    com.alibaba.nacos.client.config.impl: WARN
    cn.jay.repository: trace
  file:
    #${file.name} 后期可以改成${spring.application.name}
    path: /log/${file.name}
file:
  name: dome

在config中配置log4g2.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- status="OFF",可以去掉,它的含义为是否记录log4j2本身的event信息,默认是OFF -->
<configuration status="off" monitorInterval="60">
    <!--	<properties>-->
    <!--		<property name="projectName">-->
    <!--			riiot-->
    <!--		</property>-->
    <!--	</properties>-->


    <appenders>
        <!-- 开发环境用 -->
        <Console name="debug_console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n"/>
            <Filters>
                <ThresholdFilter level="DEBUG"/>
                <ThresholdFilter level="INFO" onMatch="DENY" onMismatch="NEUTRAL"/>
            </Filters>
        </Console>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n"/>
            <Filters>
                <ThresholdFilter level="info"/>
            </Filters>
        </Console>

        <!-- 输出日志到文件 每天一个文件(ERROR-FATAL级别) -->
        <RollingRandomAccessFile name="AppErrorDailyRollingFile" fileName="${sys:LOG_PATH}/AppError.log" append="true" bufferedIO="false" bufferSize="256"
                                 filePattern="${sys:LOG_PATH}/AppError.log.%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" />
            <Filters>
                <ThresholdFilter level="ERROR"/>
            </Filters>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
            </Policies>
        </RollingRandomAccessFile>

        <!-- 输出日志到文件 每天一个文件(WARN级别) -->
        <RollingRandomAccessFile name="AppWarnDailyRollingFile" fileName="${sys:LOG_PATH}/AppWarn.log" append="true" bufferedIO="true" bufferSize="10240" immediateFlush="false"
                                 filePattern="${sys:LOG_PATH}/AppWarn.log.%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" />
            <Filters>
                <ThresholdFilter level="WARN"/>
                <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
            </Filters>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
            </Policies>
        </RollingRandomAccessFile>

        <!-- 输出日志到文件 每天一个文件(INFO级别) -->
        <RollingRandomAccessFile name="AppInfoDailyRollingFile" fileName="${sys:LOG_PATH}/AppAccess.log" append="true" bufferedIO="true" bufferSize="409600" immediateFlush="false"
                                 filePattern="${sys:LOG_PATH}/AppAccess.log.%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" />
            <Filters>
                <ThresholdFilter level="INFO"/>
                <ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL"/>
            </Filters>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
            </Policies>
        </RollingRandomAccessFile>

        <!-- 输出日志到文件 每天一个文件(INFO级别) -->
        <RollingRandomAccessFile name="DBAccessDailyRollingFile" fileName="${sys:LOG_PATH}/DBAccess.log" append="true" bufferedIO="true" bufferSize="409600" immediateFlush="false"
                                 filePattern="${sys:LOG_PATH}/DBAccess.log.%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" />
            <Filters>
                <ThresholdFilter level="INFO"/>
                <ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL"/>
            </Filters>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
            </Policies>
        </RollingRandomAccessFile>

        <!-- 输出日志到文件 每天一个文件(ERROR级别) -->
        <RollingRandomAccessFile name="DBErrorDailyRollingFile" fileName="${sys:LOG_PATH}/DBError.log" append="true" bufferedIO="false" bufferSize="256"
                                 filePattern="${sys:LOG_PATH}/DBError.log.%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" />
            <Filters>
                <ThresholdFilter level="WARN"/>
            </Filters>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
            </Policies>
        </RollingRandomAccessFile>

        <!-- 输出日志到文件 每天一个文件(ERROR级别) -->
        <RollingRandomAccessFile name="SysErrorDailyRollingFile" fileName="${sys:LOG_PATH}/SysError.log" append="true" bufferedIO="false"
                                 filePattern="${sys:LOG_PATH}/SysError.log.%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" />
            <Filters>
                <ThresholdFilter level="WARN"/>
            </Filters>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
            </Policies>
        </RollingRandomAccessFile>

        <!-- 启动日志 -->
        <RollingRandomAccessFile name="BootLog" fileName="${sys:LOG_PATH}/Boot.log" append="false" bufferedIO="false"
                                 filePattern="${sys:LOG_PATH}/Boot.log.%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" />
            <Filters>
                <ThresholdFilter level="INFO"/>
            </Filters>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
            </Policies>
        </RollingRandomAccessFile>

     <!--   <Kafka name="SyncKafka" topic="logs" syncSend="false">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [riiot] [${jar.name}] [%X{ip}] [dev] [%t] {%c}-%m"/>
            <Property name="bootstrap.servers">192.168.1.49:9092</Property>
            <Property name="acks">0</Property>
            <Property name="compression.type">gzip</Property>
            <Property name="max.block.ms">10000</Property>
            <Filters>
                <ThresholdFilter level="INFO"/>
            </Filters>
        </Kafka>
        &lt;!&ndash; 异步发送kafka &ndash;&gt;
        <Async name="Kafka" bufferSize="512" blocking="false">
            <AppenderRef ref="SyncKafka"/>
        </Async>

        <Kafka name="KafkaErrLog" topic="errLogTopic" syncSend="false">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [riiot] [${jar.name}] [%X{ip}] [dev] [%t] {%c}-%m"/>
            <Property name="bootstrap.servers">192.168.1.49:9092</Property>
            <Property name="acks">0</Property>
            <Property name="compression.type">gzip</Property>
            <Filters>
                <ThresholdFilter level="ERROR"/>
            </Filters>
        </Kafka>
        &lt;!&ndash; 异步发送kafka &ndash;&gt;
        <Async name="AsyncKafkaErrLog" bufferSize="512" blocking="false">
            <AppenderRef ref="KafkaErrLog"/>
        </Async>
-->
    </appenders>

    <loggers>
        <!-- 只把包名是com.cmsr的日志输出到文件 -->
        <logger name="com.cmsr" additivity="false" level="debug">
            <appender-ref ref="AppErrorDailyRollingFile" />
            <appender-ref ref="AppWarnDailyRollingFile" />
            <appender-ref ref="AppInfoDailyRollingFile" />
            <appender-ref ref="console" />

            <appender-ref ref="debug_console" />
        </logger>

        <!-- 只把包名是com.cmsr.sicp.common.mybatis(DB正常执行,异常分别写到不同的log文件)的日志输出到文件 -->
        <logger name="com.cmsr.sicp.common.mybatis" additivity="false" level="debug">
            <appender-ref ref="DBAccessDailyRollingFile" />
            <appender-ref ref="DBErrorDailyRollingFile" />
            <appender-ref ref="console" />

        </logger>

        <!-- 过滤springframework输出,提高启动速度 (生产环境中需要整体删除)-->
        <logger name="org.springframework" additivity="false" level="warn">
            <appender-ref ref="console" />
        </logger>

        <!-- 启动日志单独输出 -->
        <logger name="com.cmsr.Launcher" additivity="false" level="info">
            <appender-ref ref="BootLog" />
            <appender-ref ref="console" />
        </logger>

        <!-- 定义全局。其他包的日志只输出到控制台,不输出到日志文件 -->
        <root level="error">
            <appender-ref ref="SysErrorDailyRollingFile" />
            <appender-ref ref="console" />

        </root>
    </loggers>
</configuration>

加载全部内容

相关教程
猜你喜欢
用户评论