You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python获取Azure容器应用作业的执行控制台日志

无延迟获取Azure容器应用作业控制台日志的有效方法求助

我通过Python SDK部署ContainerJob,完成begin_create_or_update操作后启动作业,但无法找到无延迟获取已执行作业控制台日志的简便方法,目前遇到以下问题:

  • AppContainersManagementClient不适用于当前场景,因为不存在容器实例组;
  • 执行AZ CLI命令 az containerapp job logs show -n my-job -g my-ressource-groupe --container my-job 时,提示No replicas found for execution;
  • 仅能通过LogsQueryClient获取日志,但存在5-10分钟的延迟。

部署及启动作业代码

from azure.identity import DefaultAzureCredential
from azure.mgmt.appcontainers import ContainerAppsAPIClient


client = ContainerAppsAPIClient(credential=DefaultAzureCredential(), subscription_id="xxxxxxxxxxxxxxxx")

job_parameters = {
    "location": "West Europe",
    "properties": {
        "environmentId": "/subscriptions/xxxxx/resourceGroups/xxxxxxxx/providers/Microsoft.App/managedEnvironments/xxxxxxxx",
        "workloadProfileName": "Consumption",
        "configuration": {
            "secrets": [
                {
                    "name": "xxxx",
                    "value": "xxxx"
                }
            ],
            "triggerType": "Manual",
            "replicaTimeout": 1800,
            "replicaRetryLimit": 0,
            "manualTriggerConfig": {
                "replicaCompletionCount": 1,
                "parallelism": 1
            },
            "registries": [
                {
                    "server": "artifacts.xxx.com",
                    "username": "xxxxx",
                    "passwordSecretRef": "xxxxx",
                },
            ]
        },
        "template": {
            "containers": [
                {
                    "image": "artifacts.xxx.com/my_image",
                    "name": "job_name",
                    "env": [
                        {
                            "name": "MY_VAR_ENV",
                            "value": "toto"
                        }
                    ],
                    "resources": {
                        "cpu": 0.25,
                        "memory": "0.5Gi"
                    },
                    "command": [
                        "echo", "'Hello World'"
                    ]
                }
            ]
        }
    }
}

response = client.jobs.begin_create_or_update(rg, job_name, job_parameters).result()

job_execution_template = {
        "containers" : [
            {
                "image": "artifacts.xxx.com/my_image",
                "name": "other_name",
                "env": [
                    {
                        "name": "CONTAINER_APP_JOB_NAME",
                        "value": val
                    }
                ],
                "resources": {
                    "cpu": 0.5,
                    "memory": "1Gi",
                },
                "command": [
                    "/bin/bash", "-c", f"echo {val}; env"
                ]
            }
        ]
    }

response = client.jobs.begin_start("my-ressource-group", "job_name", job_execution_template).result()

使用LogsQueryClient获取日志的代码

from azure.identity import DefaultAzureCredential
from azure.monitor.query import LogsQueryClient
from datetime import timedelta


# Define your resource group and subscription ID
rg = "my-ressource-group"
subscription_id = "xxxxxxxxxx"
workspace_id = "xxxxxxxxx"

# Initialize the client
credential = DefaultAzureCredential()
client = LogsQueryClient(credential)

# Define the query
query = """
ContainerAppConsoleLogs
| where ContainerGroupName startswith 'my-job'
"""


# Query the logs
response = client.query_workspace(
    workspace_id=workspace_id,
    query=query,
    timespan=timedelta(days=1)
)

# Print the logs
for table in response.tables:
    for row in table.rows:
        print(row[10]) # Log column

现寻求无延迟获取Azure容器应用作业执行控制台日志的有效方法。

内容的提问来源于stack exchange,提问作者BeGreen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 06:16:13