使用 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 ======"); } }