Azure DevOps Pipeline 上没有日志记录

Str*_*420 7 python logging azure azure-devops azure-pipelines

更新:

是否可以添加或更改在 Azure DevOps 上执行管道的命令?


在Visual Studio Code上本地运行我的程序,我确实得到了输出。

但是,在Azure DevOps上运行我的GitHub原始分支不会产生任何输出。

我遵循了 Stack Overflow 的答案,该答案引用了GitHub 问题的解决方案

我已经实现了以下内容,但 Azure 的原始日志在我的 Python 上返回空白logging

test_logging.py

import logging

filename = "my.log"

global logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
open(filename, "w").close()  # empty logs
fileHandler = logging.FileHandler(filename)
fileHandler.setFormatter(formatter)
fileHandler.setLevel(logging.INFO)
logger.addHandler(fileHandler)

logger.error('TEST')

# fetch logs
with open(filename, "r") as fileHandler:
    logs = [log.rstrip() for log in fileHandler.readlines()]
open(filename, "w").close()  # empty logs
print('logs = ', logs)
Run Code Online (Sandbox Code Playgroud)
>>> logs = []
Run Code Online (Sandbox Code Playgroud)

host.json

>>> logs = []
Run Code Online (Sandbox Code Playgroud)

然后我尝试了帖子host.json中的这个替代方案:

{
  "version":  "2.0",
  "logging": {
    "fileLoggingMode": "always",
    "logLevel": {
      "default": "Debug"
    }
  } 
}
Run Code Online (Sandbox Code Playgroud)

azure-pipeline-ontology_tagger.yaml

"logging": {
    "fileLoggingMode": "debugOnly",
    "logLevel": {
        "default": "None",
        "Host.Results": "Information",
        "Function": "Information",
        "Host.Aggregator": "Information"
    },
    "applicationInsights": {
        "samplingSettings": {
            "isEnabled": false,
            "maxTelemetryItemsPerSecond": 5
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我还需要提供其他信息,请告诉我。

Juk*_*kaK 2

我认为您从根本上混淆了这里的一些内容:您提供的链接和下面的链接提供了有关在 Azure Functions 中设置日志记录的指南。但是,您似乎正在谈论 Azure Pipelines 中的日志记录,这是完全不同的事情。所以要明确的是:

Azure Pipelines 运行生成和部署作业,将 GitHub 存储库上可能有的代码部署到 Azure Functions。管道在 Azure Pipelines 代理中执行,该代理可以是 Microsoft 或自托管。如果我们假设你使用 Microsoft 托管代理执行管道,则不应假设这些代理具有 Azure Functions 可能具有的任何功能(也不应首先执行针对 Azure Functions 的代码)。如果您想在管道中执行 python 代码,您应该首先开始查看托管代理已预安装哪些与 python 相关的功能并从那里开始工作: https: //learn.microsoft.com/en-us/azure/ devops/pipelines/agents/hosted?view=azure-devops&tabs=yaml

如果您想记录有关管道运行的信息,则应在手动排队管道时首先选中“启用系统诊断”选项。要自行实现更多日志记录,请检查:https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands ?view=azure-devops&tabs=bash

对于登录 Azure Functions,您可能需要从这里开始: https: //learn.microsoft.com/en-us/azure/azure-functions/functions-monitoring,但这与登录 Azure Pipelines 是完全不同的主题。