Пример #1
0
 def addQueueLogHandler(self):
     global logger
     try:
         storageQueue = AzureStorageQueue(
             sapmonId=self.sapmonId,
             msiClientID=self.vmTags.get("SapMonMsiClientId", None),
             subscriptionId=self.vmInstance["subscriptionId"],
             resourceGroup=self.vmInstance["resourceGroupName"])
         storageKey = storageQueue.getAccessKey()
         queueStorageLogHandler = QueueStorageHandler(
             account_name=storageQueue.accountName,
             account_key=storageKey,
             protocol="https",
             queue=storageQueue.name)
         queueStorageLogHandler.level = DEFAULT_QUEUE_LOG_LEVEL
         formatter = logging.Formatter(
             LOG_CONFIG["formatters"]["detailed"]["format"])
         queueStorageLogHandler.setFormatter(formatter)
     except Exception:
         logger.error(
             "could not add handler for the storage queue logging: %s " %
             traceback.format_exc())
         return
     logger.addHandler(queueStorageLogHandler)
     return
   def addQueueLogHandler(
           tracer: logging.Logger,
           ctx) -> None:
      # Provide access to custom (payload-specific) fields
      oldFactory = logging.getLogRecordFactory()
      def recordFactory(*args, **kwargs):
         record = oldFactory(*args, **kwargs)
         record.sapmonid = ctx.sapmonId
         record.payloadversion = PAYLOAD_VERSION
         return record
      tracer.info("adding storage queue log handler")
      try:
         storageQueue = AzureStorageQueue(tracer,
                                          ctx.sapmonId,
                                          ctx.authToken,
                                          ctx.vmInstance["subscriptionId"],
                                          ctx.vmInstance["resourceGroupName"],
                                          queueName = STORAGE_QUEUE_NAMING_CONVENTION % ctx.sapmonId)
         storageKey = storageQueue.getAccessKey()
         queueStorageLogHandler = QueueStorageHandler(account_name=storageQueue.accountName,
                                                      account_key = storageKey,
                                                      protocol = "https",
                                                      queue = storageQueue.name)
         queueStorageLogHandler.level = DEFAULT_QUEUE_TRACE_LEVEL
         jsonFormatter = JsonFormatter(tracing.config["formatters"]["json"]["fieldMapping"])
         queueStorageLogHandler.setFormatter(jsonFormatter)
         logging.setLogRecordFactory(recordFactory)

      except Exception as e:
         tracer.error("could not add handler for the storage queue logging (%s) " % e)
         return

      queueStorageLogHandler.level = DEFAULT_QUEUE_TRACE_LEVEL
      tracer.addHandler(queueStorageLogHandler)
      return
   def initCustomerAnalyticsTracer(tracer: logging.Logger,
                                   ctx) -> logging.Logger:
       tracer.info("creating customer metrics tracer object")
       try:
           storageQueue = AzureStorageQueue(tracer,
                                            ctx.sapmonId,
                                            ctx.authToken,
                                            ctx.vmInstance["subscriptionId"],
                                            ctx.vmInstance["resourceGroupName"],
                                            CUSTOMER_METRICS_QUEUE_NAMING_CONVENTION % ctx.sapmonId)
           storageKey = storageQueue.getAccessKey()
           customerMetricsLogHandler = QueueStorageHandler(account_name = storageQueue.accountName,
                                                           account_key = storageKey,
                                                           protocol = "https",
                                                           queue = storageQueue.name)
       except Exception as e:
           tracer.error("could not add handler for the storage queue logging (%s) " % e)
           return

       logger = logging.getLogger("customerMetricsLogger")
       logger.addHandler(customerMetricsLogHandler)
       return logger
Пример #4
0
import logging
import sys
import os
from azure_storage_logging.handlers import QueueStorageHandler

formatter = logging.Formatter(fmt='%(asctime)s %(levelname)-8s %(message)s',
                              datefmt='%Y-%m-%d %H:%M:%S')

screen_handler = logging.StreamHandler(stream=sys.stdout)
screen_handler.setFormatter(formatter)

logger = logging.getLogger('api')
logger.setLevel('DEBUG')
logger.addHandler(screen_handler)

if (os.environ.get('AZURE_STORAGE_ACCOUNT') is not None and
        os.environ.get('AZURE_STORAGE_KEY') is not None):
    handler = QueueStorageHandler(account_name=os.environ.get('AZURE_STORAGE_ACCOUNT'),
                                  account_key=os.environ.get('AZURE_STORAGE_KEY'),
                                  queue='api',
                                  )
    handler.setFormatter(formatter)
    logger.addHandler(handler)