learning
learning

Reputation: 11

Logging the errors to azure blob

I am trying to log errors to azure blob but, its not creating any table in the blob. I have gone through many docs and also searched for ans in stackoverflow as well. Please help me with this. Thanks

below is the code

def log():

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

Upvotes: 1

Views: 1676

Answers (1)

Jim Xu
Jim Xu

Reputation: 23111

According to the code you provided, you use TableStorageHandler to store log. It will help us store log in Azure table storage instead of Azure blob storage. Please find your logs in Azure table.

Besides, if you want to store your log in Azure blob, please refer to the following code

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

For more details, please refer to the document


Update

When we useBlobStorageRotatingFileHandler, the log does not upload if the content does not reach till maxBytes

My test code

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

enter image description here

Upvotes: 1

Related Questions