diff --git a/packages/amazonq/src/extension.ts b/packages/amazonq/src/extension.ts index 53d7cd88037..1e26724ff61 100644 --- a/packages/amazonq/src/extension.ts +++ b/packages/amazonq/src/extension.ts @@ -45,6 +45,7 @@ import { registerCommands } from './commands' import { focusAmazonQPanel } from 'aws-core-vscode/codewhispererChat' import { activate as activateAmazonqLsp } from './lsp/activation' import { hasGlibcPatch } from './lsp/client' +import { RotatingLogChannel } from './lsp/rotatingLogChannel' export const amazonQContextPrefix = 'amazonq' @@ -103,7 +104,12 @@ export async function activateAmazonQCommon(context: vscode.ExtensionContext, is globals.manifestPaths.endpoints = context.asAbsolutePath(join('resources', 'endpoints.json')) globals.regionProvider = RegionProvider.fromEndpointsProvider(makeEndpointsProvider()) - const qLogChannel = vscode.window.createOutputChannel('Amazon Q Logs', { log: true }) + // Create rotating log channel for all Amazon Q logs + const qLogChannel = new RotatingLogChannel( + 'Amazon Q Logs', + context, + vscode.window.createOutputChannel('Amazon Q Logs', { log: true }) + ) await activateLogger(context, amazonQContextPrefix, qLogChannel) globals.logOutputChannel = qLogChannel globals.loginManager = new LoginManager(globals.awsContext, new CredentialsStore()) @@ -112,6 +118,8 @@ export async function activateAmazonQCommon(context: vscode.ExtensionContext, is getLogger().error('fs.init: invalid env vars found: %O', homeDirLogs) } + getLogger().info('Rotating logger has been setup') + await activateTelemetry(context, globals.awsContext, Settings.instance, 'Amazon Q For VS Code') await initializeAuth(globals.loginManager) diff --git a/packages/amazonq/src/lsp/rotatingLogChannel.ts b/packages/amazonq/src/lsp/rotatingLogChannel.ts index 24d6e110771..a9ee36ed30c 100644 --- a/packages/amazonq/src/lsp/rotatingLogChannel.ts +++ b/packages/amazonq/src/lsp/rotatingLogChannel.ts @@ -18,6 +18,12 @@ export class RotatingLogChannel implements vscode.LogOutputChannel { private readonly MAX_FILE_SIZE = 5 * 1024 * 1024 // 5MB // eslint-disable-next-line @typescript-eslint/naming-convention private readonly MAX_LOG_FILES = 4 + private static currentLogPath: string | undefined + + private static generateNewLogPath(logDir: string): string { + const timestamp = new Date().toISOString().replace(/[:.]/g, '-').replace('T', '-').replace('Z', '') + return path.join(logDir, `amazonq-lsp-${timestamp}.log`) + } constructor( public readonly name: string, @@ -61,13 +67,19 @@ export class RotatingLogChannel implements vscode.LogOutputChannel { } private getLogFilePath(): string { + // If we already have a path, reuse it + if (RotatingLogChannel.currentLogPath) { + return RotatingLogChannel.currentLogPath + } + const logDir = this.extensionContext.storageUri?.fsPath if (!logDir) { throw new Error('No storage URI available') } - const timestamp = new Date().toISOString().replace(/[:.]/g, '-').replace('T', '-').replace('Z', '') - return path.join(logDir, `amazonq-lsp-${timestamp}.log`) + // Generate initial path + RotatingLogChannel.currentLogPath = RotatingLogChannel.generateNewLogPath(logDir) + return RotatingLogChannel.currentLogPath } private async rotateLog(): Promise { @@ -77,15 +89,22 @@ export class RotatingLogChannel implements vscode.LogOutputChannel { this.fileStream.end() } - // Create new log file - const newLogPath = this.getLogFilePath() - this.fileStream = fs.createWriteStream(newLogPath, { flags: 'a' }) + const logDir = this.extensionContext.storageUri?.fsPath + if (!logDir) { + throw new Error('No storage URI available') + } + + // Generate new path directly + RotatingLogChannel.currentLogPath = RotatingLogChannel.generateNewLogPath(logDir) + + // Create new log file with new path + this.fileStream = fs.createWriteStream(RotatingLogChannel.currentLogPath, { flags: 'a' }) this.currentFileSize = 0 // Clean up old files await this.cleanupOldLogs() - this.logger.info(`Created new log file: ${newLogPath}`) + this.logger.info(`Created new log file: ${RotatingLogChannel.currentLogPath}`) } catch (err) { this.logger.error(`Failed to rotate log file: ${err}`) }