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