<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Spring Boot 日志指南 > 原文: [https://howtodoinjava.com/spring-boot2/logging/spring-boot-logging-configurations/](https://howtodoinjava.com/spring-boot2/logging/spring-boot-logging-configurations/) **登錄 Spring Boot** 非常靈活且易于配置。 Spring Boot 通過一些簡單的配置即可支持各種日志記錄提供程序。 在本教程中,我們將介紹 Spring Boot 支持的各種日志記錄選項和配置。 ## 1\. 默認零配置日志 啟用了 Spring Boot 活動的日志記錄由[`spring-boot-starter-logging`](https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-starters/spring-boot-starter-logging/pom.xml)工件確定,并且它是自動配置,可啟用任何受支持的日志記錄提供程序([`java.util.logging`](https://docs.oracle.com/javase/8/docs/api//java/util/logging/package-summary.html),[Log4J2](https://logging.apache.org/log4j/2.x/) 和 [Logback](https://logback.qos.ch/))基于提供的配置。 如果我們不提供任何特定于日志記錄的配置,我們仍將看到打印在“控制臺”中的日志。 這是因為在 Spring Boot 中使用 **Logback** 的**默認日志支持**。 Spring Boot 的內部日志記錄是用 Apache Commons Logging 編寫的,因此它是唯一且唯一的依賴項。 直到啟動 1.x,我們都必須手動導入它。 從運行 2.x 開始,它會進行過渡下載。 更準確地說,`spring-boot-starter-web`取決于`spring-boot-starter-logging`,它為我們拉入`spring-jcl`。 Spring Boot 自動配置提供使用`Logback`的默認日志記錄,并且這些[配置文件](https://github.com/spring-projects/spring-boot/tree/master/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback)中提供了默認配置。 #### 1.1. 添加日志語句 要在應用程序代碼中添加日志語句,請使用 SLF4J 中的`org.slf4j.Logger`和`org.slf4j.LoggerFactory`。 它提供了許多有用的日志記錄方法,也使日志記錄實現與應用程序脫鉤。 `Application.java` ```java import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { private static final Logger LOGGER=LoggerFactory.getLogger(Application.class); public static void main(String[] args) { SpringApplication.run(Application.class, args); LOGGER.info("Simple log statement with inputs {}, {} and {}", 1,2,3); } } ``` `Console` ```java 2019-07-28 12:16:57.129 INFO 3416 --- [main] com.howtodoinjava.demo.Application: Simple log statement with inputs 1, 2 and 3 ``` #### 1.2. 記錄級別 Logback 支持將`ERROR`,`WARN`,`INFO`,`DEBUG`或`TRACE`作為日志記錄級別。 默認情況下,日志記錄級別設置為`INFO`。 這意味著代碼`DEBUG`和`TRACE`消息不可見。 要啟用調試或跟蹤日志記錄,我們可以在`application.properties`文件中設置日志記錄級別。 另外,我們可以在啟動應用程序時在命令行上傳遞 – `debug`或 – `trace`參數。 `Configuration` ```java # In properties file debug=true # In Console $ java -jar target/my-app-0.0.1-SNAPSHOT.jar --trace ``` 我們也可以將日志記錄級別應用于特定的軟件包。 可以在控制臺或`application.properties`文件中完成。 `Configuration` ```java # In Console -Dlogging.level.org.springframework=ERROR -Dlogging.level.com.howtodoinjava=TRACE # In properties file logging.level.org.springframework=ERROR logging.level.com.howtodoinjava=TRACE ``` > 如果使用不同的日志級別多次定義了程序包的日志級別,則將使用最低級別。 `TRACE`最低,`ERROR`最高。 #### 1.3. 日志格式 在[`defaults.xml`](https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml)文件中提到了默認的日志語句格式。 `defaults.xml` ```java <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" /> <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" /> <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" /> <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}) {faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}) {cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/> <property name="FILE_LOG_PATTERN" value="${FILE_LOG_PATTERN:-%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/> ``` 輸出以下信息。 * **日期和時間**:毫秒精度,易于排序。 * **日志級別**:`ERROR`,`WARN`,`INFO`,`DEBUG`或`TRACE`。 * 進程 ID。 * `---`分隔符用于區分實際日志消息的開始。 * **線程名稱**:放在方括號中(對于控制臺輸出,可能會被截斷)。 * **記錄器名稱**:這通常是源類名稱(通常縮寫)。 * 日志消息。 要**自定義日志格式**,請使用`logging.pattern.console`和`logging.pattern.file`屬性。 `application.properties` ```java # Logging pattern for the console logging.pattern.console= %d{yyyy-MM-dd HH:mm:ss} - %logger{36} - %msg%n # Logging pattern for file logging.pattern.file= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg% ``` #### 1.4. 記錄到文件 默認情況下,spring boot 日志僅記錄到控制臺。 如果要啟用文件記錄,可以使用簡單屬性`logging.file`或`logging.path`輕松進行。 使用`logging.path`時,它將在上述包中創建一個名為`spring.log`的文件。 `application.properties` ```java # Output to a temp_folder/file logging.file=c:/temp/application.log #logging.path=/my-folder/ # Logging pattern for file logging.pattern.file= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg% ``` ## 2\. Logback 日志記錄 對于大多數用例,默認日志記錄已經足夠了。 但是有時在企業應用程序中,我們需要對具有其他復雜需求的日志進行更好的控制。 在那種情況下,具有專用的日志日志配置是合適的。 默認情況下,Spring Boot 使用 logback,因此要自定義其行為,我們只需要在類路徑中添加`logback.xml`并定義該文件的自定義即可。 `logback.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="LOG_LOCATION" value="c:/temp" /> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} - %logger{36} - %msg%n</pattern> </encoder> </appender> <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <File>{LOG_LOCATION}/mylog.log</File> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} - %logger{36} - %msg%n</pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_LOCATION}/archived/mylog-%d{yyyy-MM-dd}.%i.log </fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>10MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> </appender> <root level="INFO"> <appender-ref ref="CONSOLE"/> <appender-ref ref="FILE"/> </root> <!-- Application logs at trace level --> <logger name="com.howtodoinjava" level="trace" additivity="false"> <appender-ref ref="RollingFile" /> <appender-ref ref="Console" /> </logger> </configuration> ``` ## 3\. Log4j2 記錄 #### 步驟 1:排除 logback,并包括 log4j2 如前所述,spring boot 使用 logback 作為默認值。 因此,如果我們必須使用其他任何日志記錄框架,例如 `log4j2`,我們必須**從應用程序的類路徑中排除登錄**。 另外,將`spring-boot-starter-log4j2`添加到類路徑。 `pom.xml` ```java <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> ``` #### 步驟 2:添加 log4j2 配置文件 現在,在類路徑(通常在**資源**文件夾中)中添加特定于 log4j2 的配置文件。 可以將其命名為以下任意一種: * `log4j2-spring.xml` * `log4j2.xml` 如果我們在其他任何文件(例如`log4j2.properties`,`applogs.xml`等)中都有日志日志配置,則可以使用`logging.file`屬性指定其路徑`application.properties`文件。 `log4j2.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN" monitorInterval="30"> <Properties> <Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property> <Property name="APP_LOG_ROOT">c:/temp</Property> </Properties> <Appenders> <Console name="console" target="SYSTEM_OUT"> <PatternLayout pattern="${LOG_PATTERN}" /> </Console> <RollingFile name="file" fileName="${APP_LOG_ROOT}/SpringBoot2App/application.log" filePattern="${APP_LOG_ROOT}/SpringBoot2App/application-%d{yyyy-MM-dd}-%i.log"> <PatternLayout pattern="${LOG_PATTERN}" /> <Policies> <SizeBasedTriggeringPolicy size="19500KB" /> </Policies> <DefaultRolloverStrategy max="1" /> </RollingFile> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="console" /> <AppenderRef ref="file" /> </Root> </Loggers> </Configuration> ``` #### 步驟 3:有或沒有 Slf4j 默認情況下,如果您正在使用 SLF4J 記錄器類,即`org.slf4j.Logger`和`org.slf4j.LoggerFactory`,則無需更改應用程序代碼,所有日志語句將繼續在目標附加程序中打印。 如果您打算僅使用 log4j2 特定的類,請使用`org.apache.logging.log4j.Logger`和`org.apache.logging.log4j.LogManager`。 我將建議使用 SLF4J 記錄器類。 `SLF4J logger classes` ```java import org.slf4j.Logger; import org.slf4j.LoggerFactory; @SpringBootApplication public class Application { private static final Logger LOGGER = LoggerFactory.getLogger(Application.class); public static void main(String[] args) { SpringApplication.run(Application.class, args); LOGGER.info("Simple log statement with inputs {}, {} and {}", 1,2,3); } } ``` `LOG4J2 logger classes` ```java import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @SpringBootApplication public class Application { private static Logger LOGGER = LogManager.getLogger(Application.class); public static void main(String[] args) { SpringApplication.run(Application.class, args); LOGGER.info("Simple log statement with inputs 1, 2 and 3"); } } ``` ## 4\. 有關 Spring Boot 日志記錄的更多示例 1. [Spring Boot 2 `log4j2.xml`示例](https://howtodoinjava.com/spring-boot2/spring-boot-log4j2-config/) 2. [Spring Boot 2 `log4j2.properties`示例](https://howtodoinjava.com/spring-boot2/spring-boot2-log4j2-properties/) 3. [`application.yml`和 Spring Boot 日志](https://howtodoinjava.com/spring-boot/configure-logging-application-yml/) 4. [`application.properties`和 Spring 日志](https://howtodoinjava.com/spring-boot/logging-application-properties/) 5. [Tomcat 和 Jetty 的 SpringBoot 嵌入式服務器日志配置](https://howtodoinjava.com/spring-boot2/embedded-server-logging-config/) 6. [AspectJ AOP 和 Spring Boot 性能日志](https://howtodoinjava.com/spring-boot2/performance-logging-aspectj-aop/) 7. [Lombok 和 Spring Boot 日志](https://howtodoinjava.com/spring-boot2/logging-with-lombok/) 8. [Spring Boot 多個日志文件示例](https://howtodoinjava.com/spring-boot2/multiple-log-files/) 9. [Spring Boot 控制臺日志配置示例](https://howtodoinjava.com/spring-boot2/console-logging-configuration/) 10. [Spring 配置文件特定的日志示例](https://howtodoinjava.com/spring-boot2/profile-specific-logging/) 請問一下您與 Spring Boot 中的**日志配置有關的問題**。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看