练习 - 日志记录
在此练习中,我们将了解用于查看示例应用程序日志的练习。
流式处理日志练习
跟踪日志
在现有的 Azure Cloud Shell 窗口中,通过以下命令查看应用程序实例的日志:
az spring app logs --name customers-service
重要
如果关闭了原始 Azure Cloud Shell 窗口,或者想要在本地开发环境中运行命令,则必须指定资源组和 Azure Spring Apps 服务名称:
az spring app logs --name customers-service --resource-group <resource group name> --service <service >instance name>
此命令将返回如下所示的日志:
...
2021-04-23 09:13:59.516 INFO [customers-service,,,] 1 --- [ main] o.s.s.p.c.CustomersServiceApplication : Started CustomersServiceApplication in 48.032 seconds (JVM running for 79.933)
2021-04-23 09:14:05.385 INFO [customers-service,,,] 1 --- [nio-1025-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-04-23 09:14:05.385 INFO [customers-service,,,] 1 --- [nio-1025-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-04-23 09:14:05.410 INFO [customers-service,,,] 1 --- [nio-1025-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 25 ms
...
连续流式传输新日志
默认情况下,az spring app logs
仅打印流式传输到应用控制台的现有日志,然后退出。 如果要流式传输新日志,请添加 -f
(--follow
):
az spring app logs --name customers-service -f
错误和警告练习
接下来,我们将创建一个错误,以显示能够快速调试并查找其根本原因的价值。
打开
customers-service
并跟踪日志:az spring app logs --name customers-service -f
接下来,在 Web 浏览器中打开一个新窗口,并使用以下命令触发警告(请将“<spring-apps-name>”替换为你的 Spring Apps 实例的名称):
https://<spring-apps-name>-api-gateway.azuremicroservices.io/api/customer/owners/George
访问前面的 URL 会触发警告,因为变量“George”无法强制转换为用于搜索客户的编号。 你应该会收到类似于以下内容的警告:
若要解决此警告,请在 Web 浏览器中转到以下 URL,以使用客户 ID 搜索客户(请记住将“<spring-apps-name>”替换为你的 Spring Apps 实例的名称):
https://<spring-apps-name>-api-gateway.azuremicroservices.io/api/customer/owners/1