繁体   English   中英

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

[英]How to see the logs if we have deployed our Spring Boot Application in Google Cloud Platform?

我们已经开发了一个Spring Boot应用程序,并已部署在Google Cloud Platform(GCP)中。 它是Compute Engine,我们将Ubuntu 16.04.5 LTS作为操作系统,并将Apache Tomcat 8.5.3作为Web服务器。

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

现在,我想查看由System.out.println()或通过“ Exceptions生成的日志,但是如何查看Google Cloud Platform的控制台?

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

有两种方法可以实现此目的:

可能但不建议使用:使用文件编写器将日志写入文件,访问文件并读取文件

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

<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用于尽可能多的功能和任意数量的控制器,它将被添加到同一Logger中并以协作方式显示。

现在,您将可以在GCP项目的“日志”标签中查看日志:)

PS这会将文件记录为信息。 您可以使用错误或其他可用的分类选项对日志消息类型进行分类。 为此,请将info替换为以下任何分类选项:

日志分类

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM