@Slf4j + lombok 记录日志

使用 slf4j + lombok 记录log

1.pom.xml中添加相关依赖

<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-api</artifactId>
   <version>1.7.25</version>
</dependency>

<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <version>1.16.18</version>
</dependency>

2.编写相关代码

<1>日志类:
package com.example.demo.logs;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class Bill2log {
    public static void log () {
        log.info("###### bill 2 log");
    }
}

<2>配置:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!--定义日志文件的存储地址-->
    <property name="LOG_HOME" value="./logs"/>

    <!-- 全日志文件 -->
    <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名-->
            <fileNamePattern>${LOG_HOME}/fie/file.%d{yyyy-MM-dd}.log.zip</fileNamePattern>
            <maxHistory>60</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- 全日志文件-error级别 -->
    <appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名-->
            <fileNamePattern>${LOG_HOME}/file/file_error.%d{yyyy-MM-dd}.log.zip</fileNamePattern>
            <maxHistory>60</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
    </appender>

    <!-- bill1日志文件 -->
    <appender name="bill1" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名-->
            <fileNamePattern>${LOG_HOME}/bill1/bill1.%d{yyyy-MM-dd}.log.zip</fileNamePattern>
            <maxHistory>60</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- bill2日志文件 -->
    <appender name="bill2" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名-->
            <fileNamePattern>${LOG_HOME}/bill2/bill2.%d{yyyy-MM-dd}.log.zip</fileNamePattern>
            <maxHistory>60</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- bill3日志文件 -->
    <appender name="bill3" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名-->
            <fileNamePattern>${LOG_HOME}/bill3/bill3.%d{yyyy-MM-dd}.log.zip</fileNamePattern>
            <maxHistory>60</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>


    <logger name="com.example.demo.logs.Bill1log" level="INFO" additivity="false">
        <appender-ref ref="bill1"/>
    </logger>
    <logger name="com.example.demo.logs.Bill2log" level="INFO" additivity="false">
        <appender-ref ref="bill2"/>
    </logger>
    <logger name="com.example.demo.logs.Bill3log" level="INFO" additivity="false">
        <appender-ref ref="bill3"/>
    </logger>

    <root level="INFO">
        <appender-ref ref="file"/>
        <appender-ref ref="file_error"/>
    </root>

</configuration>

<3>启动类:

<A>:
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class DemoApplication {

   public static void main(String[] args) {
      SpringApplication application = new SpringApplication(DemoApplication.class);
      ConfigurableApplicationContext context = application.run(args);
      ApplicationStarter applicationStarter = context.getBean(ApplicationStarter.class);
      applicationStarter.startLog();
   }
}

<B>:

package com.example.demo;

import com.example.demo.logs.Bill1log;
import com.example.demo.logs.Bill2log;
import com.example.demo.logs.Bill3log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 应用启动后执行
 *
 */

@Slf4j
@Component
public class ApplicationStarter {
    @Autowired
    private Bill1log bill1log;
    @Autowired
    private Bill2log bill2log;
    @Autowired
    private Bill3log bill3log;

    // startLog
    public void startLog() {
        bill1log.log();
        bill2log.log();
        bill3log.log();

        log.info("all log info ...");
        log.error("all log error ======");
    }
}

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值