spring boot与log4j集成

1、生成log4j.xml

<Configuration status="INFO" monitorInterval="30">
    <Properties>
        <!--  输出路径  -->
        <Property name="logpath">/Log4j/logs/log/dev</Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                    pattern="[%d][%-5p][%t] %m (%F:%L)%n"/>
        </Console>
        <RollingFile name="debug" fileName="${logpath}/debug/erp_debug.log"
                     filePattern="${logpath}/debug/erp_debug_%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="24" modulate="true"/>
                <SizeBasedTriggeringPolicy size="50 MB"/>\
            </Policies>
            <DefaultRolloverStrategy max="30">
                <Delete basePath="${logpath}/debug" maxDepth="1">
                    <IfFileName glob="erp_debug_*.log"/>
                    <IfLastModified age="15d"/>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>
        <RollingFile name="info" fileName="${logpath}/info/erp_info.log"
                     filePattern="${logpath}/info/erp_info_%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="24" modulate="true"/>
                <SizeBasedTriggeringPolicy size="50 MB"/>\
            </Policies>
            <DefaultRolloverStrategy max="30">
                <Delete basePath="${logpath}/info" maxDepth="1">
                    <IfFileName glob="erp_info_*.log"/>
                    <IfLastModified age="15d"/>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>
        <RollingFile name="warn" fileName="${logpath}/warn/erp_warn.log"
                     filePattern="${logpath}/warn/erp_warn_%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="24" modulate="true"/>
                <SizeBasedTriggeringPolicy size="50 MB"/>\
            </Policies>
            <DefaultRolloverStrategy max="30">
                <Delete basePath="${logpath}/warn" maxDepth="1">
                    <IfFileName glob="erp_warn_*.log"/>
                    <IfLastModified age="15d"/>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>
        <RollingFile name="error" fileName="${logpath}/error/erp_error.log"
                     filePattern="${logpath}/error/erp_error_%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="24" modulate="true"/>
                  <!--   每个文件最大50M -->
                <SizeBasedTriggeringPolicy size="50 MB"/>\
            </Policies>
            <DefaultRolloverStrategy max="30">
                <Delete basePath="${logpath}/error" maxDepth="1">
                    <IfFileName glob="erp_error_*.log"/>
                     <!-- 设置最大保存时间为15天-->
                    <IfLastModified age="15d"/>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>

    </Appenders>
   <!--切换输出级别-->
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="debug"/>
            <AppenderRef ref="info"/>
            <AppenderRef ref="warn"/>
            <AppenderRef ref="error"/>
        </Root>
    </Loggers>
</Configuration>

2、加入application配置

#########log4j(这里可以不做指定  除非自己重新命名)##########
logging.config=classpath:log4j.xml
logging.level.org.springframework=INFO

3、相关测试代码:

@Api(tags = "Swagger测试")
@Controller
@Slf4j
public class Mycontroller {

    @ApiOperation(value = "查询学生信息接口", notes = "查询学生信息")
    @GetMapping("/select")
    @ResponseBody
    public String selectStudentMsg() {
        System.out.println("查询了学生信息");
        log.trace("trace查询了学生信息");
        log.info("info查询了学生信息");
        log.warn("warn查询了学生信息");
        log.error("erro查询了学生信息");
        log.debug("debug查询了学生信息");
        return "查询学生信息成功";
    }

4、问题相关:

  • 日志不能正常显示:在maven中加入对应依赖(spring boot默认支持logback的日志打印形式)
<!--移除框架中的logback依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--log4j2-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
  • log4j和log4j2的区别:他们在配置文件中的写法不同。log4j2是log4j的升级版本,从spring boot1.4版本开始需要使用log4j2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值