Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/amazonq/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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())
Expand All @@ -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)
Expand Down
31 changes: 25 additions & 6 deletions packages/amazonq/src/lsp/rotatingLogChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<void> {
Expand All @@ -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}`)
}
Expand Down
Loading