gpt4 book ai didi

java - 如果我们在 Google Cloud Platform 中部署了 Spring Boot 应用程序,如何查看日志?

转载 作者:行者123 更新时间:2023-11-28 22:39:23 25 4
gpt4 key购买 nike

我们开发了一个 Spring Boot 应用程序并部署在谷歌云平台 (GCP) 中。它是 Compute Engine,我们使用 Ubuntu 16.04.5 LTS 作为操作系统,使用 Apache Tomcat 8.5.3 作为 Web 服务器。

在此应用程序中,我们使用了 System.out.println() 语句,有时我们也会抛出异常。

Now I want to see the logs which are generated by either System.out.println() or through the Exceptions but how to see the console in Google Cloud Platform?

最佳答案

您无法通过 System.out.printlnExceptions 直接查看 GCP 中的日志

有两种方法可以实现:

可能但不推荐:使用 filewriter 将日志写入文件,访问文件并读取它

推荐:将记录器添加到您的应用程序中。为此,包括 apache commons 登录到您的应用程序的 pom.xml

<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>

按要求写日志:

@RestController    
public class ExampleController {
private static final Log LOGGER = LogFactory.getLog(ExampleController.class);

@RequestMapping("/<custom-url>")
public String function() {
String message = "Example message written to the log";
String secondMessage = "Second example written to the log";
LOGGER.info(message);
LOGGER.info(secondMessage);
return message;
}
}

您可以将 Logger 用于任意多个功能和任意多个 Controller ,它将添加到同一个 Logger 并协同显示。

现在您将能够在 GCP 的项目日志选项卡中看到日志:)

附言这会将日志归档为信息。您可以使用错误或其他可用的分类选项来对您的日志消息类型进行分类。为此,将信息替换为以下任何选项以进行分类:

Log classifications

任何日志级别都会显示所有类型的日志。

关于java - 如果我们在 Google Cloud Platform 中部署了 Spring Boot 应用程序,如何查看日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53398359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com