本文介绍如何在适用于 JavaScript 的 Azure SDK 库中配置日志记录。 启用日志记录有助于诊断身份验证问题、排查凭据链问题,并深入了解 SDK作。
若要启用日志记录,可以使用以下任一选项:
- 设置
AZURE_LOG_LEVEL=verbose环境变量以打开日志记录。 - 在源代码中使用
@azure/logger包。
有效的日志级别包括verbose、info和warningerror。
注释
本文中显示的 Azure 存储代码假定存储资源已配置相应的Microsoft Entra 角色。 了解详细信息:使用 Microsoft Entra ID 授权访问 Blob 存储。
先决条件
- Azure 订阅: 免费创建一个
- Node.js LTS
- (可选)开发人员工具,例如用于在本地开发环境中进行身份验证的 Azure CLI 。 若要创建必要的上下文,请使用 Azure CLI 登录。
使用环境变量启用日志记录
使用环境变量启动应用程序,以简单方式启用日志记录。
AZURE_LOG_LEVEL=verbose node index.js
设置环境变量。
还可以将环境变量添加到 .env 项目根目录中的文件。 创建一个名为 .env 并添加以下内容的文件。
AZURE_LOG_LEVEL=verbose
AZURE_CLIENT_ID=<YOUR_CLIENT_ID>
AZURE_STORAGE_ACCOUNT_NAME=<YOUR_STORAGE_ACCOUNT_NAME>
AZURE_STORAGE_CONTAINER_NAME=<YOUR_STORAGE_CONTAINER_NAME>
在源代码中使用日志记录器包启用日志记录
以下代码示例使用 @azure/logger 包调试 Azure SDK 客户端库。
为特定服务配置日志记录
除了设置全局日志级别之外,还可以直接在代码中为特定 Azure 服务配置不同的日志级别:
// Import service-specific setLogLevel functions
import { setLogLevel as setIdentityLogLevel } from "@azure/identity";
import { setLogLevel as setStorageLogLevel } from "@azure/storage-blob";
// Set different log levels for different services
setIdentityLogLevel("verbose"); // Detailed logs for identity operations
setStorageLogLevel("warning"); // Only warnings and errors for storage operations
使用此方法,可以在同一应用程序中使用多个 Azure 服务时对日志记录详细程度进行精细控制。
使用以下代码创建
index.js。import { ChainedTokenCredential, ManagedIdentityCredential, AzureCliCredential } from "@azure/identity"; import { BlobServiceClient } from "@azure/storage-blob"; import { AzureLogger, setLogLevel } from "@azure/logger"; // Check required environment variables if (!process.env.AZURE_STORAGE_ACCOUNT_NAME) { throw new Error("AZURE_STORAGE_ACCOUNT_NAME environment variable is required"); } if (!process.env.AZURE_STORAGE_CONTAINER_NAME) { throw new Error("AZURE_STORAGE_CONTAINER_NAME environment variable is required"); } // Client ID is optional and only used in production environments // No need to check for its existence // Turn on debugging for all Azure SDKs setLogLevel("verbose"); // Configure the logger to use console. AzureLogger.log = (...args)=> { console.log(...args); }; const credential = new ChainedTokenCredential( new ManagedIdentityCredential({ clientId: process.env.AZURE_CLIENT_ID }), new AzureCliCredential() ); const blobServiceClient = new BlobServiceClient( `https://${process.env.AZURE_STORAGE_ACCOUNT_NAME}.blob.core.windows.net`, credential ); // get container properties const containerClient = blobServiceClient.getContainerClient(process.env.AZURE_STORAGE_CONTAINER_NAME); async function main() { try { const properties = await containerClient.getProperties(); console.log(properties); } catch (err) { const error = err; console.error("Error retrieving container properties:", error.message); throw error; } } main().catch((err) => { console.error("Error running sample:", err.message); process.exit(1); });创建项目并安装 npm 依赖项。
npm init -y npm pkg set type=module npm install @azure/identity @azure/storage-blob @azure/logger使用 Azure CLI 登录到本地环境中的 Azure 订阅。
az login使用环境变量文件运行应用。 Node.js 20.6.0 中引入了此选项
--env-file。node --env-file .env index.js在输出中找到成功的凭据。
ChainedTokenCredential允许代码在身份验证方法之间无缝切换,首先尝试ManagedIdentityCredential(对于 Azure 应用服务等生产环境),然后回退到AzureCliCredential(用于本地开发),并显示凭据成功的日志。
先决条件
- Azure 订阅: 免费创建一个
- Node.js LTS
- TypeScript
- (可选)身份验证工具,例如用于在本地开发环境中进行身份验证的 Azure CLI 。 若要创建必要的上下文,请使用 Azure CLI 登录。
使用环境变量启用日志记录
使用环境变量启动应用程序,以简单方式启用日志记录。
AZURE_LOG_LEVEL=verbose node index.js
设置环境变量。
还可以将环境变量添加到 .env 项目根目录中的文件。 创建一个名为 .env 并添加以下内容的文件。
AZURE_LOG_LEVEL=verbose
AZURE_CLIENT_ID=<YOUR_CLIENT_ID>
AZURE_STORAGE_ACCOUNT_NAME=<YOUR_STORAGE_ACCOUNT_NAME>
AZURE_STORAGE_CONTAINER_NAME=<YOUR_STORAGE_CONTAINER_NAME>
在源代码中使用日志记录器包启用日志功能
以下代码示例使用 @azure/logger 包调试 Azure SDK 客户端库。
为特定服务配置日志记录
除了设置全局日志级别之外,还可以直接在代码中为特定 Azure 服务配置不同的日志级别:
// Import service-specific setLogLevel functions
import { setLogLevel as setIdentityLogLevel } from "@azure/identity";
import { setLogLevel as setStorageLogLevel } from "@azure/storage-blob";
// Set different log levels for different services
setIdentityLogLevel("verbose"); // Detailed logs for identity operations
setStorageLogLevel("warning"); // Only warnings and errors for storage operations
使用此方法,可以在同一应用程序中使用多个 Azure 服务时对日志记录详细程度进行精细控制。
使用以下代码创建
index.ts。import { ChainedTokenCredential, ManagedIdentityCredential, AzureCliCredential } from "@azure/identity"; import { BlobServiceClient, ContainerGetPropertiesResponse } from "@azure/storage-blob"; import { AzureLogger, setLogLevel } from "@azure/logger"; // Check required environment variables if (!process.env.AZURE_STORAGE_ACCOUNT_NAME) { throw new Error("AZURE_STORAGE_ACCOUNT_NAME environment variable is required"); } if (!process.env.AZURE_STORAGE_CONTAINER_NAME) { throw new Error("AZURE_STORAGE_CONTAINER_NAME environment variable is required"); } // Client ID is optional and only used in production environments // No need to check for its existence // Turn on debugging for all Azure SDKs setLogLevel("verbose"); // Configure the logger to use console.log with TypeScript type safety AzureLogger.log = (...args: unknown[]): void => { console.log(...args); }; const credential = new ChainedTokenCredential( new ManagedIdentityCredential({ clientId: process.env.AZURE_CLIENT_ID }), new AzureCliCredential() ); const blobServiceClient = new BlobServiceClient( `https://${process.env.AZURE_STORAGE_ACCOUNT_NAME}.blob.core.windows.net`, credential ); // get container properties const containerClient = blobServiceClient.getContainerClient(process.env.AZURE_STORAGE_CONTAINER_NAME); async function main(): Promise<void> { try { const properties: ContainerGetPropertiesResponse = await containerClient.getProperties(); console.log(properties); } catch (err) { const error = err as Error; console.error("Error retrieving container properties:", error.message); throw error; } } main().catch((err: Error) => { console.error("Error running sample:", err.message); process.exit(1); });创建项目并安装 npm 依赖项。
npm init -y npm pkg set type=module npm install @azure/identity @azure/storage-blob @types/node @azure/logger使用 Azure CLI 登录到本地环境中的 Azure 订阅。
az login生成应用程序。
tsc使用环境变量文件运行应用。 Node.js 20.6.0 中引入了此选项
--env-file。node --env-file .env index.js在输出中找到成功的凭据 - 允许
ChainedTokenCredential代码在身份验证方法之间无缝切换,首先尝试ManagedIdentityCredential(对于 Azure 应用服务等生产环境),然后回退到AzureCliCredential(用于本地开发),日志显示哪些凭据成功。