refactor(logging): Centralize console logging with debugLogger (#11590)

This commit is contained in:
Abhi
2025-10-21 16:35:22 -04:00
committed by GitHub
parent f5e07d94bd
commit b364f37655
72 changed files with 345 additions and 289 deletions
+3 -2
View File
@@ -34,6 +34,7 @@ import type {
import { IdeClient } from '../ide/ide-client.js';
import { safeLiteralReplace } from '../utils/textUtils.js';
import { EDIT_TOOL_NAME, READ_FILE_TOOL_NAME } from './tool-names.js';
import { debugLogger } from '../utils/debugLogger.js';
export function applyReplacement(
currentContent: string | null,
@@ -255,12 +256,12 @@ class EditToolInvocation implements ToolInvocation<EditToolParams, ToolResult> {
throw error;
}
const errorMsg = error instanceof Error ? error.message : String(error);
console.log(`Error preparing edit: ${errorMsg}`);
debugLogger.log(`Error preparing edit: ${errorMsg}`);
return false;
}
if (editData.error) {
console.log(`Error: ${editData.error.display}`);
debugLogger.log(`Error: ${editData.error.display}`);
return false;
}
+5 -5
View File
@@ -388,7 +388,7 @@ class GrepToolInvocation extends BaseToolInvocation<
});
return this.parseGrepOutput(output, absolutePath);
} catch (gitError: unknown) {
console.debug(
debugLogger.debug(
`GrepLogic: git grep failed: ${getErrorMessage(
gitError,
)}. Falling back...`,
@@ -397,7 +397,7 @@ class GrepToolInvocation extends BaseToolInvocation<
}
// --- Strategy 2: System grep ---
console.debug(
debugLogger.debug(
'GrepLogic: System grep is being considered as fallback strategy.',
);
@@ -494,7 +494,7 @@ class GrepToolInvocation extends BaseToolInvocation<
});
return this.parseGrepOutput(output, absolutePath);
} catch (grepError: unknown) {
console.debug(
debugLogger.debug(
`GrepLogic: System grep failed: ${getErrorMessage(
grepError,
)}. Falling back...`,
@@ -503,7 +503,7 @@ class GrepToolInvocation extends BaseToolInvocation<
}
// --- Strategy 3: Pure JavaScript Fallback ---
console.debug(
debugLogger.debug(
'GrepLogic: Falling back to JavaScript grep implementation.',
);
strategyUsed = 'javascript fallback';
@@ -541,7 +541,7 @@ class GrepToolInvocation extends BaseToolInvocation<
} catch (readError: unknown) {
// Ignore errors like permission denied or file gone during read
if (!isNodeError(readError) || readError.code !== 'ENOENT') {
console.debug(
debugLogger.debug(
`GrepLogic: Could not read/process ${fileAbsolutePath}: ${getErrorMessage(
readError,
)}`,
+26 -18
View File
@@ -41,6 +41,7 @@ import type {
WorkspaceContext,
} from '../utils/workspaceContext.js';
import type { ToolRegistry } from './tool-registry.js';
import { debugLogger } from '../utils/debugLogger.js';
export const MCP_DEFAULT_TIMEOUT_MSEC = 10 * 60 * 1000; // default to 10 minutes
@@ -321,7 +322,7 @@ async function handleAutomaticOAuth(
wwwAuthenticate: string,
): Promise<boolean> {
try {
console.log(`🔐 '${mcpServerName}' requires OAuth authentication`);
debugLogger.log(`🔐 '${mcpServerName}' requires OAuth authentication`);
// Always try to parse the resource metadata URI from the www-authenticate header
let oauthConfig;
@@ -358,13 +359,13 @@ async function handleAutomaticOAuth(
// Perform OAuth authentication
// Pass the server URL for proper discovery
const serverUrl = mcpServerConfig.httpUrl || mcpServerConfig.url;
console.log(
debugLogger.log(
`Starting OAuth authentication for server '${mcpServerName}'...`,
);
const authProvider = new MCPOAuthProvider(new MCPOAuthTokenStorage());
await authProvider.authenticate(mcpServerName, oauthAuthConfig, serverUrl);
console.log(
debugLogger.log(
`OAuth authentication successful for server '${mcpServerName}'`,
);
return true;
@@ -840,12 +841,12 @@ export async function connectToMcpServer(
},
);
if (hasStoredTokens) {
console.log(
debugLogger.log(
`Stored OAuth token for SSE server '${mcpServerName}' was rejected. ` +
`Please re-authenticate using: /mcp auth ${mcpServerName}`,
);
} else {
console.log(
debugLogger.log(
`401 error received for SSE server '${mcpServerName}' without OAuth configuration. ` +
`Please authenticate using: /mcp auth ${mcpServerName}`,
);
@@ -862,7 +863,7 @@ export async function connectToMcpServer(
// If we didn't get the header from the error string, try to get it from the server
if (!wwwAuthenticate && hasNetworkTransport(mcpServerConfig)) {
console.log(
debugLogger.log(
`No www-authenticate header in error, trying to fetch it from server...`,
);
try {
@@ -880,13 +881,13 @@ export async function connectToMcpServer(
if (response.status === 401) {
wwwAuthenticate = response.headers.get('www-authenticate');
if (wwwAuthenticate) {
console.log(
debugLogger.log(
`Found www-authenticate header from server: ${wwwAuthenticate}`,
);
}
}
} catch (fetchError) {
console.debug(
debugLogger.debug(
`Failed to fetch www-authenticate header: ${getErrorMessage(
fetchError,
)}`,
@@ -895,7 +896,7 @@ export async function connectToMcpServer(
}
if (wwwAuthenticate) {
console.log(
debugLogger.log(
`Received 401 with www-authenticate header: ${wwwAuthenticate}`,
);
@@ -907,7 +908,7 @@ export async function connectToMcpServer(
);
if (oauthSuccess) {
// Retry connection with OAuth token
console.log(
debugLogger.log(
`Retrying connection to '${mcpServerName}' with OAuth token...`,
);
@@ -1000,12 +1001,12 @@ export async function connectToMcpServer(
},
);
if (hasStoredTokens) {
console.log(
debugLogger.log(
`Stored OAuth token for SSE server '${mcpServerName}' was rejected. ` +
`Please re-authenticate using: /mcp auth ${mcpServerName}`,
);
} else {
console.log(
debugLogger.log(
`401 error received for SSE server '${mcpServerName}' without OAuth configuration. ` +
`Please authenticate using: /mcp auth ${mcpServerName}`,
);
@@ -1018,7 +1019,9 @@ export async function connectToMcpServer(
}
// For SSE/HTTP servers, try to discover OAuth configuration from the base URL
console.log(`🔍 Attempting OAuth discovery for '${mcpServerName}'...`);
debugLogger.log(
`🔍 Attempting OAuth discovery for '${mcpServerName}'...`,
);
if (hasNetworkTransport(mcpServerConfig)) {
const serverUrl = new URL(
@@ -1030,7 +1033,7 @@ export async function connectToMcpServer(
// Try to discover OAuth configuration from the base URL
const oauthConfig = await OAuthUtils.discoverOAuthConfig(baseUrl);
if (oauthConfig) {
console.log(
debugLogger.log(
`Discovered OAuth configuration from base URL for server '${mcpServerName}'`,
);
@@ -1046,7 +1049,7 @@ export async function connectToMcpServer(
// Pass the server URL for proper discovery
const authServerUrl =
mcpServerConfig.httpUrl || mcpServerConfig.url;
console.log(
debugLogger.log(
`Starting OAuth authentication for server '${mcpServerName}'...`,
);
const authProvider = new MCPOAuthProvider(
@@ -1257,7 +1260,9 @@ export async function createTransport(
if (accessToken) {
hasOAuthConfig = true;
console.log(`Found stored OAuth token for server '${mcpServerName}'`);
debugLogger.log(
`Found stored OAuth token for server '${mcpServerName}'`,
);
}
}
}
@@ -1322,7 +1327,10 @@ export async function createTransport(
if (debugMode) {
transport.stderr!.on('data', (data) => {
const stderrStr = data.toString().trim();
console.debug(`[DEBUG] [MCP STDERR (${mcpServerName})]: `, stderrStr);
debugLogger.debug(
`[DEBUG] [MCP STDERR (${mcpServerName})]: `,
stderrStr,
);
});
}
return transport;
@@ -1340,7 +1348,7 @@ export function isEnabled(
mcpServerConfig: MCPServerConfig,
): boolean {
if (!funcDecl.name) {
console.warn(
debugLogger.warn(
`Discovered a function declaration without a name from MCP server '${mcpServerName}'. Skipping.`,
);
return false;
@@ -733,8 +733,6 @@ Content of file[1]
const invocation = tool.build({ paths: files });
await invocation.execute(new AbortController().signal);
console.log('Execution order:', executionOrder);
// Verify concurrent execution pattern
// In parallel execution: all "start:" events should come before all "end:" events
// In sequential execution: "start:file1", "end:file1", "start:file2", "end:file2", etc.
+2 -1
View File
@@ -19,6 +19,7 @@ import type { Config } from '../config/config.js';
import { fileExists } from '../utils/fileUtils.js';
import { Storage } from '../config/storage.js';
import { GREP_TOOL_NAME } from './tool-names.js';
import { debugLogger } from '../utils/debugLogger.js';
const DEFAULT_TOTAL_MAX_MATCHES = 20000;
@@ -179,7 +180,7 @@ class GrepToolInvocation extends BaseToolInvocation<
const totalMaxMatches = DEFAULT_TOTAL_MAX_MATCHES;
if (this.config.getDebugMode()) {
console.log(`[GrepTool] Total result limit: ${totalMaxMatches}`);
debugLogger.log(`[GrepTool] Total result limit: ${totalMaxMatches}`);
}
for (const searchDir of searchDirectories) {
+3 -2
View File
@@ -39,6 +39,7 @@ import { logSmartEditCorrectionEvent } from '../telemetry/loggers.js';
import { correctPath } from '../utils/pathCorrector.js';
import { EDIT_TOOL_NAME, READ_FILE_TOOL_NAME } from './tool-names.js';
import { debugLogger } from '../utils/debugLogger.js';
interface ReplacementContext {
params: EditToolParams;
currentContent: string;
@@ -616,12 +617,12 @@ class EditToolInvocation implements ToolInvocation<EditToolParams, ToolResult> {
throw error;
}
const errorMsg = error instanceof Error ? error.message : String(error);
console.log(`Error preparing edit: ${errorMsg}`);
debugLogger.log(`Error preparing edit: ${errorMsg}`);
return false;
}
if (editData.error) {
console.log(`Error: ${editData.error.display}`);
debugLogger.log(`Error: ${editData.error.display}`);
return false;
}
+3 -2
View File
@@ -21,6 +21,7 @@ import { parse } from 'shell-quote';
import { ToolErrorType } from './tool-error.js';
import { safeJsonStringify } from '../utils/safeJsonStringify.js';
import type { EventEmitter } from 'node:events';
import { debugLogger } from '../utils/debugLogger.js';
type ToolParams = Record<string, unknown>;
@@ -187,7 +188,7 @@ export class ToolRegistry {
tool = tool.asFullyQualifiedTool();
} else {
// Decide on behavior: throw error, log warning, or allow overwrite
console.warn(
debugLogger.warn(
`Tool with name "${tool.name}" is already registered. Overwriting.`,
);
}
@@ -379,7 +380,7 @@ export class ToolRegistry {
// register each function as a tool
for (const func of functions) {
if (!func.name) {
console.warn('Discovered a tool with no name. Skipping.');
debugLogger.warn('Discovered a tool with no name. Skipping.');
continue;
}
const parameters =
+3 -2
View File
@@ -32,6 +32,7 @@ import {
WebFetchFallbackAttemptEvent,
} from '../telemetry/index.js';
import { WEB_FETCH_TOOL_NAME } from './tool-names.js';
import { debugLogger } from '../utils/debugLogger.js';
const URL_FETCH_TIMEOUT_MS = 10000;
const MAX_CONTENT_LENGTH = 100000;
@@ -274,7 +275,7 @@ ${textContent}
DEFAULT_GEMINI_FLASH_MODEL,
);
console.debug(
debugLogger.debug(
`[WebFetchTool] Full response for prompt "${userPrompt.substring(
0,
50,
@@ -367,7 +368,7 @@ ${sourceListFormatted.join('\n')}`;
const llmContent = responseText;
console.debug(
debugLogger.debug(
`[WebFetchTool] Formatted tool response for prompt "${userPrompt}:\n\n":`,
llmContent,
);