DataFactory 按需 HDInsight 群集日志到 Azure Blob:“BlobStorageRotatingFileHandler”

问题描述 投票:0回答:1

我正在使用按需 HDInsight 集群在 Azure Datafactory 管道上运行 Spark 脚本。 我想将管道日志保存到 Azure 容器。尝试实现这个:

将错误记录到 azure blob

但是当我使用上面链接中提到的日志逻辑运行脚本时,我不断收到 No module named azure_storage_logging.handlers 信息。

如何在数据工厂管道上运行的 Spark 脚本中为此模块执行“pip 安装”?

或者在我的 Spark 脚本中实现日志逻辑的任何其他替代方法?

apache-spark logging pyspark azure-data-factory azure-blob-storage
1个回答
0
投票

在代码开头添加以下 Python 代码,以便安装必要的包。

import os

try:
    from azure_storage_logging.handlers import BlobStorageRotatingFileHandler
    print("Module is installed.")
except ImportError:
    print("Module is not installed. Installing...")
    os.system('pip install azure-storage==0.36.0 azure-storage-logging')
    from azure_storage_logging.handlers import BlobStorageRotatingFileHandler

代码:

import logging
import os

try:
    from azure_storage_logging.handlers import BlobStorageRotatingFileHandler
    print("Module is installed.")
except ImportError:
    print("Module is not installed. Installing...")
    os.system('pip install azure-storage==0.36.0 azure-storage-logging')
    from azure_storage_logging.handlers import BlobStorageRotatingFileHandler

mystorageaccountname='acc_name'
mystorageaccountkey='key'

logger = logging.getLogger('service_logger')
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')

输出

service.log

2023-12-27  12:35:13,292 - service_logger - WARNING - 16456 - warning message
© www.soinside.com 2019 - 2024. All rights reserved.