Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1640a89

Browse files
committed
Add Logging and JSON section
1 parent ccc4089 commit 1640a89

File tree

2 files changed

+317
-1
lines changed

2 files changed

+317
-1
lines changed

SUMMARY.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@
116116
- [25.1、添加激活 Profile](pages/spring-boot-features.md#boot-features-adding-active-profiles)
117117
- [25.2、以编程方式设置 Profile](pages/spring-boot-features.md#boot-features-programmatically-setting-profiles)
118118
- [25.3、特定 Profile 的配置文件](pages/spring-boot-features.md#boot-features-profile-specific-configuration)
119+
- [26、日志记录](pages/spring-boot-features.md#boot-features-logging)
120+
- [26.1、日志格式](pages/spring-boot-features.md#boot-features-logging-format)
121+
- [26.2、控制台输出](pages/spring-boot-features.md#boot-features-logging-console-output)
122+
- [26.2.1、着色输出](pages/spring-boot-features.md#boot-features-logging-color-coded-output)
123+
- [26.3、文件输出](pages/spring-boot-features.md#boot-features-logging-file-output)
124+
- [26.4、日志等级](pages/spring-boot-features.md#boot-features-custom-log-levels)
125+
- [26.5、日志组](pages/spring-boot-features.md#boot-features-custom-log-groups)
126+
- [26.6、自定义日志配置](pages/spring-boot-features.md#boot-features-custom-log-configuration)
127+
- [26.7、Logback 扩展](pages/spring-boot-features.md#boot-features-logback-extensions)
128+
- [26.7.1、特定 Profile 配置](pages/spring-boot-features.md#_profile_specific_configuration)
129+
- [26.7.2、环境属性](pages/spring-boot-features.md#_environment_properties)
130+
- [27、JSON](pages/spring-boot-features.md#boot-features-json)
131+
- [27.1、Jackson](pages/spring-boot-features.md#boot-features-json-jackson)
132+
- [27.2、Gson](pages/spring-boot-features.md#boot-features-json-gson)
133+
- [27.3、JSON-B](pages/spring-boot-features.md#boot-features-json-json-b)
119134
- [28、开发 Web 应用程序](pages/spring-boot-features.md#boot-features-developing-web-applications)
120135
- [28.1、Spring Web MVC 框架](pages/spring-boot-features.md#boot-features-spring-mvc)
121136
- [28.1.1、Spring MVC 自动配置](pages/spring-boot-features.md#boot-features-spring-mvc-auto-configuration)

pages/spring-boot-features.md

Lines changed: 302 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1214,23 +1214,324 @@ spring.profiles.include:
12141214

12151215
特定 profile 的 `application.properties`(或 `application.yml`)和通过 `@ConfigurationProperties` 引用的文件被当做文件并加载。有关详细信息,请参见[第 24.4 章节:特定 Profile 的属性文件](#boot-features-external-config-profile-specific-properties)。
12161216

1217-
**待续……**
1217+
<a id="boot-features-logging"></a>
12181218

1219+
## 26、日志记录
12191220

1221+
Spring Boot 使用 [Commons Logging](https://commons.apache.org/logging) 记录所有内部日志,但开放日志的底层实现。其为 [Java Util Logging
1222+
](https://docs.oracle.com/javase/8/docs/api//java/util/logging/package-summary.html)、[Log4J2](https://logging.apache.org/log4j/2.x/) 和 [Logback](http://logback.qos.ch/) 提供了默认配置。在每种情况下,日志记录器都预先配置为使用控制台输出,并且还提供可选的文件输出。
12201223

1224+
默认情况下,如果您使用了 **Starter**,则使用 Logback 进行日志记录。还包括合适的 Logback 路由,以确保在使用 Java Util Logging、Commons Logging、Log4J 或 SLF4J 的依赖库都能正常工作。
12211225

1226+
**提示**
1227+
1228+
> Java 有很多日志框架可供使用。如果以上列表让您感到困惑,请不要担心。通常,您不需要更改日志依赖,并且 Spring Boot 提供的默认配置可以保证日志正常工作。
1229+
1230+
<a id="boot-features-logging-format"></a>
1231+
1232+
### 26.1、日志格式
1233+
1234+
Spring Boot 默认日志输出类似于以下示例:
1235+
1236+
```console
1237+
2014-03-05 10:57:51.112 INFO 45469 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.52
1238+
2014-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
1239+
2014-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1358 ms
1240+
2014-03-05 10:57:51.698 INFO 45469 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
1241+
2014-03-05 10:57:51.702 INFO 45469 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
1242+
```
1243+
1244+
输出以下项:
1245+
1246+
- 日期和时间:毫秒精度,易于排序。
1247+
- 日志级别:`ERROR`、`WARN`、`INFO`、`DEBUG` 或 `TRACE`。
1248+
- 进程 ID。
1249+
- 一个 `---` 分隔符,用于区分实际日志内容的开始。
1250+
- 线程名称:在方括号中(可能会截断控制台输出)。
1251+
- 日志记录器名称:这通常是源类名称(通常为缩写)。
1252+
- 日志内容。
1253+
1254+
**注意**
1255+
1256+
> Logback 没有 `FATAL` 级别。该级别映射到 `ERROR`。
1257+
1258+
<a id="boot-features-logging-console-output"></a>
1259+
1260+
### 26.2、控制台输出
1261+
1262+
默认日志配置会在写入时将消息回显到控制台。默认情况下,会记录 `ERROR`、`WARN` 和 `INFO` 级别的日志。您还可以通过使用 `--debug` 标志启动应用程序来启用**调试**模式。
1263+
1264+
```console
1265+
$ java -jar myapp.jar --debug
1266+
```
1267+
1268+
**注意**
1269+
1270+
> 您还可以在 `application.properties` 中指定 `debug=true`。
1271+
1272+
启用调试模式后,核心日志记录器(内嵌容器、Hibernate 和 Spring Boot)将被配置为输出更多日志信息。启用调试模式不会将应用程序配置为使用 `DEBUG` 级别记录所有日志内容。
1273+
1274+
或者,您可以通过使用 `--trace` 标志(或在 `application.properties` 中的设置 `trace=true`)启动应用程序来启用**跟踪**模式。这样做可以为选择的核心日志记录器(内嵌容器、Hibernate 模式生成和整个 Spring 组合)启用日志追踪。
1275+
1276+
<a id="boot-features-logging-color-coded-output"></a>
1277+
1278+
### 26.2.1、着色输出
1279+
1280+
如果您的终端支持 ANSI,则可以使用颜色输出来提高可读性。您可以将 `spring.output.ansi.enabled` 设置为[受支持的值](https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/api/org/springframework/boot/ansi/AnsiOutput.Enabled.html)以覆盖自动检测。
1281+
1282+
可使用 `%clr` 转换字配置颜色编码。最简单形式是,转换器根据日志级别对输出进行着色,如下所示:
1283+
1284+
```
1285+
%clr(%5p)
1286+
```
1287+
1288+
下表描述日志级别与颜色的映射关系:
1289+
1290+
| 级别 | 颜色 |
1291+
| --- | --- |
1292+
| `FATAL` | 红(Red) |
1293+
| `ERROR` | 红(Red) |
1294+
| `WARN` | 黄(Yellow) |
1295+
| `INFO` | 绿(Green) |
1296+
| `DEBUG` | 绿(Green) |
1297+
| `TRACE` | 绿(Green) |
1298+
1299+
或者,您可以通过将其作为转换选项指定应使用的颜色或样式。例如,要将文本变为黄色,请使用以下设置:
1300+
1301+
```
1302+
%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){yellow}
1303+
```
1304+
1305+
支持以下颜色和样式:
1306+
1307+
- `blue`
1308+
- `cyan`
1309+
- `faint`
1310+
- `green`
1311+
- `magenta`
1312+
- `red`
1313+
- `yellow`
1314+
1315+
<a id="boot-features-logging-file-output"></a>
1316+
1317+
### 26.3、文件输出
1318+
1319+
默认情况下,Spring Boot 仅记录到控制台,不会写入日志文件。想除了控制台输出之外还要写入日志文件,则需要设置 `logging.file` 或 `logging.path` 属性(例如,在 `application.properties` 中)。
1320+
1321+
下表展示了如何与 `logging.*` 属性一起使用:
1322+
1323+
**表 26.1、Logging 属性**
1324+
1325+
| `logging.file` | `logging.path` | 示例 | 描述 |
1326+
| --- | --- | --- | --- |
1327+
| (无) | (无) | | 仅在控制台输出 |
1328+
| 指定文件 | (无) | `my.log` | 写入指定的日志文件。名称可以是绝对位置或相对于当前目录。 |
1329+
| (无) | 指定目录 | `/var/log` | 将 `spring.log` 写入指定的目录。名称可以是绝对位置或相对于当前目录。 |
1330+
1331+
日志文件在达到 10MB 时会轮转,并且与控制台输出一样,默认情况下会记录 `ERROR`、`WARN` 和 `INFO` 级别的内容。可以使用 `logging.file.max-size` 属性更改大小限制。除非已设置 `logging.file.max-history` 属性,否则以前轮转的文件将无限期归档。
1332+
1333+
**注意**
1334+
1335+
> 日志记录系统在应用程序生命周期的早期开始初始化。因此,通过 `@PropertySource` 注解加载的属性文件中是找不到日志属性的。
1336+
1337+
**提示**
1338+
1339+
> 日志属性独立于实际的日志底层。因此,spring Boot 不管理特定的配置 key(例如 Logback 的 `logback.configurationFile`)。
1340+
1341+
1342+
<a id="boot-features-custom-log-levels"></a>
1343+
1344+
### 26.4、日志等级
1345+
1346+
所有受支持的日志记录系统都可以使用 `logging.level.<logger-name>=<level>` 来设置 Spring `Environment` 中的记录器等级(例如,在 `application.properties` 中)。其中 `level` 是 TRACE、DEBUG、INFO、WARN、ERROR、FATAL 和 OFF 其中之一。可以使用 `logging.level.root` 配置 `root` 记录器。
1347+
1348+
以下示例展示了 `application.properties` 中默认的日志记录设置:
1349+
1350+
```ini
1351+
logging.level.root=WARN
1352+
logging.level.org.springframework.web=DEBUG
1353+
logging.level.org.hibernate=ERROR
1354+
```
1355+
1356+
<a id="boot-features-custom-log-groups"></a>
1357+
1358+
### 26.5、日志组
1359+
1360+
将相关记录器组合在一起以便可以同时配置,这通常很有用。例如,您可以更改**所有** Tomcat 相关记录器的日志记录级别,但您无法轻松记住顶层的包名。
1361+
1362+
为了解决这个问题,Spring Boot 允许您在 Spring `Environment` 中定义日志记录组。例如,以下通过将 **tomcat** 组添加到 `application.properties` 来定义 **tomcat** 组:
1363+
1364+
```ini
1365+
logging.group.tomcat=org.apache.catalina, org.apache.coyote, org.apache.tomcat
1366+
```
1367+
1368+
定义后,您可以使用一行配置来更改组中所有记录器的级别:
1369+
1370+
```ini
1371+
logging.level.tomcat=TRACE
1372+
```
1373+
1374+
Spring Boot 包含以下预定义的日志记录组,可以直接使用:
1375+
1376+
| 名称 | 日志记录器 |
1377+
| --- | --- |
1378+
| web | `org.springframework.core.codec``org.springframework.http``org.springframework.web` |
1379+
| sql | `org.springframework.jdbc.core``org.hibernate.SQL` |
1380+
1381+
1382+
<a id="boot-features-custom-log-configuration"></a>
1383+
1384+
### 26.6、自定义日志配置
1385+
1386+
可以通过在 classpath 中引入适合的库来激活各种日志记录系统,并且可以通过在 classpath 的根目录中或在以下 Spring `Environment` 属性指定的位置提供合适的配置文件来进一步自定义:`logging.config`
1387+
1388+
您可以使用 `org.springframework.boot.logging.LoggingSystem` 系统属性强制 Spring Boot 使用特定的日志记录系统。该值应该是一个实现了 `LoggingSystem` 的类的完全限定类名。您还可以使用 `none` 值完全禁用 Spring Boot 的日志记录配置。
1389+
1390+
**注意**
1391+
1392+
> 由于日志记录在创建 `ApplicationContext` 之前初始化,因此无法在 Spring `@Configuration` 文件中控制来自 `@PropertySources` 的日志记录。更改日志记录系统或完全禁用它的唯一方法是通过系统属性设置。
1393+
1394+
根据您的日志记录系统,将加载以下文件:
1395+
1396+
| 日志记录系统 | 文件 |
1397+
| --- | --- |
1398+
| Logback | `logback-spring.xml``logback-spring.groovy``logback.xml` 或者 `logback.groovy` |
1399+
| Log4j2 | `log4j2-spring.xml` 或者 `log4j2.xml` |
1400+
| JDK(Java Util Logging) | `logging.properties` |
1401+
1402+
**注意**
1403+
1404+
> 如果可能,我们建议您使用 `-spring` 的形式来配置日志记录(比如 `logback-spring.xml` 而不是 `logback.xml`)。如果使用标准的配置位置,Spring 无法完全控制日志初始化。
1405+
1406+
**警告**
1407+
1408+
> Java Util Logging 存在已知的类加载问题,这些问题在以**可执行 jar** 运行时会触发。如果可能的话,我们建议您在使用**可执行 jar** 方式运行时避免使用它。
1409+
1410+
为了进行自定义,部分其他属性会从 Spring `Environment` 传输到 System 属性,如下表所述:
1411+
1412+
| Spring Environment | 系统属性 | 说明 |
1413+
| --- | --- | --- |
1414+
| `logging.exception-conversion-word` | `LOG_EXCEPTION_CONVERSION_WORD` | 记录异常时使用的转换字。 |
1415+
| `logging.file` | `LOG_FILE` | 如果已定义,则在默认日志配置中使用它。 |
1416+
| `logging.file.max-size` | `LOG_FILE_MAX_SIZE` | 最大日志文件大小(如果启用了 LOG_FILE)。(仅支持默认的 Logback 设置。) |
1417+
|`logging.file.max-history` | `LOG_FILE_MAX_HISTORY` | 要保留的归档日志文件最大数量(如果启用了 LOG_FILE)。(仅支持默认的 Logback 设置。) |
1418+
| `logging.path` | `LOG_PATH` | 如果已定义,则在默认日志配置中使用它。 |
1419+
| `logging.pattern.console` | `CONSOLE_LOG_PATTERN` | 要在控制台上使用的日志模式(stdout)。(仅支持默认的 Logback 设置。) |
1420+
| `logging.pattern.dateformat` | `LOG_DATEFORMAT_PATTERN` | 日志日期格式的 Appender 模式。(仅支持默认的 Logback 设置。) |
1421+
| `logging.pattern.file` | `FILE_LOG_PATTERN` | 要在文件中使用的日志模式(如果启用了 `LOG_FILE`)。(仅支持默认的 Logback 设置。) |
1422+
| `logging.pattern.level` | `LOG_LEVEL_PATTERN` | 渲染日志级别时使用的格式(默认值为 `%5p`)。(仅支持默认的 Logback 设置。) |
1423+
| `PID` | `PID` | 当前进程 ID(如果可能,则在未定义为 OS 环境变量时发现)。 |
1424+
1425+
所有受支持的日志记录系统在解析其配置文件时都可以参考系统属性。有关示例,请参阅 `spring-boot.jar` 中的默认配置:
1426+
1427+
- [Logback](https://github.com/spring-projects/spring-boot/tree/v2.1.1.RELEASE/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml)
1428+
- [Log4j 2](https://github.com/spring-projects/spring-boot/tree/v2.1.1.RELEASE/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2.xml)
1429+
- [Java Util logging](https://github.com/spring-projects/spring-boot/tree/v2.1.1.RELEASE/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/java/logging-file.properties)
1430+
1431+
**提示**
1432+
1433+
> 如果要在日志记录属性中使用占位符,则应使用 [Spring Boot 的语法](#boot-features-external-config-placeholders-in-properties),而不是使用底层框架的语法。值得注意的是,如果使用 Logback,则应使用 `:` 作为属性名称与其默认值之间的分隔符,而不是使用 `:-`
1434+
1435+
**提示**
1436+
1437+
<blockquote>
1438+
1439+
您可以通过仅覆盖 `LOG_LEVEL_PATTERN`(或带 Logback 的 `logging.pattern.level`)将 MDC 和其他特别的内容添加到日志行。例如,如果使用 `logging.pattern.level=user:%X{user} %5p`,则默认日志格式包含 **user** MDC 项(如果存在),如下所示:
1440+
1441+
```console
1442+
2015-09-30 12:30:04.031 user:someone INFO 22174 --- [ nio-8080-exec-0] demo.Controller
1443+
Handling authenticated request
1444+
```
1445+
1446+
</blockquote>
1447+
1448+
<a id="boot-features-logback-extensions"></a>
1449+
1450+
### 26.7、Logback 扩展
1451+
1452+
Spring Boot 包含许多 Logback 扩展,可用于进行高级配置。您可以在 `logback-spring.xml` 配置文件中使用这些扩展。
1453+
1454+
**注意**
1455+
1456+
> 由于标准的 logback.xml 配置文件加载过早,因此无法在其中使用扩展。您需要使用 `logback-spring.xml` 或定义 `logging.config` 属性。
1457+
1458+
**警告**
1459+
1460+
> 扩展不能与 Logback 的[配置扫描](http://logback.qos.ch/manual/configuration.html#autoScan)一起使用。如果尝试这样做,更改配置文件会导致发生类似以下错误日志:
1461+
1462+
```console
1463+
ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:71 - no applicable action for [springProperty], current ElementPath is [[configuration][springProperty]]
1464+
ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:71 - no applicable action for [springProfile], current ElementPath is [[configuration][springProfile]]
1465+
```
1466+
1467+
<a id="_profile_specific_configuration"></a>
1468+
1469+
### 26.7.1、特定 Profile 配置
1470+
1471+
`<springProfile>` 标签允许您根据激活的 Spring profile 选择性地包含或排除配置部分。在 `<configuration>` 元素中的任何位置都支持配置 profile。使用 `name` 属性指定哪个 proifle 接受配置。`<springProfile>` 标记可以包含简单的 proifle 名称(例如 `staging`)或 profile 表达式。profile 表达式允许表达更复杂的 profile 逻辑,例如 `production & (eu-central | eu-west)`。有关详细信息,请查阅[参考指南](https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#beans-definition-profiles-java)。以下清单展示了三个示例 profile:
1472+
1473+
```xml
1474+
<springProfile name="staging">
1475+
<!-- configuration to be enabled when the "staging" profile is active -->
1476+
</springProfile>
1477+
1478+
<springProfile name="dev | staging">
1479+
<!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
1480+
</springProfile>
1481+
1482+
<springProfile name="!production">
1483+
<!-- configuration to be enabled when the "production" profile is not active -->
1484+
</springProfile>
1485+
```
1486+
1487+
<a id="_environment_properties"></a>
1488+
1489+
### 26.7.2、环境属性
1490+
1491+
使用 `<springProperty>` 标记可以让您暴露 Spring 环境(`Environment`)中的属性,以便在 Logback 中使用。如果在 Logback 配置中访问来自 `application.properties` 文件的值,这样做很有用。标签的工作方式与 Logback 的标准 `<property>` 标签类似。但是,您可以指定属性(来自 `Environment`)的 `source`,而不是指定直接的 `value`。如果需要将属性存储在 `local` 范围以外的其他位置,则可以使用 `scope` 属性。如果需要回退值(如果未在 `Environment` 中设置该属性),则可以使用 `defaultValue` 属性。以下示例展示了如何暴露属性以便在 Logback 中使用:
1492+
1493+
```xml
1494+
<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host"
1495+
defaultValue="localhost"/>
1496+
<appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender">
1497+
<remoteHost>${fluentHost}</remoteHost>
1498+
...
1499+
</appender>
1500+
```
1501+
1502+
**注意**
1503+
1504+
> 必须以 kebab 风格(短横线小写风格)指定 `source`(例如 `my.property-name`)。但可以使用宽松规则将属性添加到 `Environment` 中。
1505+
1506+
<a id="boot-features-json"></a>
1507+
1508+
## 27、JSON
12221509

1510+
Spring Boot 为三个 JSON 映射库提供了内置集成:
12231511

1512+
- GSON
1513+
- Jackson
1514+
- JSON-B
12241515

1516+
Jackson 是首选和默认的库。
12251517

1518+
<a id="boot-features-json-jackson"></a>
12261519

1520+
## 27.1、Jackson
12271521

1522+
Spring Boot 提供了 Jackson 的自动配置,Jackson 是 `spring-boot-starter-json` 的一部分。当 Jackson 在 classpath 上时,会自动配置 `ObjectMapper` bean。Spring Boot 提供了几个配置属性来[自定义 `ObjectMapper` 的配置](howto.md#howto-customize-the-jackson-objectmapper)
12281523

1524+
<a id="boot-features-json-gson"></a>
12291525

1526+
## 27.2、Gson
12301527

1528+
Spring Boot 提供 Gson 的自动配置。当 Gson 在 classpath 上时,会自动配置 `Gson` bean。Spring Boot 提供了几个 `spring.gson.*` 配置属性来自定义配置。为了获得更多控制,可以使用一个或多个 `GsonBuilderCustomizer` bean。
12311529

1530+
<a id="boot-features-json-json-b"></a>
12321531

1532+
## 27.3、JSON-B
12331533

1534+
Spring Boot 提供了 JSON-B 的自动配置。当 JSON-B API 和实现在 classpath 上时,将自动配置 `Jsonb` bean。首选的 JSON-B 实现是 Apache Johnzon,它提供了依赖管理。
12341535

12351536
<a id="boot-features-developing-web-applications"></a>
12361537

0 commit comments

Comments
 (0)