Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 757
eigen Avatar asked Sep 17 '25 17:09

eigen


1 Answers

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.

like image 92
Jay Gong Avatar answered Sep 20 '25 07:09

Jay Gong