天天看點

6.日志

目錄

  • 6.日志
    • 日志架構
    • SLF4j使用
    • SpringBoot日志關系
    • 日志使用
      • 1、預設配置
      • 2、指定配置
    • 切換日志架構

d日志門面 (日志的抽象層) 日志實作
JCL(Jakarta Commons Logging) 、 SLF4j(Simple Logging Facade for Java) 、 jboss-logging Log4j 、 JUL(java.util.logging) 、 Log4j2 、 Logback

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

日志門面: SLF4J;

日志實作:Logback;

SpringBoot:底層是Spring架構,Spring架構預設是用JCL;

​ SpringBoot選用 SLF4j和logback;

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

圖示;

6.日志

每一個日志的實作架構都有自己的配置檔案。使用slf4j以後,配置檔案還是做成日志實作架構自己本身的配置檔案;

2、遺留問題

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

統一日志記錄,即使是别的架構和我一起統一使用slf4j進行輸出?

6.日志

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

1、将系統中其他日志架構先排除出去;

2、用中間包來替換原有的日志架構;

3、我們導入slf4j其他的實作

<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>
           

底層依賴關系

6.日志

總結:

​ 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();
           
6.日志

​ 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修改日志的預設配置

application.properties配置檔案:

# 調整日志級别
logging.level.com.atguigu=trace


# 不指定路徑在目前項目下生成springboot.log日志檔案
#logging.file=springboot.log
# 可以指定完整的路徑;
#logging.file=G:/springboot.log

#指定日志輸出路徑的目錄,日志檔案使用springboot預設的檔案名spring.log
#logging.path=
# 在目前磁盤的根路徑下建立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) 隻在控制台輸出
指定檔案名 my.log 輸出日志到my.log檔案
指定目錄 /var/log 輸出到指定目錄的 spring.log 檔案中

給類路徑下(resources目錄)放上每個日志架構自己的配置檔案即可;SpringBoot就不使用他預設配置的了

比如在resources目錄下放入logback.xml,檔案内部是日志格式内容。或者放其他日志架構的檔案,檔案名是下方表中Customization内的名稱

Logging System Customization
Logback

logback-spring.xml

,

logback-spring.groovy

logback.xml

or

logback.groovy

Log4j2

log4j2-spring.xml

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>

           

如:

logback.xml日志檔案:

<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]

不推薦使用log4j,因為存在問題被slf4j+logback日志取代。

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

<!--Log4j架構:spring boot預設slf4j轉換為log4j-->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
</dependency>

           
<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>

<!--log4j2依賴-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>