Spring Boot日志记录和Google Cloud Platform日志查看器 [英] Spring Boot Logging and Google Cloud Platform Log Viewer

查看:144
本文介绍了Spring Boot日志记录和Google Cloud Platform日志查看器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

每个请求都将被分组,并且可以通过扩展该行来查看该请求的所有日志记录信息.但是,当使用Spring Boot时,请求不再分组,并且日志条目仅逐行显示.当有多个请求时,日志条目将变得非常混乱,因为无法以分组方式查看它们.我以相同的方式设置了logging.properties:

  .level = INFOhandlers = java.util.logging.ConsoleHandlerjava.util.logging.ConsoleHandler.level =最终java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatterjava.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 运行时来重现此"嵌套日志功能",但我们需要自己进行管理.

作为

I'm running a Spring Boot application within the Google Cloud Platform and viewing the log files viewing the Google Platform Logs Viewer. Before using Spring Boot and just using simple servlets, the logging entries would be displayed as:

Each request would be grouped and all the logging information for that request could be seen by expanding the row. However, when using Spring Boot the requests are no longer grouped and the log entries are just shown line by line. When there are multiple requests the log entries get very confusing as a result because it isn't possible to view them in a grouped way. I have my logging.properties setup in the same way:

.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

The Logger is initialised in each class as:

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

And then the logging API is used as: LOG.info("My Message");

I don't understand why the statements are being logged differently and no longer grouped but it must have something with the way Spring Boot handles logging?

解决方案

Since recent runtimes, AppEngine is evolving with a behaviour that is more and more converging with a container based approach, more "opened" as new other products (like Cloud Run for example).

This is changing a little the way we're developing with GAE, specific legacy libraries aren't available (SearchAPI...), and it is changing also how logs are managed.

We can reproduce this "nested log feature" with new java11 runtime, but we need to manage it ourself.

As official docs mentioned:

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

It means, if we retrieve the trace identifier received inside X-Cloud-Trace-Context HTTP header of our request, we can then use it to add a new LogEntry by passing it as the trace identifier attribute.

This can be done by using Stackdriver Logging Client libraries

With Spring GCP

Fortunately, Spring Cloud GCP is there to make our lives easier.

You can find a sample project which implements it. Be careful, it's a AppEngine Flexible example, but it will work fine with Standard runtime. It uses Logback.

From a working Spring Boot project on GAE Java11, steps to follow are :

  • Add spring-cloud-gcp-starter-logging dependency :

<!-- Starter for Stackriver Logging -->   
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-gcp-starter-logging</artifactId>
   <version>1.2.1.RELEASE</version>
</dependency>

  • Add a logback-spring.xml inside src/main/resources folder :

<?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>

  • Enable Spring GCP logging feature, inside src/main/resources/application.properties :

spring.cloud.gcp.logging.enabled=true

  • And use LOGGER inside your code:

@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 !");
    }
}

Result will be in Stackdriver Logging viewer, for appengine.googleapis.com/request_log :

这篇关于Spring Boot日志记录和Google Cloud Platform日志查看器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆