-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_before_logging.py
36 lines (28 loc) · 1.36 KB
/
filter_before_logging.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import logging
from opencensus.ext.azure.log_exporter import AzureLogHandler
logFormatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s")
logger = logging.getLogger('log-app')
logger.setLevel(logging.DEBUG)
def filter_log(envelope):
print('In callback function')
logMessage = envelope.data.baseData.message
if(logMessage.find('secret') >= 0):
print('log message contains sensitive information')
return False
return True
fileHandler = logging.FileHandler("{0}/{1}.log".format('.', 'test'))
fileHandler.setFormatter(logFormatter)
logger.addHandler(fileHandler)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
logger.addHandler(consoleHandler)
#use valid Application Insights instrumentation key
insightsHandler = AzureLogHandler(connection_string='InstrumentationKey=06e8****-****-****-****-********a701')
insightsHandler.setFormatter(logFormatter)
insightsHandler.add_telemetry_processor(filter_log)
logger.addHandler(insightsHandler)
app_properties = {'app_name': 'log_filter', 'api_key': 'ertsyulkhgjhgf', 'api_secret': 'CYUJKLJKHGFHVB3456YHG89GHJ4567VB'}
print('Sending logs to app insights')
logger.info("Log statment without any sensitive information")
logger.debug('Log statement with sensitive information: %s', app_properties)
print('End sending logs to app insights')