編輯

共用方式為


How to log with Azure SDK client libraries

To diagnose an unexpected issue or to understand what any Azure SDK client library for JavaScript is doing, enable logging in your app. You can do this with either of the methods below:

  • Use AZURE_LOG_LEVEL=verbose environment variable to turn on logging.
  • Use @azure/logger package in your source code.

Valid log levels include verbose, info, warning, and error.

Prerequisites

  • An Azure subscription - Create one for free
  • Node.js LTS.
  • Optional, authentication tool such as Azure CLI used for authentication in a local development environment, create the necessary context by signing in with the Azure CLI.

Debug with environment variable

A simple way to use the environment variable is to start the application with the environment variable.

AZURE_LOG_LEVEL=verbose node index.js

Debug with logger package in source code

The following code sample uses the @azure/logger package to debug the Azure SDK client libraries.

  1. Create index.js with the following code:

    import { 
        ChainedTokenCredential, 
        ManagedIdentityCredential, 
        AzureCliCredential 
    } from "@azure/identity";
    import { BlobServiceClient } from "@azure/storage-blob";
    
    // Turn on debugging    
    const { AzureLogger, setLogLevel } = require("@azure/logger");
    setLogLevel("verbose");
    AzureLogger.log = (...args) => {
        console.log(...args);
    };
    
    const credential = new ChainedTokenCredential(
        new ManagedIdentityCredential({ clientId: "<YOUR_CLIENT_ID>" }),
        new AzureCliCredential()
    );
    
    const blobServiceClient = new BlobServiceClient(
        "https://dinaberrystor.blob.core.windows.net",
        credential
    );
    // get container properties
    const containerClient = blobServiceClient.getContainerClient("<CONTAINER_NAME>");
    
    async function main(){
        const properties = await containerClient.getProperties();
        console.log(properties);
    }
    
    main().catch((err) => {
        console.error("Error running sample:", err.message);
    });
    
  2. Install the npm dependencies.

    npm install @azure/identity @azure/storage-blob
    
  3. Sign into your Azure subscription in your local environment with Azure CLI:

    az login
    
  4. Run the app:

    node index.js
    
  5. Find the successful credential: getToken() => SUCCESS.

    ...
    azure:identity:info AzureCliCredential => getToken() => SUCCESS. Scopes: https://storage.azure.com/.default.
    azure:identity:info ChainedTokenCredential => getToken() => Result for AzureCliCredential: SUCCESS. Scopes: https://storage.azure.com/.defau
    lt.