<s> import logging <EOL> import applicationinsights <EOL> enabled_instrumentation_keys = { } <EOL> def enable ( instrumentation_key , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not instrumentation_key : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if instrumentation_key in enabled_instrumentation_keys : <EOL> logging . getLogger ( ) . removeHandler ( enabled_instrumentation_keys [ instrumentation_key ] ) <EOL> handler = LoggingHandler ( instrumentation_key , * args , ** kwargs ) <EOL> handler . setLevel ( logging . INFO ) <EOL> enabled_instrumentation_keys [ instrumentation_key ] = handler <EOL> logging . getLogger ( ) . addHandler ( handler ) <EOL> return handler <EOL> class LoggingHandler ( logging . Handler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , instrumentation_key , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not instrumentation_key : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> telemetry_channel = kwargs . get ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in kwargs : <EOL> del kwargs [ '<STR_LIT>' ] <EOL> self . client = applicationinsights . TelemetryClient ( instrumentation_key , telemetry_channel ) <EOL> super ( LoggingHandler , self ) . __init__ ( * args , ** kwargs ) <EOL> def flush ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . flush ( ) <EOL> return super ( LoggingHandler , self ) . flush ( ) <EOL> def emit ( self , record ) : <EOL> """<STR_LIT>""" <EOL> properties = { <EOL> '<STR_LIT>' : record . processName , <EOL> '<STR_LIT>' : record . module , <EOL> '<STR_LIT>' : record . filename , <EOL> '<STR_LIT>' : record . lineno , <EOL> '<STR_LIT>' : record . levelname , <EOL> } <EOL> if record . exc_info : <EOL> self . client . track_exception ( * record . exc_info , properties = properties ) <EOL> return <EOL> formatted_message = self . format ( record ) <EOL> self . client . track_trace ( formatted_message , properties = properties ) </s>
