def attach_appinsights(logger, instrumentation_key: str): if not instrumentation_key: logger.warning( "appinsights instrumentation key is null; not writing to app insights" ) return handler = LoggingHandler(instrumentation_key) # TODO: extend this collection to jobid, pipelineid or farmid etc. handler.client._context.properties['Collection'] = 'ADF_LOGS' # Removing all PIO information from context. # Due to a bug in LoggingHanlder constructor, its not honoring context passed. # so trying to set values once handler is created. Overriding with 'None' string # instead of None as for later value, its taking default value from constructor. handler.client._context.device = contracts.Device() handler.client._context.device.os_version = NONE_STRING handler.client._context.device.locale = NONE_STRING handler.client._context.device.id = NONE_STRING handler.client._context.device.type = NONE_STRING handler.client._context.device.oem_name = NONE_STRING handler.client._context.device.model = NONE_STRING handler.client._context.location = contracts.Location() handler.client._context.location.ip = NONE_STRING handler.client._context.user = contracts.User() handler.client._context.user.id = NONE_STRING handler.client._context.user.account_id = NONE_STRING handler.client._context.user.auth_user_id = NONE_STRING handler.client._context.session = contracts.Session() handler.client._context.session.id = NONE_STRING handler.client._context.cloud = contracts.Cloud() handler.client._context.cloud.role = NONE_STRING handler.setLevel(Logger.get_logging_level()) # Log formatter looks similar to default python logging statement. handler.setFormatter( logging.Formatter( '[%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(process)d:%(thread)d]: %(message)s' )) # enable exceptions to app insights enable(instrumentation_key) logger.addHandler(handler) logger.info("Attached app insights handler to logger")
def __init__(self, bright_host_ip: str, metrics: Iterable[str], instrumentation_key: str): self.__set_bright_host_ip(bright_host_ip) self.__set_metrics(metrics) self.__set_instrumentation_key(instrumentation_key) bright_cluster = self.__create_bright_cluster() self.__set_bright_cluster(bright_cluster) emit_handler = LoggingHandler(instrumentation_key) emit_handler.setLevel(logging.DEBUG) self.__set_emit_handler(emit_handler) emitter = logging.getLogger('application-insights') emitter.setLevel(logging.DEBUG) emitter.addHandler(emit_handler) self.__set_emitter(emitter) mutex = threading.Lock() self.__set_mutex(mutex)
def setup_logger(): logging.setLoggerClass(CustomLogger) _logger = logging.getLogger() _logger.setLevel(logging.DEBUG) # Stdout handler c_handler = logging.StreamHandler() c_handler.setLevel(logging.DEBUG) c_handler.setFormatter(logging.Formatter(format_str)) _logger.addHandler(c_handler) # Application insight handler if utils.convert_to_boolean(os.environ["AZURE_USEAPPINSIGHT"]): from applicationinsights import TelemetryClient from applicationinsights.logging import LoggingHandler a_handler = LoggingHandler(os.environ["AZURE_INSTRUMENTATION_KEY"]) a_handler.setLevel(logging.DEBUG) a_handler.setFormatter(logging.Formatter(ai_str)) _logger.addHandler(a_handler) tc = TelemetryClient(os.environ["AZURE_INSTRUMENTATION_KEY"]) tc.channel.queue.max_queue_length = 2
#Enable unhandled exception logging enable(instrumentation_key) #setup other needed variables tc = TelemetryClient(instrumentation_key) tc.context.application.ver = '0.0.1' tc.context.device.id = 'Sample notebook' telemetry_channel = channel.TelemetryChannel() telemetry_channel.context.application.ver = '1.0.0' telemetry_channel.context.properties['application_name'] = 'sample_notebook' telemetry_channel.context.properties['application_id'] = sc.applicationId handler = LoggingHandler(instrumentation_key, telemetry_channel=telemetry_channel) handler.setLevel(logging.DEBUG) handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s')) logger = logging.getLogger('simple_logger') logger.setLevel(logging.INFO) logger.addHandler(handler) logger.info('Starting sample app ... ') def getReadConfig(): readConfig = { "Endpoint": "https://nomier-test-sql.documents.azure.com:443/", "Masterkey": "<MK>", "Database": "partitionIdTestDB", "Collection": "sourcecollection", "SamplingRatio": "1.0"