diff --git a/sdk/cosmosdb/cosmos/package.json b/sdk/cosmosdb/cosmos/package.json index 85563e4b65e2..fdfdddf1a90e 100644 --- a/sdk/cosmosdb/cosmos/package.json +++ b/sdk/cosmosdb/cosmos/package.json @@ -101,6 +101,7 @@ "@azure/dev-tool": "^1.0.0", "@azure/eslint-plugin-azure-sdk": "^3.0.0", "@azure/identity": "^2.0.1", + "@azure/logger": "^1.0.0", "@microsoft/api-extractor": "^7.18.11", "@rollup/plugin-json": "^4.0.0", "@rollup/plugin-multi-entry": "^3.0.0", diff --git a/sdk/cosmosdb/cosmos/src/ClientContext.ts b/sdk/cosmosdb/cosmos/src/ClientContext.ts index ddd8aeadd5d8..d50adc4a265e 100644 --- a/sdk/cosmosdb/cosmos/src/ClientContext.ts +++ b/sdk/cosmosdb/cosmos/src/ClientContext.ts @@ -11,7 +11,6 @@ import { PartitionKeyRange } from "./client/Container/PartitionKeyRange"; import { Resource } from "./client/Resource"; import { Constants, HTTPMethod, OperationType, ResourceType } from "./common/constants"; import { getIdFromLink, getPathFromLink, parseLink } from "./common/helper"; -import { logger } from "./common/logger"; import { StatusCodes, SubStatusCodes } from "./common/statusCodes"; import { CosmosClientOptions } from "./CosmosClientOptions"; import { ConnectionPolicy, ConsistencyLevel, DatabaseAccount, PartitionKey } from "./documents"; @@ -30,9 +29,9 @@ import { SessionContainer } from "./session/sessionContainer"; import { SessionContext } from "./session/SessionContext"; import { BulkOptions } from "./utils/batch"; import { sanitizeEndpoint } from "./utils/checkURL"; +import { AzureLogger, createClientLogger } from "@azure/logger"; -/** @hidden */ -const log = logger("ClientContext"); +const logger: AzureLogger = createClientLogger("ClientContext"); const QueryJsonContentType = "application/query+json"; @@ -177,16 +176,16 @@ export class ClientContext { } } this.applySessionToken(request); - log.info( + logger.info( "query " + requestId + " started" + (request.partitionKeyRangeId ? " pkrid: " + request.partitionKeyRangeId : "") ); - log.silly(request); + logger.verbose(request); const start = Date.now(); const response = await executeRequest(request); - log.info("query " + requestId + " finished - " + (Date.now() - start) + "ms"); + logger.info("query " + requestId + " finished - " + (Date.now() - start) + "ms"); this.captureSessionToken(undefined, path, OperationType.Query, response.headers); return this.processQueryFeedResponse(response, !!query, resultFn); } diff --git a/sdk/cosmosdb/cosmos/src/common/logger.ts b/sdk/cosmosdb/cosmos/src/common/logger.ts index 8c8bf382bfcf..158b37ab085b 100644 --- a/sdk/cosmosdb/cosmos/src/common/logger.ts +++ b/sdk/cosmosdb/cosmos/src/common/logger.ts @@ -1,75 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -import debugLib from "debug"; +import { createClientLogger, AzureLogger } from "@azure/logger"; -/** @hidden */ -let cosmosLevelFilter = "warn|error"; - -if (typeof process !== "undefined" && process.env && process.env.COSMOS_LOG_LEVEL) { - cosmosLevelFilter = process.env.COSMOS_LOG_LEVEL; -} - -/** @hidden */ -const cosmosDebug = debugLib("cosmos"); - -/** @hidden */ -type logLevel = "silly" | "debug" | "info" | "warn" | "error"; - -/** @hidden */ -const levelLogger = (namespaceLogger: debugLib.Debugger, level: logLevel) => { - return (message: string | { [key: string]: any }) => { - if (cosmosLevelFilter.includes(level)) { - namespaceLogger("[" + new Date().toISOString() + "][" + level + "]: %o", message); - } - }; -}; - -/** @hidden */ -export const logger = ( - namespace: string -): { - silly: ( - message: - | string - | { - [key: string]: any; - } - ) => void; - debug: ( - message: - | string - | { - [key: string]: any; - } - ) => void; - info: ( - message: - | string - | { - [key: string]: any; - } - ) => void; - warn: ( - message: - | string - | { - [key: string]: any; - } - ) => void; - error: ( - message: - | string - | { - [key: string]: any; - } - ) => void; -} => { - const namespaceLogger = cosmosDebug.extend(namespace); - return { - silly: levelLogger(namespaceLogger, "silly"), - debug: levelLogger(namespaceLogger, "debug"), - info: levelLogger(namespaceLogger, "info"), - warn: levelLogger(namespaceLogger, "warn"), - error: levelLogger(namespaceLogger, "error") - }; -}; +/** + * The \@azure/logger configuration for this package. + */ +export const defaultLogger: AzureLogger = createClientLogger("cosmosdb"); diff --git a/sdk/cosmosdb/cosmos/src/queryExecutionContext/defaultQueryExecutionContext.ts b/sdk/cosmosdb/cosmos/src/queryExecutionContext/defaultQueryExecutionContext.ts index f3590cd428d1..d519b7fc3c5a 100644 --- a/sdk/cosmosdb/cosmos/src/queryExecutionContext/defaultQueryExecutionContext.ts +++ b/sdk/cosmosdb/cosmos/src/queryExecutionContext/defaultQueryExecutionContext.ts @@ -1,15 +1,13 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. +import { AzureLogger, createClientLogger } from "@azure/logger"; import { Constants } from "../common"; -import { logger } from "../common/logger"; import { ClientSideMetrics, QueryMetrics } from "../queryMetrics"; import { FeedOptions, Response } from "../request"; import { getInitialHeader } from "./headerUtils"; import { ExecutionContext } from "./index"; -/** @hidden */ -const log = logger("defaultQueryExecutionContext"); - +const logger: AzureLogger = createClientLogger("ClientContext"); /** @hidden */ export type FetchFunctionCallback = (options: FeedOptions) => Promise>; @@ -133,11 +131,11 @@ export class DefaultQueryExecutionContext implements ExecutionContext { try { let p: Promise>; if (this.nextFetchFunction !== undefined) { - log.debug("using prefetch"); + logger.verbose("using prefetch"); p = this.nextFetchFunction; this.nextFetchFunction = undefined; } else { - log.debug("using fresh fetch"); + logger.verbose("using fresh fetch"); p = this.fetchFunctions[this.currentPartitionIndex](this.options); } const response = await p; diff --git a/sdk/cosmosdb/cosmos/src/queryExecutionContext/parallelQueryExecutionContextBase.ts b/sdk/cosmosdb/cosmos/src/queryExecutionContext/parallelQueryExecutionContextBase.ts index 66515d83ab6e..01758667ad0b 100644 --- a/sdk/cosmosdb/cosmos/src/queryExecutionContext/parallelQueryExecutionContextBase.ts +++ b/sdk/cosmosdb/cosmos/src/queryExecutionContext/parallelQueryExecutionContextBase.ts @@ -3,7 +3,7 @@ import PriorityQueue from "priorityqueuejs"; import semaphore from "semaphore"; import { ClientContext } from "../ClientContext"; -import { logger } from "../common/logger"; +import { AzureLogger, createClientLogger } from "@azure/logger"; import { StatusCodes, SubStatusCodes } from "../common/statusCodes"; import { FeedOptions, Response } from "../request"; import { PartitionedQueryExecutionInfo } from "../request/ErrorResponse"; @@ -16,7 +16,7 @@ import { getInitialHeader, mergeHeaders } from "./headerUtils"; import { SqlQuerySpec } from "./SqlQuerySpec"; /** @hidden */ -const log = logger("parallelQueryExecutionContextBase"); +const logger: AzureLogger = createClientLogger("parallelQueryExecutionContextBase"); /** @hidden */ export enum ParallelQueryExecutionContextBaseStates { @@ -92,7 +92,7 @@ export abstract class ParallelQueryExecutionContextBase implements ExecutionCont ? targetPartitionRanges.length : Math.min(options.maxDegreeOfParallelism, targetPartitionRanges.length); - log.info( + logger.info( "Query starting against " + targetPartitionRanges.length + " ranges with parallelism of " + diff --git a/sdk/cosmosdb/cosmos/src/request/RequestHandler.ts b/sdk/cosmosdb/cosmos/src/request/RequestHandler.ts index 6303533e0e16..34ecceed1163 100644 --- a/sdk/cosmosdb/cosmos/src/request/RequestHandler.ts +++ b/sdk/cosmosdb/cosmos/src/request/RequestHandler.ts @@ -8,7 +8,6 @@ import { } from "@azure/core-rest-pipeline"; import { trimSlashes } from "../common"; import { Constants } from "../common/constants"; -import { logger } from "../common/logger"; import { executePlugins, PluginOn } from "../plugins/Plugin"; import * as RetryUtility from "../retry/retryUtility"; import { defaultHttpAgent, defaultHttpsAgent } from "./defaultAgent"; @@ -19,9 +18,9 @@ import { Response as CosmosResponse } from "./Response"; import { TimeoutError } from "./TimeoutError"; import { URL } from "../utils/url"; import { getCachedDefaultHttpClient } from "../utils/cachedClient"; +import { AzureLogger, createClientLogger } from "@azure/logger"; -/** @hidden */ -const log = logger("RequestHandler"); +const logger: AzureLogger = createClientLogger("RequestHandler"); async function executeRequest(requestContext: RequestContext): Promise> { return executePlugins(requestContext, httpRequest, PluginOn.request); @@ -112,7 +111,7 @@ async function httpRequest( if (response.status >= 400) { const errorResponse: ErrorResponse = new Error(result.message); - log.warn( + logger.warning( response.status + " " + requestContext.endpoint +