|
30 | 30 | log_fmt = base_log_fmt.copy() |
31 | 31 | audit_log_fmt = {"audit": True} |
32 | 32 | audit_log_fmt.update(log_fmt) |
| 33 | + |
33 | 34 | log_cfg = { |
34 | 35 | "version": 1, |
35 | 36 | "disable_existing_loggers": False, |
|
43 | 44 | "formatter": "json", |
44 | 45 | "level": LOG_LEVEL, |
45 | 46 | }, |
46 | | - "appinsights": { |
47 | | - "class": "opencensus.ext.azure.log_exporter.AzureLogHandler", |
48 | | - "connection_string": AZURE_APPLICATIONINSIGHTS_CONNSTRING, |
49 | | - "formatter": "json-audit", |
50 | | - "level": "DEBUG", |
51 | | - }, |
52 | | - "appinsightsaccess": { |
53 | | - "class": "opencensus.ext.azure.log_exporter.AzureLogHandler", |
54 | | - "connection_string": AZURE_APPLICATIONINSIGHTS_CONNSTRING, |
55 | | - "formatter": "json", |
56 | | - "level": "DEBUG", |
57 | | - }, |
58 | 47 | }, |
59 | 48 | "loggers": { |
60 | 49 | "jwtproxy.applogs": { |
|
64 | 53 | }, |
65 | 54 | "jwtproxy.auditlogs": { |
66 | 55 | "propagate": False, |
67 | | - "handlers": ["appinsights"], |
| 56 | + "handlers": ["console"], # changed below if Azure Connection string is set |
68 | 57 | "level": "DEBUG", # Send everything |
69 | 58 | }, |
70 | 59 | "jwtproxy.accesslogs": { |
71 | 60 | "propagate": False, |
72 | | - "handlers": ["appinsightsaccess"], |
| 61 | + "handlers": ["console"], # changed below if Azure Connection string is set |
73 | 62 | "level": "DEBUG", # Send everything |
74 | 63 | }, |
75 | 64 | }, |
76 | 65 | } |
77 | 66 |
|
78 | | -if not AZURE_APPLICATIONINSIGHTS_CONNSTRING: |
79 | | - log_cfg["handlers"].pop("appinsights") |
80 | | - log_cfg["handlers"].pop("appinsightsaccess") |
81 | | - log_cfg["loggers"]["jwtproxy.auditlogs"]["handlers"] = [ |
82 | | - "console", |
83 | | - ] |
84 | | - log_cfg["loggers"]["jwtproxy.accesslogs"]["handlers"] = [ |
85 | | - "console", |
86 | | - ] |
| 67 | +if AZURE_APPLICATIONINSIGHTS_CONNSTRING: |
| 68 | + from azure.monitor.opentelemetry.exporter import AzureMonitorLogExporter |
| 69 | + from opentelemetry.sdk._logs import LoggerProvider |
| 70 | + from opentelemetry.sdk._logs.export import BatchLogRecordProcessor |
| 71 | + |
| 72 | + logger_provider = LoggerProvider() |
| 73 | + logger_provider.add_log_record_processor( |
| 74 | + BatchLogRecordProcessor( |
| 75 | + AzureMonitorLogExporter(connection_string=AZURE_APPLICATIONINSIGHTS_CONNSTRING) |
| 76 | + ) |
| 77 | + ) |
| 78 | + log_cfg["handlers"]["appinsights"] = { |
| 79 | + "class": "opentelemetry.sdk._logs.LoggingHandler", |
| 80 | + "logger_provider": logger_provider, |
| 81 | + "formatter": "json-audit", |
| 82 | + "level": "DEBUG", |
| 83 | + } |
| 84 | + log_cfg["handlers"]["appinsightsaccess"] = { |
| 85 | + "class": "opentelemetry.sdk._logs.LoggingHandler", |
| 86 | + "logger_provider": logger_provider, |
| 87 | + "formatter": "json", |
| 88 | + "level": "DEBUG", |
| 89 | + } |
| 90 | + log_cfg["loggers"]["jwtproxy.auditlogs"]["handlers"] = ["appinsights"] |
| 91 | + log_cfg["loggers"]["jwtproxy.accesslogs"]["handlers"] = ["appinsightsaccess"] |
87 | 92 | dictConfig(log_cfg) |
88 | 93 |
|
89 | 94 | app_logger = logging.getLogger("jwtproxy.applogs") |
|
0 commit comments