admin管理员组

文章数量:1279147

I am using python SDK to deploy a ContainerJob.

Once the job is begin_create_or_update I start it.

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",
                    "username": "xxxxx",
                    "passwordSecretRef": "xxxxx",
                },
            ]
        },
        "template": {
            "containers": [
                {
                    "image": "artifacts.xxx/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/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()

And I find no documentation or easy way to get Console Logs of this executed Job.

Package AppContainersManagementClient does not seem to be suitable for me, since I don't have a Container instance Group.

I tried using az cli, but still no success.

az containerapp job logs show -n my-job -g my-ressource-groupe --container my-job

No execution provided, defaulting to latest execution: my-job-wx64v7y No replicas found for execution

The only way I found was to use LogsQueryClient package. But i have 5 to 10 minutes lags of logs.

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

I am using python SDK to deploy a ContainerJob.

Once the job is begin_create_or_update I start it.

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",
                    "username": "xxxxx",
                    "passwordSecretRef": "xxxxx",
                },
            ]
        },
        "template": {
            "containers": [
                {
                    "image": "artifacts.xxx/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/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()

And I find no documentation or easy way to get Console Logs of this executed Job.

Package AppContainersManagementClient does not seem to be suitable for me, since I don't have a Container instance Group.

I tried using az cli, but still no success.

az containerapp job logs show -n my-job -g my-ressource-groupe --container my-job

No execution provided, defaulting to latest execution: my-job-wx64v7y No replicas found for execution

The only way I found was to use LogsQueryClient package. But i have 5 to 10 minutes lags of logs.

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
Share Improve this question asked Feb 24 at 16:09 BeGreenBeGreen 9511 gold badge19 silver badges53 bronze badges 2
  • 2 Use the LogsQueryClient to query the Log Analytics workspace. Although it comes with a delay, this is the supported way to retrieve the Container Job’s console logs. – Suresh Chikkam Commented Feb 25 at 3:35
  • We had a K8S Cluster before, we could retrieve log of the pod instantly, we have a need to have instant logs for our usecase. So I think we will have to send logs to Elastic Search and retrieve it. Such a waste of energy for a bad design in ACA... – BeGreen Commented Feb 25 at 7:40
Add a comment  | 

1 Answer 1

Reset to default 0

Azure Container Apps is designed as a fully managed, serverless service that integrate with centralized monitoring (i.e., Azure Monitor and Log Analytics). This design choice means that logs are not streamed instantly but rather ingested into Log Analytics, where there’s a small delay before they’re available.

  • Implement a custom logging pipeline—forwarding logs directly to a solution like ElasticSearch so they can query them in near real time.

Configure Container Apps Jobs diagnostic settings so that stdout, stderr, and other logs are forwarded to an Azure Event Hub. Create an Azure Function with an Event Hub trigger. When a new log message comes, the function reads the message.

init.py:

import logging
import os
import json
import azure.functions as func
from elasticsearch import Elasticsearch, RequestsHttpConnection

def main(events: func.EventHubEvent):
    # Elasticsearch connection settings from environment variables
    es_host = os.environ.get("ELASTICSEARCH_HOST")
    es_port = int(os.environ.get("ELASTICSEARCH_PORT", 443))
    es_user = os.environ.get("ELASTICSEARCH_USER")
    es_password = os.environ.get("ELASTICSEARCH_PASSWORD")
    
    # Connect to Elasticsearch
    es = Elasticsearch(
        hosts=[{'host': es_host, 'port': es_port}],
        http_auth=(es_user, es_password),
        use_ssl=True,
        verify_certs=True,
        connection_class=RequestsHttpConnection
    )
    
    for event in events:
        try:
            # Each event message is expected to be a JSON string
            message_body = event.get_body().decode('utf-8')
            log_entry = json.loads(message_body)
            
            # Optionally, add a timestamp or other metadata if needed
            response = es.index(index="container-app-logs", body=log_entry)
            logging.info(f"Indexed log entry: {response}")
        except Exception as e:
            logging.error(f"Error processing event: {e}")

function.json:

{
  "bindings": [
    {
      "type": "eventHubTrigger",
      "name": "events",
      "direction": "in",
      "eventHubName": "your-eventhub-name",
      "connection": "EVENTHUB_CONNECTION_STRING",
      "cardinality": "many",
      "consumerGroup": "$Default"
    }
  ]
}

Response:

Log event was generated.

{
  "timestamp": "2025-02-25T12:34:56.789Z",
  "containerAppName": "xxxxxxxxxxx",
  "containerName": "xxxxxxxxxx",
  "executionId": "xxxxxxxxxxxxxxxxx64v7y",
  "logLevel": "INFO",
  "message": "Container started successfully. Running job command: echo 'Hello World'",
  "environment": "West Europe",
  "replicaId": "replica-1"
}
{
  "timestamp": "2025-02-25T12:35:10.456Z",
  "containerAppName": "xxxxxxxxxx",
  "containerName": "xxxxxxxxx",
  "executionId": "xxxxxxxxxxxxxxxx64v7y",
  "logLevel": "DEBUG",
  "message": "Job execution in progress... processing step 1 of 3",
  "environment": "West Europe",
  "replicaId": "replica-1"
}

本文标签: Get Console logs of Azure Container Apps Jobs Execution in pythonStack Overflow