文章作者:Tyan
博客:noahsnail.com | CSDN | 簡書
Spring Boot uses Commons Logging for all internal logging, but leaves the underlying log implementation open. Default configurations are provided for Java Util Logging, Log4J2 and Logback. In each case loggers are pre-configured to use console output with optional file output also available.
Spring Boot所有的內部日志都采取Commons Logging,但開放了底層的日志實現。提供了對Java Util Logging,Log4J2和Logback的默許實現。在每一個logger中都預先配置使用控制臺作為輸出,同時也可用可選的文件輸出。
By default, If you use the ‘Starters’, Logback will be used for logging. Appropriate Logback routing is also included to ensure that dependent libraries that use Java Util Logging, Commons Logging, Log4J or SLF4J will all work correctly.
默許情況下,如果使用Starters
,Logback將作為日志。也要包括恰當的Logback規則來保證依賴庫使用Java Util Logging,Commons Logging,Log4J或SLF4J都能正確工作。
There are a lot of logging frameworks available for Java. Don’t worry if the above list seems confusing. Generally you won’t need to change your logging dependencies and the Spring Boot defaults will work just fine.
在Java中有許多日志框架可用。沒必要擔心上面的列表看起來有點混亂。通常情況下你不需要改變你的日志依賴,Spring Boot默許情況下能很好的工作。
The default log output from Spring Boot looks like this:
Spring Boot默許的日志輸出看起來以下:
2014-03-05 10:57:51.112 INFO 45469 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.52
2014-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2014-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1358 ms
2014-03-05 10:57:51.698 INFO 45469 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2014-03-05 10:57:51.702 INFO 45469 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
The following items are output:
Date and Time?—?Millisecond precision and easily sortable.
Log Level?—?ERROR, WARN, INFO, DEBUG or TRACE.
Process ID.
A ---
separator to distinguish the start of actual log messages.
Thread name?—?Enclosed in square brackets (may be truncated for console output).
Logger name?—?This is usually the source class name (often abbreviated).
The log message.
下面的是輸出項:
Date and Time?—?精確到毫秒且容易排序。
Log級別?—?ERROR,WARN,INFO,DEBUG或TRACE。
進程ID。
---
分隔符來辨別真實的日志信息的開始。
線程名稱?—?用方括號包裹(在控制臺輸出中常常是縮減的)。
Logger名稱?—?源類名(常常是簡寫)。
日志信息。
Logback does not have a FATAL level (it is mapped to ERROR)
Logback沒有FATAL級別(它映照到ERROR)。
The default log configuration will echo messages to the console as they are written. By default ERROR
, WARN
and INFO
level messages are logged. You can also enable a “debug” mode by starting your application with a --debug
flag.
默許的日志配置會將信息輸出到控制臺。默許情況下會輸出ERROR
,WARN
和INFO
級別的信息。你也能夠通過--debug
來啟動你的利用,從而啟用“debug”模式。
$ java -jar myapp.jar --debug
you can also specify
debug=true
in yourapplication.properties
.
你也能夠在
application.properties
指定debug=true
。
When the debug mode is enabled, a selection of core loggers (embedded container, Hibernate and Spring Boot) are configured to output more information. Enabling the debug mode does not configure your application to log all messages with DEBUG
level.
當啟用調試模式時,配置選擇的核心logger(嵌入式容器,Hibernate和Spring Boot)來輸出更多的信息。啟動調試模式不會將你的利用配置成輸出所有DEBUG
級別的信息。
Alternatively, you can enable a “trace” mode by starting your application with a --trace
flag (or trace=true
in your application.properties
). This will enable trace logging for a selection of core loggers (embedded container, Hibernate schema generation and the whole Spring portfolio).
或,你可以用--trace
標記(或在application.properties
中添加trace=true
)啟動你的利用,從而啟用“trace”模式。這將在選擇的核心logger中(嵌入式容器,Hibernate模式生成和全部Spring文件夾)啟用trace日志。
If your terminal supports ANSI, color output will be used to aid readability. You can set spring.output.ansi.enabled
to a supported value to override the auto detection.
如果你的終端支持ANSI,色彩輸出可以用來輔助瀏覽。你可以為spring.output.ansi.enabled
設置1個支持值來覆蓋自動檢測。
Color coding is configured using the %clr
conversion word. In its simplest form the converter will color the output according to the log level, for example:
色彩編碼用%clr
轉換詞來配置。最簡單的情勢是根據日志級別進行色彩輸出,例如:
%clr(%5p)
The mapping of log level to a color is as follows:
日志級別與色彩的映照以下:
Level | Color |
---|---|
FATAL | Red |
ERROR | Red |
WARN | Yellow |
INFO | Green |
DEBUG | Green |
TRACE | Green |
Alternatively, you can specify the color or style that should be used by providing it as an option to the conversion. For example, to make the text yellow:
或,你可以通過在轉換器中提供選項來指定應當試用的色彩或風格。例如,為了使文本顯示黃色:
%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){yellow}
The following colors and styles are supported:
支持下面的色彩和風格:
By default, Spring Boot will only log to the console and will not write log files. If you want to write log files in addition to the console output you need to set a logging.file
or logging.path
property (for example in your application.properties
).
默許情況下,Spring Boot只能輸出日志到控制臺,不會寫入日志文件。如果你想除控制臺以外還想寫日志文件,你需要設置logging.file
或logging.path
屬性(例如,在你的application.properties
)。
The following table shows how the logging.*
properties can be used together:
下表顯示了logging.*
屬性怎樣在1起使用:
Table 26.1. Logging properties
logging.file | logging.path | Example | Description |
---|---|---|---|
(none | (none) | Console only logging. | |
Specific file | (none) | my.log | Writes to the specified log file. Names can be an exact location or relative to the current directory. |
(none) | Specific directory | /var/log | Writes spring.log to the specified directory. Names can be an exact location or relative to the current directory. |
表 26.1. Logging屬性
logging.file | logging.path | Example | Description |
---|---|---|---|
(none | (none) | 只輸出到控制臺 | |
指定文件 | (none) | my.log | 寫入指定的日志文件。名字可以是1個絕對位置或相對當前目錄。 |
(none) | Specific directory | /var/log | 寫spring.log 到1個指定的目錄。名字可以是1個絕對位置或相對當前目錄。 |
Log files will rotate when they reach 10 MB and as with console output, ERROR
, WARN
and INFO
level messages are logged by default.
當日志文件到達10M光陰志文件將循環,至于控制臺輸出,默許情況下只輸出ERROR
,WARN
和INFO
級別的信息。
The logging system is initialized early in the application lifecycle and as such logging properties will not be found in property files loaded via
@PropertySource
annotations.
日志系統在利用程序的生命周期初期進行初始化,一樣地,通過
@PropertySource
注解加載的屬性文件中將不會發現日志屬性。
Logging properties are independent of the actual logging infrastructure. As a result, specific configuration keys (such as
logback.configurationFile
for Logback) are not managed by spring Boot.
日志屬性獨立于真實的日志基礎架構以外。因此,Spring Boot不管理特定的配置主鍵(例如Logback的
logback.configurationFile
)。
All the supported logging systems can have the logger levels set in the Spring Environment
(so for example in application.properties
) using ‘logging.level.*=LEVEL’ where ‘LEVEL’ is one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF. The root
logger can be configured using logging.level.root
. Example application.properties
:
Spring支持的所有日志系統都可以在Spring的Environment
中設置日志級別(例如在application.properties
設置),使用logging.level.*=LEVEL
進行設置,LEVEL
是TRACE,DEBUG,INFO,WARN,ERROR,FATAL,OFF中的1個。root
日志器可以用logging.level.root
來配置。例如在application.properties
中:
logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
By default Spring Boot remaps Thymeleaf
INFO
messages so that they are logged atDEBUG
level. This helps to reduce noise in the standard log output. SeeLevelRemappingAppender
for details of how you can apply remapping in your own configuration.
默許情況下Spring Boot重新映照Thymeleaf的
INFO
信息,為了它們能在DEBUG
級別進行輸出。這能幫助減少標準日志輸出中的噪聲。關于怎樣在你自己的配置中使用重新映照的更多細節請看LevelRemappingAppender
。
The various logging systems can be activated by including the appropriate libraries on the classpath, and further customized by providing a suitable configuration file in the root of the classpath, or in a location specified by the Spring Environment
property logging.config
.
在類路徑中通過包括恰當的庫可以激活各種日志系統,在類路徑的根目錄中提供1個適合的配置文件可以對日志系統進行更進1步的定制,或將配置文件放在Spring Environment
的logging.config
屬性中指定的位置。
You can force Spring Boot to use a particular logging system using the org.springframework.boot.logging.LoggingSystem
system property. The value should be the fully-qualified class name of a LoggingSystem
implementation. You can also disable Spring Boot’s logging configuration entirely by using a value of none
.
你可使用系統屬性org.springframework.boot.logging.LoggingSystem
來強迫Spring Boot使用1個特定的日志系統。它的值應當是全限定類名的LoggingSystem
實現。你也能夠通過使用值none
來完全禁用Spring Boot的日志配置。
Since logging is initialized before the
ApplicationContext
is created, it isn’t possible to control logging from@PropertySources
in Spring@Configuration
files. System properties and the conventional Spring Boot external configuration files work just fine.
由于日志是在
ApplicationContext
創建之前初始化,因此不可能從Spring@Configuration
文件中的@PropertySources
來控制日志。系統屬性和傳統的Spring Boot外部配置文件可以工作的很好。
Depending on your logging system, the following files will be loaded:
根據你的日志系統,將會加載下面的文件:
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 |
When possible we recommend that you use the
-spring
variants for your logging configuration (for examplelogback-spring.xml
rather thanlogback.xml
). If you use standard configuration locations, Spring cannot completely control log initialization.
我們建議你盡量的使用
-spring
變種來進行日志配置(例如,logback-spring.xml
而不是logback.xml
)。如果你在使用標準的配置路徑,Spring不能完全控制日志的初始化。
There are known classloading issues with Java Util Logging that cause problems when running from an ‘executable jar’. We recommend that you avoid it if at all possible.
在Java Java Util Logging中存在類加載問題,當從
executable jar
運行時會引發問題。我們建議你盡量的避免它。
To help with the customization some other properties are transferred from the Spring Environment
to System properties:
為了幫助定制1些其它屬性,從Spring Environment
中轉移到系統屬性中:
Spring Environment | System Property | Comments |
---|---|---|
logging.exception-conversion-word | LOG_EXCEPTION_CONVERSION_WORD | The conversion word that’s used when logging exceptions. |
logging.file | LOG_FILE | Used in default log configuration if defined. |
logging.path | LOG_PATH | Used in default log configuration if defined. |
logging.pattern.console | CONSOLE_LOG_PATTERN | The log pattern to use on the console (stdout). (Only supported with the default logback setup.) |
logging.pattern.file | FILE_LOG_PATTERN | The log pattern to use in a file (if LOG_FILE enabled). (Only supported with the default logback setup.) |
logging.pattern.level | LOG_LEVEL_PATTERN | The format to use to render the log level (default %5p). (Only supported with the default logback setup.) |
PID | PID | The current process ID (discovered if possible and when not already defined as an OS environment variable). |
Spring Environment | System Property | Comments |
---|---|---|
logging.exception-conversion-word | LOG_EXCEPTION_CONVERSION_WORD | 當日志出現異常時會用到這個轉換詞。 |
logging.file | LOG_FILE | 如果定義了,會在默許的日志配置中使用。 |
logging.path | LOG_PATH | 如果定義了,會在默許的日志配置中使用。 |
logging.pattern.console | CONSOLE_LOG_PATTERN | 用在控制臺中的日志模式(stdout)。(只支持默許的Logback設置。) |
logging.pattern.file | FILE_LOG_PATTERN | 用在文件中的日志模式 (如果啟用LOG_FILE )。(只支持默許的Logback設置。) |
logging.pattern.level | LOG_LEVEL_PATTERN | 這類模式用來實行日志級別(默許%5p )。 (只支持默許的Logback設置。) |
PID | PID | 當前的進程ID |
All the logging systems supported can consult System properties when parsing their configuration files. See the default configurations in spring-boot.jar
for examples.
當轉換配置文件時,所有的日志系統都支持查詢系統屬性。例如spring-boot.jar
的默許配置。
If you want to use a placeholder in a logging property, you should use Spring Boot’s syntax and not the syntax of the underlying framework. Notably, if you’re using Logback, you should use
:
as the delimiter between a property name and its default value and not:-
.
如果你在日志屬性中想使用占位符,你應當試用Spring Boot的語法而不是底層框架的語法。特別是,如果你在使用Logback,你應當使用
:
作為屬性名和默許值之間的分隔符,而不是:-
。
You can add MDC and other ad-hoc content to log lines by overriding only the
LOG_LEVEL_PATTERN
(orlogging.pattern.level
with Logback). For example, if you uselogging.pattern.level=user:%X{user} %5p
then the default log format will contain an MDC entry for “user” if it exists, e.g.
2015-09⑶0 12:30:04.031 user:juergen INFO 22174 --- [ nio⑻080-exec-0] demo.Controller
Handling authenticated request
你可以通太重寫
LOG_LEVEL_PATTERN
(或Logback中的logging.pattern.level
)來添加MDC和其它的專門內容來日志行中。例如,你可使用logging.pattern.level=user:%X{user} %5p
,默許的日志情勢將包括MDC輸入,如果它存在的話,例如:
2015-09⑶0 12:30:04.031 user:juergen INFO 22174 --- [ nio⑻080-exec-0] demo.Controller
Handling authenticated request
Spring Boot includes a number of extensions to Logback which can help with advanced configuration. You can use these extensions in your logback-spring.xml
configuration file.
Spring Boot包括許多Logback的擴大,這有助于進行更高級的配置。你可以在你的logback-spring.xml
配置文件中使用這些擴大。
You cannot use extensions in the standard
logback.xml
configuration file since it’s loaded too early. You need to either uselogback-spring.xml
or define alogging.config
property.
你不能在標準的
logback.xml
配置文件中使用擴大,由于它加載的太早了。你需要使用logback-spring.xml
或定義logging.config
屬性。
The <springProfile>
tag allows you to optionally include or exclude sections of configuration based on the active Spring profiles. Profile sections are supported anywhere within the <configuration>
element. Use the name
attribute to specify which profile accepts the configuration. Multiple profiles can be specified using a comma-separated list.
<springProfile>
標簽允許你自由的包括或排除基于激活的Spring profiles的配置的1部份。在<configuration>
元素的任何地方都支持Profile部份。使用name
屬性來指定哪個profile接受配置。多個profiles可以用1個逗號分隔的列表來指定。
<springProfile name="staging">
<!-- configuration to be enabled when the "staging" profile is active -->
</springProfile>
<springProfile name="dev, staging">
<!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
</springProfile>
<springProfile name="!production">
<!-- configuration to be enabled when the "production" profile is not active -->
</springProfile>
The <springProperty>
tag allows you to surface properties from the Spring Environment
for use within Logback. This can be useful if you want to access values from your application.properties
file in your logback configuration. The tag works in a similar way to Logback’s standard <property>
tag, but rather than specifying a direct value
you specify the source
of the property (from the Environment
). You can use the scope
attribute if you need to store the property somewhere other than in local
scope. If you need a fallback value in case the property is not set in the Environment
, you can use the defaultValue
attribute.
<springProperty>
標簽允許你在Logback使用從Spring Environment
取得的屬性。如果你想在你的Logback配置中訪問application.properties
文件中的屬性,這是非常有用的。這個標簽與Logback的標準<property>
標簽的作用方式類似,但不是為你指定的source
屬性(從Environment
中)指定1個直接的value
。如果你需要在某個地方而不是在local
作用域中存儲屬性,你可使用scope
屬性。如果你需要1個備用值以防屬性沒有在Environment
中設置,你可使用defaultValue
屬性。
<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host"
defaultValue="localhost"/>
<appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender">
<remoteHost>${fluentHost}</remoteHost>
...
</appender>
The
RelaxedPropertyResolver
is used to accessEnvironment
properties. If specify thesource
in dashed notation (my-property-name
) all the relaxed variations will be tried (myPropertyName
,MY_PROPERTY_NAME
etc).
RelaxedPropertyResolver
是用來訪問Environment
屬性。如果在破折號符號中指定了source
,則會嘗試所有的不嚴格的變種(myPropertyName
,MY_PROPERTY_NAME
等)。