将Spring Boot 3的应用程序日志优化为适用于Google Cloud Logging
首先
目前,我正在使用Spring Boot 3和Google Cloud进行应用程序开发。

结构化日志
云日志服务能自动将包含特定字段的JSON日志进行映射和读取。
https://cloud.google.com/logging/docs/structured-logging?hl=ja#special-payload-fields
譬如,若在JSON日志的severity字段中包含日志的重要程度(如INFO、ERROR等),则能够根据该重要程度将日志分类并显示出来。
包含用于对同一请求的日志进行分组的trace字段等,还有其他一些后来会感到高兴的字段。
春云-谷歌云平台日志启动器
Google Cloud为Spring环境提供了多个模块。
https://spring.io/projects/spring-cloud-gcp
由于也有专为Cloud Logging设计的starter模块,在此我们将使用它来进行操作。
https://mvnrepository.com/artifact/com.google.cloud/spring-cloud-gcp-starter-logging
构建.gradle
添加依存。
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.google.cloud:spring-cloud-gcp-starter-logging:4.5.0' // 追加
}
logback-spring.xml 可以被重述为:spring框架的logback配置文件。
由于在 spring-cloud-gcp-starter-logging 中定义了 logback-json-appender.xml,因此会加载它。基本上只需要这个就可以了。
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="com/google/cloud/spring/logging/logback-json-appender.xml"/>
<root level="INFO">
<appender-ref ref="CONSOLE_JSON"/>
</root>
</configuration>
我只需要一种选项,将它放在resources文件夹中。
演示程序
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class DemoApplication {
@Autowired
private DemoService demoService;
@GetMapping("/")
public void demo() {
demoService.exec();
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
package com.example.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service
public class DemoService {
private static final Logger LOGGER = LoggerFactory.getLogger(DemoService.class);
public void exec() {
LOGGER.info("This is INFO.");
LOGGER.warn("This is WARNING.");
LOGGER.error("This is ERROR.");
}
}
部署并检查日志。

我想在启动时也隐藏掉标志。
只需在application.properties(或yaml)中写入以下内容即可。
spring.main.banner-mode=off
包括追踪

当使用Async时的trace
通常情况下,使用Spring Boot的Async功能时,会丢失trace信息。为了解决这个问题,我们可以使用micrometer-tracing-bridge-brave,将AsyncExecutor进行包装。
dependencies {
implementation 'io.micrometer:micrometer-tracing-bridge-brave:1.1.2' // 追加
}
package com.example.demo;
import io.micrometer.context.ContextExecutorService;
import io.micrometer.context.ContextSnapshotFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
@EnableAsync
@Configuration(proxyBeanMethods = false)
public class AsyncTraceContextConfig implements AsyncConfigurer {
private final ThreadPoolTaskExecutor taskExecutor;
public AsyncTraceContextConfig(ThreadPoolTaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
@Override
public Executor getAsyncExecutor() {
return ContextExecutorService.wrap(taskExecutor.getThreadPoolExecutor(),
() -> ContextSnapshotFactory.builder().build().captureAll());
}
}
其他
之前
-
- logback-jackson (https://mvnrepository.com/artifact/ch.qos.logback.contrib/logback-jackson)
- logstash-logback-encoder (https://github.com/logfellow/logstash-logback-encoder)
我曾经使用过这些工具,但是对于Google Cloud x Spring Boot,只需要使用spring-cloud-gcp-starter-logging,就可以轻松整理日志,而无需复杂的配置,我觉得很好。