Python Azure函数计时器触发器日志信息在部署时不显示

3bygqnnd  于 2023-10-22  发布在  Python
关注(0)|答案(1)|浏览(139)

我有一个定时器触发功能,下载毫升模型。我想记录这些模型的名称,并在azure monitor中查看此输出。下面是我如何使用日志库的一个例子。这只是一段代码。为了澄清,keys变量从我之前加载的一个特定的json文件中获取所有模型名称。

import datetime
import logging
import azure.functions as func
from azureml.core import Model

def main(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    if mytimer.past_due:
    
        keys = model_file.keys()
        logging.info("downloading the following  models:")
        for key in keys:
            logging.info(key)
            model_obj = Model(ws, key)
            model_path = model_obj.download(target_dir=downloaded_models_path)
            print(model_path)

    logging.info(
        "download_ml_models timer trigger function ran at %s", utc_timestamp)

我希望日志显示的模型,我已经记录的实际信息。但是,从下面你可以看到Azure监视器只显示函数已成功执行

uemypmqf

uemypmqf1#

  • 我尝试了下面的方法来记录我的TimerTrigger中下载的型号名称。*
    My init.py:-
import datetime
import logging
import os
import azure.functions as func
from azureml.core import Model, Workspace
from applicationinsights import TelemetryClient
import json

# Initialize Application Insights
instrumentation_key = ("InstrumentationKey=xxxxx8bd;IngestionEndpoint=https://uksouth-1.in.applicationinsights.azure.com/;LiveEndpoint=https://uksouth.livediagnostics.monitor.azure.com/")
tc = TelemetryClient(instrumentation_key)

# Function to download models
def download_models():
    try:
        ws = Workspace.from_config('./config.json')
        # Load your Azure ML Workspace. Replace with your own values.
        ws = Workspace(subscription_id="xxxxxxx74c23f",
                       resource_group="rg-name",
                       workspace_name="mlws-name")

        # Replace with the actual path where you want to download the models
        downloaded_models_path = "temp"

        utc_timestamp = datetime.datetime.utcnow().replace(
            tzinfo=datetime.timezone.utc).isoformat()

        # Load model info from the JSON file
        with open("model_info.json", "r") as json_file:
            model_info = json.load(json_file)

        logging.info("Downloading the following models:")
        for model in model_info["models"]:
            model_name = model["name"]
            model_path = model["path"]
            logging.info("Model Name: %s", model_name)

            model_obj = Model(ws, model_name)
            model_path = model_obj.download(target_dir=downloaded_models_path)
            logging.info("Downloaded model %s to %s", model_name, model_path)
            tc.track_trace("Downloaded model {} to {}".format(model_name, model_path))

        tc.flush()

    except Exception as e:
        logging.error("An error occurred: %s", str(e))
        tc.track_exception()
        tc.flush()

# Azure Function entry point
def main(mytimer: func.TimerRequest) -> None:
    if mytimer.past_due:
        logging.info("Downloading models triggered by the timer...")
        tc.track_event("Downloading models triggered by the timer")
        download_models()

    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()
    logging.info("download_ml_models timer trigger function ran at %s", utc_timestamp)
    tc.track_event("download_ml_models timer trigger function ran at {}".format(utc_timestamp))
    tc.flush()

在当前代码中。如果要在Azure Monitor中查看此输出,请修改代码中的日志记录行,以包含正在下载的模型的真实的名称。只需将行**logging.info(key)替换为logging.info(f"downloading model: key")**。这将记录下载时模型的名称。

import datetime
import logging
import azure.functions as func
from azureml.core import Model

def main(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    if mytimer.past_due:
    
        keys = model_file.keys()
        logging.info("downloading the following  models:")
        for key in keys:
            logging.info(f"downloading model: key")
            model_obj = Model(ws, key)
            model_path = model_obj.download(target_dir=downloaded_models_path)
            print(model_path)

    logging.info(
        "download_ml_models timer trigger function ran at %s", utc_timestamp)

相关问题