求助:Azure Communication Services中AzureLogger的正确使用示例(文档示例不完整引发报错)
I’ve run into this exact snag before, so let’s break down what’s going wrong and fix it with working, tested code examples.
What’s Causing the Error?
Your code mixes ES module syntax (import) with CommonJS (require), and you’re passing the entire @azure/logger module to CallClient instead of the configured AzureLogger instance. The error pops up because the module object doesn’t have the info method that Azure Communication Services (ACS) expects from a logger.
Working ES Module Example (Recommended for Modern Projects)
If you’re using ES modules (standard for most modern JavaScript/TypeScript setups), here’s a complete, functional setup:
import { AzureLogger } from '@azure/logger'; import { CallClient } from '@azure/communication-calling'; // Step 1: Set your desired log level (optional but critical for debugging) AzureLogger.setLogLevel('verbose'); // Step 2: Customize log output (optional—skip this to use default console behavior) AzureLogger.verbose = (...args) => console.log('[VERBOSE]', ...args); AzureLogger.info = (...args) => console.log('[INFO]', ...args); AzureLogger.warning = (...args) => console.warn('[WARNING]', ...args); AzureLogger.error = (...args) => console.error('[ERROR]', ...args); // Step 3: Initialize CallClient with the configured AzureLogger const callClient = new CallClient({ logger: AzureLogger });
Working CommonJS Example
If your project uses CommonJS (Node.js without ES module support), use this instead:
const { AzureLogger } = require('@azure/logger'); const { CallClient } = require('@azure/communication-calling'); // Set log level AzureLogger.setLogLevel('verbose'); // Customize log output (optional) AzureLogger.verbose = (...args) => console.log('[VERBOSE]', ...args); AzureLogger.info = (...args) => console.log('[INFO]', ...args); AzureLogger.warning = (...args) => console.warn('[WARNING]', ...args); AzureLogger.error = (...args) => console.error('[ERROR]', ...args); // Initialize CallClient const callClient = new CallClient({ logger: AzureLogger });
Key Rules to Avoid This Error Again
- Stick to one module system: Don’t mix
importandrequirein the same file—this creates inconsistent object references that break the logger. - Pass
AzureLogger, not the whole module: TheCallClientexpects an object withinfo,verbose,warning, anderrormethods. TheAzureLoggeryou import/require is exactly this object. - Double-check dependencies: Your
@azure/logger@1.0.1version works perfectly with this syntax. If you upgrade later, just confirm the logger’s API remains consistent (it’s been stable for years).
Why the Official Example Works (and Yours Didn’t)
The official example directly modifies the static methods of the AzureLogger object and passes it to CallClient—that’s correct. Your mistake was importing AzureLogger then requiring the entire module and passing that module object instead of the actual AzureLogger instance from your import.
内容的提问来源于stack exchange,提问作者Scott

