文章作者:Tyan
博客:noahsnail.com | CSDN | 簡(jiǎn)書(shū)
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所有的內(nèi)部日志都采取Commons Logging,但開(kāi)放了底層的日志實(shí)現(xiàn)。提供了對(duì)Java Util Logging,Log4J2和Logback的默許實(shí)現(xiàn)。在每一個(gè)logger中都預(yù)先配置使用控制臺(tái)作為輸出,同時(shí)也可用可選的文件輸出。
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將作為日志。也要包括恰當(dāng)?shù)腖ogback規(guī)則來(lái)保證依賴庫(kù)使用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中有許多日志框架可用。沒(méi)必要擔(dān)心上面的列表看起來(lái)有點(diǎn)混亂。通常情況下你不需要改變你的日志依賴,Spring Boot默許情況下能很好的工作。
The default log output from Spring Boot looks like this:
Spring Boot默許的日志輸出看起來(lái)以下:
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.
下面的是輸出項(xiàng):
Date and Time?—?精確到毫秒且容易排序。
Log級(jí)別?—?ERROR,WARN,INFO,DEBUG或TRACE。
進(jìn)程ID。
---
分隔符來(lái)辨別真實(shí)的日志信息的開(kāi)始。
線程名稱?—?用方括號(hào)包裹(在控制臺(tái)輸出中常常是縮減的)。
Logger名稱?—?源類名(常常是簡(jiǎn)寫(xiě))。
日志信息。
Logback does not have a FATAL level (it is mapped to ERROR)
Logback沒(méi)有FATAL級(jí)別(它映照到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.
默許的日志配置會(huì)將信息輸出到控制臺(tái)。默許情況下會(huì)輸出ERROR
,WARN
和INFO
級(jí)別的信息。你也能夠通過(guò)--debug
來(lái)啟動(dòng)你的利用,從而啟用“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.
當(dāng)啟用調(diào)試模式時(shí),配置選擇的核心logger(嵌入式容器,Hibernate和Spring Boot)來(lái)輸出更多的信息。啟動(dòng)調(diào)試模式不會(huì)將你的利用配置成輸出所有DEBUG
級(jí)別的信息。
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
標(biāo)記(或在application.properties
中添加trace=true
)啟動(dòng)你的利用,從而啟用“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,色彩輸出可以用來(lái)輔助瀏覽。你可以為spring.output.ansi.enabled
設(shè)置1個(gè)支持值來(lái)覆蓋自動(dòng)檢測(cè)。
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
轉(zhuǎn)換詞來(lái)配置。最簡(jiǎn)單的情勢(shì)是根據(jù)日志級(jí)別進(jìn)行色彩輸出,例如:
%clr(%5p)
The mapping of log level to a color is as follows:
日志級(jí)別與色彩的映照以下:
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:
或,你可以通過(guò)在轉(zhuǎn)換器中提供選項(xiàng)來(lái)指定應(yīng)當(dāng)試用的色彩或風(fēng)格。例如,為了使文本顯示黃色:
%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){yellow}
The following colors and styles are supported:
支持下面的色彩和風(fēng)格:
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只能輸出日志到控制臺(tái),不會(huì)寫(xiě)入日志文件。如果你想除控制臺(tái)以外還想寫(xiě)日志文件,你需要設(shè)置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) | 只輸出到控制臺(tái) | |
指定文件 | (none) | my.log | 寫(xiě)入指定的日志文件。名字可以是1個(gè)絕對(duì)位置或相對(duì)當(dāng)前目錄。 |
(none) | Specific directory | /var/log | 寫(xiě)spring.log 到1個(gè)指定的目錄。名字可以是1個(gè)絕對(duì)位置或相對(duì)當(dāng)前目錄。 |
Log files will rotate when they reach 10 MB and as with console output, ERROR
, WARN
and INFO
level messages are logged by default.
當(dāng)日志文件到達(dá)10M光陰志文件將循環(huán),至于控制臺(tái)輸出,默許情況下只輸出ERROR
,WARN
和INFO
級(jí)別的信息。
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.
日志系統(tǒng)在利用程序的生命周期初期進(jìn)行初始化,一樣地,通過(guò)
@PropertySource
注解加載的屬性文件中將不會(huì)發(fā)現(xiàn)日志屬性。
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.
日志屬性獨(dú)立于真實(shí)的日志基礎(chǔ)架構(gòu)以外。因此,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支持的所有日志系統(tǒng)都可以在Spring的Environment
中設(shè)置日志級(jí)別(例如在application.properties
設(shè)置),使用logging.level.*=LEVEL
進(jìn)行設(shè)置,LEVEL
是TRACE,DEBUG,INFO,WARN,ERROR,F(xiàn)ATAL,OFF中的1個(gè)。root
日志器可以用logging.level.root
來(lái)配置。例如在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
級(jí)別進(jìn)行輸出。這能幫助減少標(biāo)準(zhǔn)日志輸出中的噪聲。關(guān)于怎樣在你自己的配置中使用重新映照的更多細(xì)節(jié)請(qǐng)看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
.
在類路徑中通過(guò)包括恰當(dāng)?shù)膸?kù)可以激活各種日志系統(tǒng),在類路徑的根目錄中提供1個(gè)適合的配置文件可以對(duì)日志系統(tǒng)進(jìn)行更進(jìn)1步的定制,或?qū)⑴渲梦募旁赟pring 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
.
你可使用系統(tǒng)屬性org.springframework.boot.logging.LoggingSystem
來(lái)強(qiáng)迫Spring Boot使用1個(gè)特定的日志系統(tǒng)。它的值應(yīng)當(dāng)是全限定類名的LoggingSystem
實(shí)現(xiàn)。你也能夠通過(guò)使用值none
來(lái)完全禁用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
創(chuàng)建之前初始化,因此不可能從Spring@Configuration
文件中的@PropertySources
來(lái)控制日志。系統(tǒng)屬性和傳統(tǒng)的Spring Boot外部配置文件可以工作的很好。
Depending on your logging system, the following files will be loaded:
根據(jù)你的日志系統(tǒng),將會(huì)加載下面的文件:
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
變種來(lái)進(jìn)行日志配置(例如,logback-spring.xml
而不是logback.xml
)。如果你在使用標(biāo)準(zhǔn)的配置路徑,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中存在類加載問(wèn)題,當(dāng)從
executable jar
運(yùn)行時(shí)會(huì)引發(fā)問(wèn)題。我們建議你盡量的避免它。
To help with the customization some other properties are transferred from the Spring Environment
to System properties:
為了幫助定制1些其它屬性,從Spring Environment
中轉(zhuǎn)移到系統(tǒng)屬性中:
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 | 當(dāng)日志出現(xiàn)異常時(shí)會(huì)用到這個(gè)轉(zhuǎn)換詞。 |
logging.file | LOG_FILE | 如果定義了,會(huì)在默許的日志配置中使用。 |
logging.path | LOG_PATH | 如果定義了,會(huì)在默許的日志配置中使用。 |
logging.pattern.console | CONSOLE_LOG_PATTERN | 用在控制臺(tái)中的日志模式(stdout)。(只支持默許的Logback設(shè)置。) |
logging.pattern.file | FILE_LOG_PATTERN | 用在文件中的日志模式 (如果啟用LOG_FILE )。(只支持默許的Logback設(shè)置。) |
logging.pattern.level | LOG_LEVEL_PATTERN | 這類模式用來(lái)實(shí)行日志級(jí)別(默許%5p )。 (只支持默許的Logback設(shè)置。) |
PID | PID | 當(dāng)前的進(jìn)程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.
當(dāng)轉(zhuǎn)換配置文件時(shí),所有的日志系統(tǒng)都支持查詢系統(tǒng)屬性。例如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:-
.
如果你在日志屬性中想使用占位符,你應(yīng)當(dāng)試用Spring Boot的語(yǔ)法而不是底層框架的語(yǔ)法。特別是,如果你在使用Logback,你應(yīng)當(dāng)使用
:
作為屬性名和默許值之間的分隔符,而不是:-
。
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
你可以通太重寫(xiě)
LOG_LEVEL_PATTERN
(或Logback中的logging.pattern.level
)來(lái)添加MDC和其它的專門(mén)內(nèi)容來(lái)日志行中。例如,你可使用logging.pattern.level=user:%X{user} %5p
,默許的日志情勢(shì)將包括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的擴(kuò)大,這有助于進(jìn)行更高級(jí)的配置。你可以在你的logback-spring.xml
配置文件中使用這些擴(kuò)大。
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.
你不能在標(biāo)準(zhǔn)的
logback.xml
配置文件中使用擴(kuò)大,由于它加載的太早了。你需要使用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>
標(biāo)簽允許你自由的包括或排除基于激活的Spring profiles的配置的1部份。在<configuration>
元素的任何地方都支持Profile部份。使用name
屬性來(lái)指定哪個(gè)profile接受配置。多個(gè)profiles可以用1個(gè)逗號(hào)分隔的列表來(lái)指定。
<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>
標(biāo)簽允許你在Logback使用從Spring Environment
取得的屬性。如果你想在你的Logback配置中訪問(wèn)application.properties
文件中的屬性,這是非常有用的。這個(gè)標(biāo)簽與Logback的標(biāo)準(zhǔn)<property>
標(biāo)簽的作用方式類似,但不是為你指定的source
屬性(從Environment
中)指定1個(gè)直接的value
。如果你需要在某個(gè)地方而不是在local
作用域中存儲(chǔ)屬性,你可使用scope
屬性。如果你需要1個(gè)備用值以防屬性沒(méi)有在Environment
中設(shè)置,你可使用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
是用來(lái)訪問(wèn)Environment
屬性。如果在破折號(hào)符號(hào)中指定了source
,則會(huì)嘗試所有的不嚴(yán)格的變種(myPropertyName
,MY_PROPERTY_NAME
等)。