温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot中如何整合log4g2

发布时间:2021-11-02 17:51:05 来源:亿速云 阅读:188 作者:小新 栏目:开发技术

这篇文章主要介绍了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中添加配置

springboot中如何整合log4g2

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>

感谢你能够认真阅读完这篇文章,希望小编分享的“springboot中如何整合log4g2”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI