google-cloud-platform - Spring Boot 日志记录和 Google Cloud Platform 日志查看器

标签 google-cloud-platform stackdriver java.util.logging

我正在 Google Cloud Platform 中运行 Spring Boot 应用程序,并通过 Google Platform Logs Viewer 查看日志文件。在使用 Spring Boot 并仅使用简单的 servlet 之前,日志记录条目将显示为:

Logs are grouped and show the blue information icon

每个请求都会被分组,并且可以通过展开行来查看该请求的所有日志记录信息。然而,当使用 Spring Boot 时,请求不再分组,日志条目只是逐行显示。当存在多个请求时,日志条目会变得非常困惑,因为无法以分组方式查看它们。我以同样的方式设置了logging.properties:

.level = INFO
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=FINEST
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format = [%1$tc] %4$s: %2$s - %5$s %6$s%n

记录器在每个类中初始化为:

private static final java.util.logging.Logger LOG = java.util.logging.Logger.getLogger(MyClass.class.getName());

然后日志记录 API 用作: LOG.info("我的消息");

我不明白为什么这些语句以不同的方式记录并且不再分组,但它必须与 Spring Boot 处理日志记录的方式有某种关系?

最佳答案

自最近的运行时以来,AppEngine 的行为越来越与基于容器的方法融合,与其他新产品(例如 Cloud Run)一样更加“开放”。

这稍微改变了我们使用 GAE 进行开发的方式,特定的遗留库不可用(SearchAPI ...),并且它还改变了日志的管理方式。

我们可以使用新的 java11 运行时重现此“嵌套日志功能”,但我们需要自己管理它。

official docs提到:

In the Logs Viewer, log entries correlated by the same trace can be viewed in a "parent-child" format.

这意味着,如果我们检索请求的 X-Cloud-Trace-Context HTTP header 中收到的 trace 标识符,我们就可以使用它来添加新的LogEntry 将其作为 trace 标识符属性传递。

这可以通过使用 Stackdriver Logging Client libraries 来完成

使用 Spring GCP

幸运的是,Spring Cloud GCP是为了让我们的生活更轻松。

您可以找到sample project它实现了它。请注意,这是一个 AppEngineFlexible 示例,但它可以在 Standard 运行时正常工作。 它使用 Logback .

GAE Java11 上运行的 Spring Boot 项目,要遵循的步骤是:

  • 添加 spring-cloud-gcp-starter-logging 依赖项:
<!-- Starter for Stackriver Logging -->   
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-gcp-starter-logging</artifactId>
   <version>1.2.1.RELEASE</version>
</dependency>

  • src/main/resources文件夹中添加logback-spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/cloud/gcp/autoconfigure/logging/logback-appender.xml" />
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />

    <root level="INFO">
        <!-- If running in GCP, remove the CONSOLE appender otherwise logs will be duplicated. -->
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="STACKDRIVER" />
    </root>
</configuration>
  • src/main/resources/application.properties 内启用 Spring GCP 日志记录功能:
spring.cloud.gcp.logging.enabled=true
  • 并在代码中使用 LOGGER:
@SpringBootApplication
@RestController
public class DemoApplication {
    private static final Log LOGGER = LogFactory.getLog(DemoApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @GetMapping()
    public SomeData get() {
        LOGGER.info("My info message");
        LOGGER.warn("My warning message");
        LOGGER.error("My error message");

        return new SomeData("Hello from Spring boot !");
    }
}

结果将显示在 appengine.googleapis.com/request_logStackdriver Logging 查看器中:

Stackdriver Logging Viewer

关于google-cloud-platform - Spring Boot 日志记录和 Google Cloud Platform 日志查看器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60115784/

相关文章:

通过多个类进行 Java 日志记录

mysql - GTID 一致性 : CREATE TEMPORARY TABLE and DROP TEMPORARY TABLE can only be executed outside transactional context

group-by - Big Query 按分组字段排序

google-app-engine - 如何在 Java 8 模式下运行 IntelliJ 的 Google App Engine Dev Server 插件

stackdriver - 如何通过 Python 中的 Stackdriver Logging 将错误记录到 Stackdriver Error Reporting

java - 谷歌应用引擎。堆栈驱动程序。使用 Java 进行日志记录

Tomcat 控制台日志从我的应用程序获取输出

java.util.logging FileHandler.count 不工作

google-cloud-platform - 基于 TaggedOutput 数量动态 fork Beam(数据流)管道

python-3.x - stackdriver 日志记录客户端库缺少 python 的严重性