springboot第二章----------打造企業級微信點餐系統(2)----------日誌的搭建

一、在pom.xml中添加依賴

<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	
	<dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
    </dependency>
	
	<!-- lombok -->
	<dependency>
	    <groupId>org.projectlombok</groupId>
	    <artifactId>lombok</artifactId>
	    <scope>provided</scope>
	</dependency>
	
	
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>

二、創建日誌文件:LoggerTest.java

在裏面有兩種寫法

第一種方法:在日誌類裏面都必須添加上:

1. 添加註解

@RunWith(SpringRunner.class)
@SpringBootTest

2. 類名.class

private final Logger logger = LoggerFactory.getLogger(LoggerTest.class);

package com.fjz.vxsell;

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

import lombok.extern.slf4j.Slf4j;

@RunWith(SpringRunner.class)
@SpringBootTest
public class LoggerTest {
	private final Logger logger = LoggerFactory.getLogger(LoggerTest.class);
	
	@Test
	public void test(){
		logger.debug("debug");
		logger.info("info");
		logger.error("error");
	}

}

 注:這種方法有點小麻煩,因爲每次用日誌的時候都得寫當前類

第二種方法:加上@Slf4j這個註解

 

三、日誌的級別

默認輸出該等級之上的日誌,順序是由高到底:

ERROR--WARN--INFO--DEBUG--TRACE

四、如果在info/warn/....中加上內容,有兩種方法

第一種方法:

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class LoggerTest {
	
	@Test
	public void test(){
		String name = "jack";
		String password = "123456";
		log.info("name: "+ name+", "+"password: "+password);
		
        log.debug("debug");
        log.error("error ");

但是如果是需要輸出多個name和password,會比較麻煩,有第二種方法展示

第二種方法:

結果爲:

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章