SpringBoot+LogBack实现不同业务输出到不同日志文件

        工作中要求根据不同的业务输出到不同的日志文件,为了方便查找,以下是简单的实现流程,方便后期再次使用到。

1.创建个springboot项目

2.在resource下创建文件logback-spring.xml

3.pom文件添加

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

4.添加logback-spring.xml的内容

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!--打印到控制台-->
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d %-5level [%-18.18thread] - [%X{requestId:-SYSTEM}]:+++++++++ %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="datasourceLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--日志保存到此,根据fileNamePattern进行保存,新的日志继续保存到该文件-->
        <file>logs/datasource/datasource.log</file>
        <!--按天轮转-->
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!--一整天归档文件名称,%d{yyyy-MM-dd}代表日期-->
            <fileNamePattern>logs/datasource/datasource.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <!--文件最大保存天数-->
            <maxHistory>7</maxHistory>
            <!--单个日志最大容量,至少10MB才能看出来-->
            <maxFileSize>100MB</maxFileSize>
            <!--所有日志最多占多大容量-->
            <totalSizeCap>2GB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <!--日志的输入格式-->
            <pattern>%d %-5level [%-18.18thread] - [%X{requestId:-SYSTEM}]:====== %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>
    <appender name="initLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/init/init.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>logs/init/init.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <maxHistory>7</maxHistory>
            <maxFileSize>100MB</maxFileSize>
            <totalSizeCap>2GB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>%d %-5level [%-18.18thread] - [%X{requestId:-SYSTEM}]:====== %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>
     <!--    如果appender里没有限定日志如何区分,那么root可以统一设置,如果不配置那么控制台和文件不会输出任何日志,这里root的level不做限制-->
    <root level="INFO">
        <appender-ref ref="stdout"/>
    </root>

    
    <!--数据源日志-->
    <logger name="datasourceLog" level="DEBUG" additivity="false">
        <!--        输出到文件-->
        <appender-ref ref="datasourceLog"/>
        <!--        允许控制台输出-->
        <appender-ref ref="stdout"/>
    </logger>
    <!--初始化日志-->
    <logger name="initLog" level="DEBUG" additivity="false">
        <!--        输出到文件-->
        <appender-ref ref="initLog"/>
        <!--        允许控制台输出-->
        <appender-ref ref="stdout"/>
    </logger>

</configuration>

5.创建接口,LogConfig,定义所有的log对象,getLogger中定义的对应logback-spring.xml中的logger中的name,当用到哪个log对象时,就会根据他的类型选择输出到哪个文件中。

//引入的都是slf4j的
public interface LogConfig {
     Logger datasourceLog =  LoggerFactory.getLogger("datasourceLog");
     Logger initLog =  LoggerFactory.getLogger("initLog");
}

6.模拟使用接口,触发log打印

@RestController
public class TestLogController {

    @GetMapping("/testLog")
    public void  testLog(){
        LogConfig.initLog.debug("init start...");
        LogConfig.initLog.error("find Error");
        LogConfig.initLog.debug("init  end...");

        LogConfig.datasourceLog.debug("datasource start...");
        LogConfig.datasourceLog.error("find Error");
        LogConfig.datasourceLog.debug("datasource end...");
    }
}

7.启动项目,访问localhost:8080/testLog

8.在项目的根目录发现文件夹,夹中有对应的日志文件

 完毕,有问题的私聊或评论。

  • 0
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值