Created
March 25, 2026 05:18
-
-
Save JacksonWeber/cf973eeeb656a8485b0ae2019d6c1a47 to your computer and use it in GitHub Desktop.
Workaround for Azure/azure-sdk-for-js#33975 — Override Azure Monitor distro loggers with a custom DiagLogger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Workaround for https://github.com/Azure/azure-sdk-for-js/issues/33975 | |
| * | |
| * Problem: | |
| * @azure/monitor-opentelemetry's internal Logger class unconditionally | |
| * takes over both the OpenTelemetry diagnostic logger (via diag.setLogger()) | |
| * and the Azure SDK logger (via AzureLogger.log = ...) when it initializes. | |
| * The Logger class is not exported, and initialization is non-deterministic | |
| * (lazy singleton on first internal use), making it hard to override. | |
| * | |
| * Solution: | |
| * Call useAzureMonitor() first, then immediately re-set both loggers | |
| * to your own implementation. OTel's diag.setLogger() fully supports | |
| * custom DiagLogger implementations, and AzureLogger.log can be reassigned. | |
| */ | |
| import { useAzureMonitor } from "@azure/monitor-opentelemetry"; | |
| import { AzureLogger, setLogLevel } from "@azure/logger"; | |
| import { diag, DiagLogLevel } from "@opentelemetry/api"; | |
| import type { DiagLogger } from "@opentelemetry/api"; | |
| // ============================================================================= | |
| // Step 1: Define your custom DiagLogger implementation | |
| // ============================================================================= | |
| // Implement the 5 required methods: error, warn, info, debug, verbose. | |
| // Route them to whatever logging framework you use (winston, pino, bunyan, etc.) | |
| const myCustomDiagLogger: DiagLogger = { | |
| error: (message, ...args) => { | |
| // Replace with your logging framework, e.g. winston.error(message, ...args) | |
| console.error(`[MY-LOGGER][ERROR] ${message}`, ...args); | |
| }, | |
| warn: (message, ...args) => { | |
| console.warn(`[MY-LOGGER][WARN] ${message}`, ...args); | |
| }, | |
| info: (message, ...args) => { | |
| console.info(`[MY-LOGGER][INFO] ${message}`, ...args); | |
| }, | |
| debug: (message, ...args) => { | |
| console.debug(`[MY-LOGGER][DEBUG] ${message}`, ...args); | |
| }, | |
| verbose: (message, ...args) => { | |
| console.trace(`[MY-LOGGER][VERBOSE] ${message}`, ...args); | |
| }, | |
| }; | |
| // ============================================================================= | |
| // Step 2: Initialize the Azure Monitor distro (this triggers the Logger takeover) | |
| // ============================================================================= | |
| useAzureMonitor({ | |
| azureMonitorExporterOptions: { | |
| connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING, | |
| }, | |
| // ... your other options | |
| }); | |
| // ============================================================================= | |
| // Step 3: Override BOTH loggers AFTER useAzureMonitor() | |
| // ============================================================================= | |
| // 3a. Override the OpenTelemetry diagnostic logger with your custom implementation. | |
| // This replaces the distro's DiagFileConsoleLogger that was set in the | |
| // Logger constructor (src/shared/logging/logger.ts:60-63). | |
| diag.setLogger(myCustomDiagLogger, { logLevel: DiagLogLevel.INFO }); | |
| // 3b. Override the Azure SDK logger output. | |
| // This replaces the distro's AzureLogger.log override | |
| // (src/shared/logging/logger.ts:85-87). | |
| setLogLevel("info"); | |
| AzureLogger.log = (...args) => { | |
| // Route Azure SDK logs into your logging framework | |
| console.log(`[MY-LOGGER][AZURE-SDK]`, ...args); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment