SpringBoot第 3 讲:SpringBoot+Junit+Log4J

一、创建Maven项目

参考:SpringBoot第 1 讲:HelloWorld_秦毅翔的专栏-CSDN博客

 

 二、修改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>

三、application.properties配置文件

#-------------------Log4J-------------------
#包级别日志控制logging.level.com.example.springbootlogbackdemo.config=warn
#ERROR、WARN、INFO、DEBUG
logging.level.root=INFO
#logging.level.org.springframework=DEBUG

logging.level.root定义日志级别,级别由低到高分别是DEGUG-->INFO-->WARN-->ERRORD

四、自定义日志工具类Log.java

package demo.junit.utils;

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);
	}
}

五、单元测试

package demo.junit;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import demo.junit.utils.Log;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestJunit {

	/**
	 * 要先启动boot程序
	 */
	@Test
	public void test() {
		Log.d(getClass(), "日志debug");
		Log.i(getClass(), "日志info");
		Log.w(getClass(), "日志warn");
		Log.e(getClass(), "日志error");
	}
}

六、启动BootApplication

package demo.junit;

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

@SpringBootApplication
public class JunitBootApplication {

	public static void main(String[] args) {
		SpringApplication.run(JunitBootApplication.class, args);
	}
}

七、源代码

https://download.csdn.net/download/qzc70919700/30475133

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值