Spring Boot與日誌的關係

市面上的日誌框架;html

JUL、JCL、Jboss-logging、logback、log4j、log4j二、slf4j....java

日誌門面 (日誌的抽象層) 日誌實現 JCL(Jakarta Commons Logging) SLF4j(Simple Logging Facade for Java) jboss-logging Log4j JUL(java.util.logging) Log4j2 Logback 左邊選一個門面(抽象層)、右邊來選一個實現;web

日誌門面: SLF4J;spring

日誌實現:Logback;springboot

SpringBoot:底層是Spring框架,Spring框架默認是用JCL;‘app

​ SpringBoot選用 SLF4j和logback;框架

二、SLF4j使用spring-boot

一、如何在系統中使用SLF4j https://www.slf4j.orgui

之後開發的時候,日誌記錄方法的調用,不該該來直接調用日誌的實現類,而是調用日誌抽象層裏面的方法;spa

給系統裏面導入slf4j的jar和 logback的實現jar

import org.slf4j.Logger; import org.slf4j.LoggerFactory;

public class HelloWorld { public static void main(String[] args) { Logger logger = LoggerFactory.getLogger(HelloWorld.class); logger.info("Hello World"); } }

每個日誌的實現框架都有本身的配置文件。使用slf4j之後,配置文件仍是作成日誌實現框架本身自己的配置文件;

二、遺留問題

a(slf4j+logback): Spring(commons-logging)、Hibernate(jboss-logging)、MyBatis、xxxx

統一日誌記錄,即便是別的框架和我一塊兒統一使用slf4j進行輸出?

如何讓系統中全部的日誌都統一到slf4j;

一、將系統中其餘日誌框架先排除出去;

二、用中間包來替換原有的日誌框架;

三、咱們導入slf4j其餘的實現

三、SpringBoot日誌關係

<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
	</dependency>

SpringBoot使用它來作日誌功能;

<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-logging</artifactId>
	</dependency>

底層依賴關係

總結:

​ 1)、SpringBoot底層也是使用slf4j+logback的方式進行日誌記錄

​ 2)、SpringBoot也把其餘的日誌都替換成了slf4j;

​ 3)、中間替換包?

@SuppressWarnings("rawtypes") public abstract class LogFactory {

static String UNSUPPORTED_OPERATION_IN_JCL_OVER_SLF4J = "http://www.slf4j.org/codes.html#unsupported_operation_in_jcl_over_slf4j";

static LogFactory logFactory = new SLF4JLogFactory();

​ 4)、若是咱們要引入其餘框架?必定要把這個框架的默認日誌依賴移除掉? Spring框架用的是 commons-logging;

<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-core</artifactId>
		<exclusions>
			<exclusion>
				<groupId>commons-logging</groupId>
				<artifactId>commons-logging</artifactId>
			</exclusion>
		</exclusions>
	</dependency>

SpringBoot能自動適配全部的日誌,並且底層使用slf4j+logback的方式記錄日誌,引入其餘框架的時候,只須要把這個框架依賴的日誌框架排除掉便可;

四、日誌使用;

一、默認配置

SpringBoot默認幫咱們配置好了日誌;

//記錄器
Logger logger = LoggerFactory.getLogger(getClass());
@Test
public void contextLoads() {
	//System.out.println();

	//日誌的級別;
	//由低到高   trace<debug<info<warn<error
	//能夠調整輸出的日誌級別;日誌就只會在這個級別以之後的高級別生效
	logger.trace("這是trace日誌...");
	logger.debug("這是debug日誌...");
	//SpringBoot默認給咱們使用的是info級別的,沒有指定級別的就用SpringBoot默認規定的級別;root級別
	logger.info("這是info日誌...");
	logger.warn("這是warn日誌...");
	logger.error("這是error日誌...");


}
日誌輸出格式:
	%d表示日期時間,
	%thread表示線程名,
	%-5level:級別從左顯示5個字符寬度
	%logger{50} 表示logger名字最長50個字符,不然按照句點分割。 
	%msg:日誌消息,
	%n是換行符
-->
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n

SpringBoot修改日誌的默認配置

logging.level.com.atguigu=trace

#logging.path=

不指定路徑在當前項目下生成springboot.log日誌

能夠指定完整的路徑;

#logging.file=G:/springboot.log

在當前磁盤的根路徑下建立spring文件夾和裏面的log文件夾;使用 spring.log 做爲默認文件

logging.path=/spring/log

在控制檯輸出的日誌的格式

logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n

指定文件中日誌輸出的格式

logging.pattern.file=%d{yyyy-MM-dd} === [%thread] === %-5level === %logger{50} ==== %msg%n logging.file logging.path Example Description (none) (none) 只在控制檯輸出 指定文件名 (none) my.log 輸出日誌到my.log文件 (none) 指定目錄 /var/log 輸出到指定目錄的 spring.log 文件中 二、指定配置

給類路徑下放上每一個日誌框架本身的配置文件便可;SpringBoot就不使用他默認配置的了

Logging System Customization Logback logback-spring.xml, logback-spring.groovy, logback.xml or logback.groovy Log4j2 log4j2-spring.xml or log4j2.xml JDK (Java Util Logging) logging.properties logback.xml:直接就被日誌框架識別了;

logback-spring.xml:日誌框架就不直接加載日誌的配置項,由SpringBoot解析日誌配置,可使用SpringBoot的高級Profile功能

<springProfile name="staging"> <!-- configuration to be enabled when the "staging" profile is active --> 能夠指定某段配置只在某個環境下生效 </springProfile>

如:

<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> <!-- 日誌輸出格式: %d表示日期時間, %thread表示線程名, %-5level:級別從左顯示5個字符寬度 %logger{50} 表示logger名字最長50個字符,不然按照句點分割。 %msg:日誌消息, %n是換行符 --> <layout class="ch.qos.logback.classic.PatternLayout"> <springProfile name="dev"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ----> [%thread] ---> %-5level %logger{50} - %msg%n</pattern> </springProfile> <springProfile name="!dev"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ==== [%thread] ==== %-5level %logger{50} - %msg%n</pattern> </springProfile> </layout> </appender> 若是使用logback.xml做爲日誌配置文件,還要使用profile功能,會有如下錯誤

no applicable action for [springProfile]

五、切換日誌框架

能夠按照slf4j的日誌適配圖,進行相關的切換;

slf4j+log4j的方式;

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <exclusions>
    <exclusion>
      <artifactId>logback-classic</artifactId>
      <groupId>ch.qos.logback</groupId>
    </exclusion>
    <exclusion>
      <artifactId>log4j-over-slf4j</artifactId>
      <groupId>org.slf4j</groupId>
    </exclusion>
  </exclusions>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
</dependency>

切換爲log4j2

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-logging</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
相關文章
相關標籤/搜索