Log4j->SLF4j->Logback是同一个人开发的
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class LoggerTest {
// private final Logger log= LoggerFactory.getLogger(LoggerTest.class);
@Test
public void test1(){
log.debug("debug test...");
log.info("info test...");
log.error("error test...");
String name="kelly";
String password="123456";
log.error("error test...name:{},password:{}",name,password); //占位符使用
}
}
lombok的使用
IntelliJ IDEA lombok插件的安装和使用:
https://jingyan.baidu.com/article/0a52e3f4e53ca1bf63ed725c.html
用注解@Slf4j代替private final Logger log= LoggerFactory.getLogger(LoggerTest.class);
使用时直接用log去记录日志
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
Logback的配置
1.application.yml配置
logging:
file: D:/logs/test/log.txt
2.logback-spring.xml配置(建议用这种)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
%d - %msg%n
</pattern>
</layout>
</appender>
<appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level><!--该过滤器过滤掉ERROR-->
<onMatch>DENY</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
<encoder>
<pattern>
%msg%n
</pattern>
</encoder>
<!--滚动策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--路径-->
<fileNamePattern>d:/logs/test/info.%d.log</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<encoder>
<pattern>
%msg%n
</pattern>
</encoder>
<!--滚动策略-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--路径-->
<fileNamePattern>d:/logs/test/error.%d.log</fileNamePattern>
</rollingPolicy>
</appender>
<root level="debug">
<appender-ref ref="consoleLog" />
<appender-ref ref="fileInfoLog" />
<appender-ref ref="fileErrorLog" />
</root>
</configuration>