【问题标题】:Logging the errors to azure blob将错误记录到 azure blob
【发布时间】:2020-05-05 07:02:02
【问题描述】:

我正在尝试将错误记录到 azure blob,但它没有在 blob 中创建任何表。我浏览了很多文档,也在stackoverflow中搜索了ans。请帮我解决一下这个。 谢谢

下面是代码

定义日志():

import logging
import sys
from azure_storage_logging.handlers import BlobStorageRotatingFileHandler

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

_LOGFILE_TMPDIR = mkdtemp()

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 = TableStorageHandler(account_name=mystorageaccountname,
                                         account_key=mystorageaccountkey,
                                         protocol='https',
                                         table='logtable',
                                         batchsize=100,
                                         extra_properties=None, 
                                         partition_key_formatter=None, 
                                         row_key_formatter=None, 
                                         is_emulated=False)
logger.addHandler(azure_blob_handler)

logger.warning('warning message')

【问题讨论】:

  • 基于此链接:github.com/michiya/azure-storage-logging 并且根据您的代码,您的日志记录数据应该在您使用 TableStorageHandler 时进入 Azure 表存储。请检查表而不是 blob 容器。
  • 它也没有创建任何表。我也尝试使用 BlobStorageRotatingFileHandler 创建一个文件,但我看不到任何文件。

标签: azure-storage azure-blob-storage azure-databricks python-logging


【解决方案1】:

根据您提供的代码,您使用TableStorageHandler 来存储日志。它将帮助我们将日志存储在 Azure 表存储而不是 Azure Blob 存储中。请在 Azure 表中查找您的日志。

另外,如果你想将日志存储在 Azure blob 中,请参考以下代码

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')

更多详情请参考document


更新

当我们使用BlobStorageRotatingFileHandler时,如果内容没有达到maxBytes就不会上传日志

我的测试代码

import logging
import sys
from azure_storage_logging.handlers import BlobStorageRotatingFileHandler

mystorageaccountname='blobstorage0516'
mystorageaccountkey=''

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')

【讨论】:

  • 它也没有创建任何表。我也尝试使用 BlobStorageRotatingFileHandler 创建一个文件,但我看不到任何文件。
  • @learning 既然您的问题已经解决了,请您接受答案:meta.stackexchange.com/questions/5234/…吗?它可能会帮助更多有类似问题的人。
猜你喜欢
  • 2018-02-17
  • 2022-08-12
  • 1970-01-01
  • 1970-01-01
  • 2017-04-01
  • 1970-01-01
  • 2019-09-21
  • 2019-05-19
  • 2018-02-26
相关资源
最近更新 更多