eigen
eigen

Reputation: 95

Set Python Logging to Azure Blob, but Can not Find Log File there

I'm setting up logging on a flask service. I tried to write logs to an azure blob storage with the following code

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)

Then, I tried with logger.warning('test warning'), but there's not log file created on the azure blob container.

Can anyone help me figure out where I did wrong?

Best, Eigen

Upvotes: 3

Views: 4967

Answers (1)

Jay Gong
Jay Gong

Reputation: 23782

I tried your code in my flask but did't reproduce your issue.

code:

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

output:

enter image description here

More details, you could refer to the doc and source code.

Hope it helps you. Any concern,please let me know.

Upvotes: 5

Related Questions