springboot搭建日誌框架(三)

目錄

1、日誌框架

2、SLF4j使用

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

2、遺留問題

3、SpringBoot日誌關係

4、日誌使用;

1、默認配置

2、指定配置

5、切換日誌框架


市面上的日誌框架;

JUL、JCL、Jboss-logging、logback、log4j、log4j2、slf4j....

左邊選一個門面(抽象層)、右邊來選一個實現;

日誌門面: SLF4J;

日誌實現:Logback;

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

==SpringBoot選用 SLF4j和logback;==

 

2、SLF4j使用

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

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

給系統裏面導入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");
  }
}

圖示:

 

2、遺留問題

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

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

如何讓系統中所有的日誌都統一到slf4j;

==1、將系統中其他日誌框架先排除出去;==

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

==3、我們導入slf4j其他的實現==

3、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的方式記錄日誌,引入其他框架的時候,只需要把這個框架依賴的日誌框架排除掉即可;==

4、日誌使用;

1、默認配置

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

2、指定配置

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

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]

5、切換日誌框架

可以按照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>

 

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