azure - 将 Python 日志记录设置为 Azure Blob,但在那里找不到日志文件

标签 azure logging flask azure-blob-storage

我正在设置 flask 服务的日志记录。我尝试使用以下代码将日志写入 azure 的 blob 存储

import logging
import sys

from azure_storage_logging.handlers import BlobStorageRotatingFileHandler

logger = logging.getLogger('service_logger')
logger.setLevel(logging.DEBUG)
log_formater = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(process)d - %(message)s')
azure_blob_handler = BlobStorageRotatingFileHandler(filename = 'service.log', 
                                                    account_name='servicestorage',
                                                    account_key='',
                                                    maxBytes= maxBytes,
                                                    container='service-log')
azure_blob_handler.setLevel(logging.INFO)
azure_blob_handler.setFormatter(log_formater)
logger.addHandler(azure_blob_handler)

然后,我尝试使用 logger.warning('test warning'),但没有在 azure blob 容器上创建日志文件。

谁能帮我找出我哪里做错了?

最好, 特征值

最佳答案

我在我的 flask 中尝试了您的代码,但没有重现您的问题。

代码:

@app.route('/log')
def log():

    import logging
    import sys
    from azure_storage_logging.handlers import BlobStorageRotatingFileHandler

    mystorageaccountname='***'
    mystorageaccountkey='***'

    logger = logging.getLogger('service_logger')
    logger.setLevel(logging.DEBUG)
    log_formater = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(process)d - %(message)s')
    azure_blob_handler = BlobStorageRotatingFileHandler(filename = 'service.log', 
                                                        account_name=mystorageaccountname,
                                                        account_key=mystorageaccountkey,
                                                        maxBytes= 5,
                                                        container='service-log')
    azure_blob_handler.setLevel(logging.INFO)
    azure_blob_handler.setFormatter(log_formater)
    logger.addHandler(azure_blob_handler)

    logger.warning('warning message')

输出:

enter image description here

更多详情,可以引用docsource code .

希望对您有帮助。如有任何疑问,请告诉我。

关于azure - 将 Python 日志记录设置为 Azure Blob,但在那里找不到日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51586370/

相关文章:

java - 如何在使用 Eclipse 创建的可运行 jar 中指定外部 log4j2 配置文件

python - 如何使用 Flask/uwsgi/nginx 服务器管理内容和 python 代码?

azure - 尝试在 azure 上部署示例 Web 应用程序时出现问题

c# - Azure 应用服务抛出运行时错误 : Assembly not Found

azure - 批量上传时触发一次 Azure Function

logging - 使 log4j DailyRollingFileAppender 滚动日期或大小,以先到者为准

azure - 在 Azure 数据工厂中计划 U-SQL 作业

java - 使用任何文件附加程序的 Log4j 每日轮换和每月保留

python - 一对多关系和Fk列位置

python - Flask 和 Bootstrap 多个可折叠项,但它们各自只打开第一个