实时流式传输 Azure Spring Cloud 应用日志Stream Azure Spring Cloud app logs in real-time

Azure Spring Cloud 支持在 Azure CLI 中进行日志流式处理,以获取实时应用程序控制台日志,从而进行故障排除。Azure Spring Cloud enables log streaming in Azure CLI to get real-time application console logs for troubleshooting. 还可以通过诊断设置分析日志和指标You can also Analyze logs and metrics with diagnostics settings.

先决条件Prerequisites

备注

ASC CLI 扩展已从 0.2.0 版本更新为 0.2.1 版本。The ASC CLI extension is updated from version 0.2.0 to 0.2.1. 此更改影响日志流式处理命令的语法:az spring-cloud app log tail,将其替换为 az spring-cloud app logsThis change affects the syntax of the command for log streaming: az spring-cloud app log tail, which is replaced by: az spring-cloud app logs. az spring-cloud app log tail 命令将在未来版本中弃用。The command: az spring-cloud app log tail will be deprecated in a future release. 如果你一直使用 0.2.0 版本,可以升级到 0.2.1。If you have been using version 0.2.0, you can upgrade to 0.2.1. 首先,使用 az extension remove -n spring-cloud 命令删除旧版本。First, remove the old version with the command: az extension remove -n spring-cloud. 然后,使用 az extension add -n spring-cloud 命令安装 0.2.1 版本。Then, install 0.2.1 by the command: az extension add -n spring-cloud.

使用 CLI 跟踪日志Use CLI to tail logs

若要避免重复指定资源组和服务实例名称,请设置默认资源组名称和群集名称。To avoid repeatedly specifying your resource group and service instance name, set your default resource group name and cluster name.

az configure --defaults group=<service group name>
az configure --defaults spring-cloud=<service instance name>

在以下示例中,将在命令中省略资源组和服务名称。In following examples, the resource group and service name will be omitted in the commands.

跟踪具有单个实例的应用的日志Tail log for app with single instance

如果名为 auth-service 的应用只有一个实例,则可以使用以下命令查看该应用实例的日志:If an app named auth-service has only one instance, you can view the log of the app instance with the following command:

az spring-cloud app logs -n auth-service

这会返回以下日志:This will return logs:

...
2020-01-15 01:54:40.481  INFO [auth-service,,,] 1 --- [main] o.apache.catalina.core.StandardService  : Starting service [Tomcat]
2020-01-15 01:54:40.482  INFO [auth-service,,,] 1 --- [main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.22]
2020-01-15 01:54:40.760  INFO [auth-service,,,] 1 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/uaa]  : Initializing Spring embedded WebApplicationContext
2020-01-15 01:54:40.760  INFO [auth-service,,,] 1 --- [main] o.s.web.context.ContextLoader  : Root WebApplicationContext: initialization completed in 7203 ms

...

跟踪具有多个实例的应用的日志Tail log for app with multiple instances

如果名为 auth-service 的应用具有多个实例,则可以使用 -i/--instance 选项查看实例日志。If multiple instances exist for the app named auth-service, you can view the instance log by using the -i/--instance option.

首先,可以通过以下命令获取应用实例名称。First, you can get the app instance names with following command.

az spring-cloud app show -n auth-service --query properties.activeDeployment.properties.instances -o table

结果如下:With results:

Name                                         Status    DiscoveryStatus
-------------------------------------------  --------  -----------------
auth-service-default-12-75cc4577fc-pw7hb  Running   UP
auth-service-default-12-75cc4577fc-8nt4m  Running   UP
auth-service-default-12-75cc4577fc-n25mh  Running   UP

然后,可以使用 -i/--instance 选项来流式传输应用实例日志:Then, you can stream logs of an app instance with the option -i/--instance option:

az spring-cloud app logs -n auth-service -i auth-service-default-12-75cc4577fc-pw7hb

还可以从 Azure 门户获取应用实例的详细信息。You can also get details of app instances from the Azure portal. 在 Azure Spring Cloud 服务的左侧导航窗格中选择“应用”之后,选择“应用实例” 。After selecting Apps in the left navigation pane of your Azure Spring Cloud service, select App Instances.

连续流式传输新日志Continuously stream new logs

默认情况下,az spring-cloud ap log tail 仅打印流式传输到应用控制台的现有日志,然后退出。By default, az spring-cloud ap log tail prints only existing logs streamed to the app console and then exits. 如果要流式传输新日志,请添加 -f (--follow):If you want to stream new logs, add -f (--follow):

az spring-cloud app logs -n auth-service -f

查看支持的所有日志记录选项:To check all the logging options supported:

az spring-cloud app logs -h 

后续步骤Next steps