springboot 使用 log4j

1.在pom.xmlz中移除springboot中对logback依赖

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions> <!--移除框架中的logback依赖-->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

2.引入log4j

<!--log4j2-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

3.在application.properties文件中配置日志的配置文件,此处配置的log4j2.xml为log4j的配置想xml文件,该文件与application.properties在同一路径下

#log4j2输出日志
logging.config=classpath:log4j2.xml

4.log4j2.xml中的配置

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appenders>
        <!-- 控制台输出 -->
        <console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class %L %M - %msg%n"/>
        </console>

        <!-- fileName:输出路径  filePattern:命名规则 -->
        <RollingFile name="AllFile" fileName="logs/All.log"
                     filePattern="logs/$${date:yyyy-MM-dd}/allOut-%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="all" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <!-- 输出格式 -->
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%n" charset="UTF-8"/>
            <Policies>
                <!--  每天生成一个日志文件  -->
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
                <!-- SizeBasedTriggeringPolicy单个文件的大小限制 -->
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
            <!-- DefaultRolloverStrategy同一个文件下的最大文件数 -->
            <DefaultRolloverStrategy max="50"/>
        </RollingFile>


        <RollingFile name="ErrorFile" fileName="logs/Error.log"
                     filePattern="logs/$${date:yyyy-MM-dd}/err-%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <!-- 输出格式 -->
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n" charset="UTF-8"/>
            <Policies>
                <!--  每天生成一个日志文件  -->
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
                <!-- SizeBasedTriggeringPolicy单个文件的大小限制 -->
                <SizeBasedTriggeringPolicy size="100MB"/>
            </Policies>
            <!-- DefaultRolloverStrategy同一个文件下的最大文件数 -->
            <DefaultRolloverStrategy max="50"/>
        </RollingFile>

    </appenders>

    <loggers>
        <!--过滤掉spring无用的debug信息-->
        <logger name="org.springframework" level="error"></logger>
        <logger name="com.zhangxiaosan.top" level="debug"></logger>

        <root level="info">
            <appender-ref ref="Console"/>
            <appender-ref ref="AllFile"/>
            <appender-ref ref="ErrorFile"/>
        </root>
    </loggers>

</configuration>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小张帅三代

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值