Skip to content

Commit 5e256b9

Browse files
authored
feat(amazonq): added logs of toolkit to the same disk file too #7701
feat(amazonq): added logs of toolkit to the same disk file too
2 parents e49f15f + 5dbbbd7 commit 5e256b9

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

packages/amazonq/src/extension.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import { registerCommands } from './commands'
4545
import { focusAmazonQPanel } from 'aws-core-vscode/codewhispererChat'
4646
import { activate as activateAmazonqLsp } from './lsp/activation'
4747
import { hasGlibcPatch } from './lsp/client'
48+
import { RotatingLogChannel } from './lsp/rotatingLogChannel'
4849

4950
export const amazonQContextPrefix = 'amazonq'
5051

@@ -103,7 +104,12 @@ export async function activateAmazonQCommon(context: vscode.ExtensionContext, is
103104
globals.manifestPaths.endpoints = context.asAbsolutePath(join('resources', 'endpoints.json'))
104105
globals.regionProvider = RegionProvider.fromEndpointsProvider(makeEndpointsProvider())
105106

106-
const qLogChannel = vscode.window.createOutputChannel('Amazon Q Logs', { log: true })
107+
// Create rotating log channel for all Amazon Q logs
108+
const qLogChannel = new RotatingLogChannel(
109+
'Amazon Q Logs',
110+
context,
111+
vscode.window.createOutputChannel('Amazon Q Logs', { log: true })
112+
)
107113
await activateLogger(context, amazonQContextPrefix, qLogChannel)
108114
globals.logOutputChannel = qLogChannel
109115
globals.loginManager = new LoginManager(globals.awsContext, new CredentialsStore())
@@ -112,6 +118,8 @@ export async function activateAmazonQCommon(context: vscode.ExtensionContext, is
112118
getLogger().error('fs.init: invalid env vars found: %O', homeDirLogs)
113119
}
114120

121+
getLogger().info('Rotating logger has been setup')
122+
115123
await activateTelemetry(context, globals.awsContext, Settings.instance, 'Amazon Q For VS Code')
116124

117125
await initializeAuth(globals.loginManager)

packages/amazonq/src/lsp/rotatingLogChannel.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ export class RotatingLogChannel implements vscode.LogOutputChannel {
1818
private readonly MAX_FILE_SIZE = 5 * 1024 * 1024 // 5MB
1919
// eslint-disable-next-line @typescript-eslint/naming-convention
2020
private readonly MAX_LOG_FILES = 4
21+
private static currentLogPath: string | undefined
22+
23+
private static generateNewLogPath(logDir: string): string {
24+
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').replace('T', '-').replace('Z', '')
25+
return path.join(logDir, `amazonq-lsp-${timestamp}.log`)
26+
}
2127

2228
constructor(
2329
public readonly name: string,
@@ -61,13 +67,19 @@ export class RotatingLogChannel implements vscode.LogOutputChannel {
6167
}
6268

6369
private getLogFilePath(): string {
70+
// If we already have a path, reuse it
71+
if (RotatingLogChannel.currentLogPath) {
72+
return RotatingLogChannel.currentLogPath
73+
}
74+
6475
const logDir = this.extensionContext.storageUri?.fsPath
6576
if (!logDir) {
6677
throw new Error('No storage URI available')
6778
}
6879

69-
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').replace('T', '-').replace('Z', '')
70-
return path.join(logDir, `amazonq-lsp-${timestamp}.log`)
80+
// Generate initial path
81+
RotatingLogChannel.currentLogPath = RotatingLogChannel.generateNewLogPath(logDir)
82+
return RotatingLogChannel.currentLogPath
7183
}
7284

7385
private async rotateLog(): Promise<void> {
@@ -77,15 +89,22 @@ export class RotatingLogChannel implements vscode.LogOutputChannel {
7789
this.fileStream.end()
7890
}
7991

80-
// Create new log file
81-
const newLogPath = this.getLogFilePath()
82-
this.fileStream = fs.createWriteStream(newLogPath, { flags: 'a' })
92+
const logDir = this.extensionContext.storageUri?.fsPath
93+
if (!logDir) {
94+
throw new Error('No storage URI available')
95+
}
96+
97+
// Generate new path directly
98+
RotatingLogChannel.currentLogPath = RotatingLogChannel.generateNewLogPath(logDir)
99+
100+
// Create new log file with new path
101+
this.fileStream = fs.createWriteStream(RotatingLogChannel.currentLogPath, { flags: 'a' })
83102
this.currentFileSize = 0
84103

85104
// Clean up old files
86105
await this.cleanupOldLogs()
87106

88-
this.logger.info(`Created new log file: ${newLogPath}`)
107+
this.logger.info(`Created new log file: ${RotatingLogChannel.currentLogPath}`)
89108
} catch (err) {
90109
this.logger.error(`Failed to rotate log file: ${err}`)
91110
}

0 commit comments

Comments
 (0)