1.修改pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- SpringBoot支持01、parent:Begin -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<!-- SpringBoot支持01、parent:End -->
<groupId>org.personal.qin.demos</groupId>
<artifactId>junit_log4j_demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- java版本 -->
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- SpringBoot:Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot:End -->
<!-- SpringTest:Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- SpringTest:End -->
<!-- junit:Begin -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- junit:End -->
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- 资源文件拷贝插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- SpringBoot支持03、添加SpringBoot的插件支持:Begin -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- SpringBoot支持03、添加SpringBoot的插件支持:End -->
</plugins>
</build>
</project>
2.创建Log工具类
util包下创建Log工具类
package Log4j.util;
import org.slf4j.LoggerFactory;
public class Log {
/**
* debug的日志
*
* @param clazz
* @param msg
*/
public static void d(Class<?> tag, String msg) {
LoggerFactory.getLogger(tag).debug(msg);
}
/**
* info的日志
*
* @param tag
* @param msg
*/
public static void i(Class<?> tag, String msg) {
LoggerFactory.getLogger(tag).info(msg);
}
/**
* warn的日志
*
* @param tag
* @param msg
*/
public static void w(Class<?> tag, String msg) {
LoggerFactory.getLogger(tag).warn(msg);
}
/**
* error的日志
*
* @param tag
* @param msg
*/
public static void e(Class<?> tag, String msg) {
LoggerFactory.getLogger(tag).error(msg);
}
}
3.创建测试类
测试类创建在src/test/java下,注意包要和主程序包下
添加两行注解,即可让程序知道这个是测试类
@RunWith(SpringRunner.class)
@SpringBootTest
ctrl+shift+o导包
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
然后正常使用JAVA的测试
package Log4j.test;
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
public class TestLog {
@Test
public void test() {
System.out.println("测试成功");
}
}
4.创建application.properties文件
对Log的访问级别进行设置
logging.level.root定义日志级别,级别由低到高分别是DEGUG–>INFO–>WARN–>ERRORD
#-------------------Log4J-------------------
#\u5305\u7EA7\u522B\u65E5\u5FD7\u63A7\u5236logging.level.com.example.springbootlogbackdemo.config=warn
#ERROR\u3001WARN\u3001INFO\u3001DEBUG
logging.level.root=INFO
#logging.level.org.springframework=DEBUG
5.启动SpringBoot
回到测试类,正常测试即可,可以右键,RunAs JunitTest
package Log4j.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import Log4j.util.Log;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestLog {
@Test
public void test() {
System.out.println("测试成功");
Log.d(getClass(), "这是一个debug级别的输出语句");
Log.i(getClass(), "这是一个Log日志的info级别输出语句");
Log.w(TestLog.class, "这是warn级别的输出语句");
Log.e(getClass(), "这是一个error级别的输出语句");
}
}
可以看到debug级别没有输出,因为我们设置的application.properties配置文件中设置的级别为最高info级别