-
Notifications
You must be signed in to change notification settings - Fork 746
feat(amazonq): write logs to disk with rotation & cleanup #7691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| /*! | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import * as vscode from 'vscode' | ||
| import * as path from 'path' | ||
| import * as fs from 'fs' // eslint-disable-line no-restricted-imports | ||
| import { getLogger } from 'aws-core-vscode/shared' | ||
|
|
||
| export class RotatingLogChannel implements vscode.LogOutputChannel { | ||
| private fileStream: fs.WriteStream | undefined | ||
| private originalChannel: vscode.LogOutputChannel | ||
| private logger = getLogger('amazonqLsp') | ||
| private _logLevel: vscode.LogLevel = vscode.LogLevel.Info | ||
| private currentFileSize = 0 | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| private readonly MAX_FILE_SIZE = 5 * 1024 * 1024 // 5MB | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| private readonly MAX_LOG_FILES = 4 | ||
|
|
||
| constructor( | ||
| public readonly name: string, | ||
| private readonly extensionContext: vscode.ExtensionContext, | ||
| outputChannel: vscode.LogOutputChannel | ||
| ) { | ||
| this.originalChannel = outputChannel | ||
| this.initFileStream() | ||
| } | ||
|
|
||
| private async cleanupOldLogs(): Promise<void> { | ||
| try { | ||
| const logDir = this.extensionContext.storageUri?.fsPath | ||
| if (!logDir) { | ||
| return | ||
| } | ||
|
|
||
| // Get all log files | ||
| const files = await fs.promises.readdir(logDir) | ||
| const logFiles = files | ||
| .filter((f) => f.startsWith('amazonq-lsp-') && f.endsWith('.log')) | ||
| .map((f) => ({ | ||
| name: f, | ||
| path: path.join(logDir, f), | ||
| time: fs.statSync(path.join(logDir, f)).mtime.getTime(), | ||
| })) | ||
| .sort((a, b) => b.time - a.time) // Sort newest to oldest | ||
|
|
||
| // Remove all but the most recent MAX_LOG_FILES files | ||
| for (const file of logFiles.slice(this.MAX_LOG_FILES - 1)) { | ||
| try { | ||
| await fs.promises.unlink(file.path) | ||
| this.logger.debug(`Removed old log file: ${file.path}`) | ||
| } catch (err) { | ||
| this.logger.error(`Failed to remove old log file ${file.path}: ${err}`) | ||
| } | ||
| } | ||
| } catch (err) { | ||
| this.logger.error(`Failed to cleanup old logs: ${err}`) | ||
| } | ||
| } | ||
|
|
||
| private getLogFilePath(): string { | ||
| 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`) | ||
| } | ||
|
|
||
| private async rotateLog(): Promise<void> { | ||
| try { | ||
| // Close current stream | ||
| if (this.fileStream) { | ||
| this.fileStream.end() | ||
| } | ||
|
|
||
| // Create new log file | ||
| const newLogPath = this.getLogFilePath() | ||
| this.fileStream = fs.createWriteStream(newLogPath, { flags: 'a' }) | ||
| this.currentFileSize = 0 | ||
|
|
||
| // Clean up old files | ||
| await this.cleanupOldLogs() | ||
|
|
||
| this.logger.info(`Created new log file: ${newLogPath}`) | ||
| } catch (err) { | ||
| this.logger.error(`Failed to rotate log file: ${err}`) | ||
| } | ||
| } | ||
|
|
||
| private initFileStream() { | ||
| try { | ||
| const logDir = this.extensionContext.storageUri | ||
| if (!logDir) { | ||
| this.logger.error('Failed to get storage URI for logs') | ||
| return | ||
| } | ||
|
|
||
| // Ensure directory exists | ||
| if (!fs.existsSync(logDir.fsPath)) { | ||
| fs.mkdirSync(logDir.fsPath, { recursive: true }) | ||
| } | ||
|
|
||
| const logPath = this.getLogFilePath() | ||
| this.fileStream = fs.createWriteStream(logPath, { flags: 'a' }) | ||
| this.currentFileSize = 0 | ||
| this.logger.info(`Logging to file: ${logPath}`) | ||
| } catch (err) { | ||
| this.logger.error(`Failed to create log file: ${err}`) | ||
| } | ||
| } | ||
|
|
||
| get logLevel(): vscode.LogLevel { | ||
| return this._logLevel | ||
| } | ||
|
|
||
| get onDidChangeLogLevel(): vscode.Event<vscode.LogLevel> { | ||
| return this.originalChannel.onDidChangeLogLevel | ||
| } | ||
|
|
||
| trace(message: string, ...args: any[]): void { | ||
| this.originalChannel.trace(message, ...args) | ||
| this.writeToFile(`[TRACE] ${message}`) | ||
| } | ||
|
|
||
| debug(message: string, ...args: any[]): void { | ||
| this.originalChannel.debug(message, ...args) | ||
| this.writeToFile(`[DEBUG] ${message}`) | ||
| } | ||
|
|
||
| info(message: string, ...args: any[]): void { | ||
| this.originalChannel.info(message, ...args) | ||
| this.writeToFile(`[INFO] ${message}`) | ||
| } | ||
|
|
||
| warn(message: string, ...args: any[]): void { | ||
| this.originalChannel.warn(message, ...args) | ||
| this.writeToFile(`[WARN] ${message}`) | ||
| } | ||
|
|
||
| error(message: string | Error, ...args: any[]): void { | ||
| this.originalChannel.error(message, ...args) | ||
| this.writeToFile(`[ERROR] ${message instanceof Error ? message.stack || message.message : message}`) | ||
| } | ||
|
|
||
| append(value: string): void { | ||
| this.originalChannel.append(value) | ||
| this.writeToFile(value) | ||
| } | ||
|
|
||
| appendLine(value: string): void { | ||
| this.originalChannel.appendLine(value) | ||
| this.writeToFile(value + '\n') | ||
| } | ||
|
|
||
| replace(value: string): void { | ||
| this.originalChannel.replace(value) | ||
| this.writeToFile(`[REPLACE] ${value}`) | ||
| } | ||
|
|
||
| clear(): void { | ||
| this.originalChannel.clear() | ||
| } | ||
|
|
||
| show(preserveFocus?: boolean): void | ||
| show(column?: vscode.ViewColumn, preserveFocus?: boolean): void | ||
| show(columnOrPreserveFocus?: vscode.ViewColumn | boolean, preserveFocus?: boolean): void { | ||
| if (typeof columnOrPreserveFocus === 'boolean') { | ||
| this.originalChannel.show(columnOrPreserveFocus) | ||
| } else { | ||
| this.originalChannel.show(columnOrPreserveFocus, preserveFocus) | ||
| } | ||
| } | ||
|
|
||
| hide(): void { | ||
| this.originalChannel.hide() | ||
| } | ||
|
|
||
| dispose(): void { | ||
| // First dispose the original channel | ||
| this.originalChannel.dispose() | ||
|
|
||
| // Close our file stream if it exists | ||
| if (this.fileStream) { | ||
| this.fileStream.end() | ||
| } | ||
|
|
||
| // Clean up all log files | ||
| const logDir = this.extensionContext.storageUri?.fsPath | ||
| if (logDir) { | ||
| try { | ||
| const files = fs.readdirSync(logDir) | ||
| for (const file of files) { | ||
| if (file.startsWith('amazonq-lsp-') && file.endsWith('.log')) { | ||
| fs.unlinkSync(path.join(logDir, file)) | ||
| } | ||
| } | ||
| this.logger.info('Cleaned up all log files during disposal') | ||
| } catch (err) { | ||
| this.logger.error(`Failed to cleanup log files during disposal: ${err}`) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private writeToFile(content: string): void { | ||
| if (this.fileStream) { | ||
| try { | ||
| const timestamp = new Date().toISOString() | ||
| const logLine = `${timestamp} ${content}\n` | ||
| const size = Buffer.byteLength(logLine) | ||
|
|
||
| // If this write would exceed max file size, rotate first | ||
| if (this.currentFileSize + size > this.MAX_FILE_SIZE) { | ||
| void this.rotateLog() | ||
| } | ||
|
|
||
| this.fileStream.write(logLine) | ||
| this.currentFileSize += size | ||
| } catch (err) { | ||
| this.logger.error(`Failed to write to log file: ${err}`) | ||
| void this.rotateLog() | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we are using
Amazon Q Language Serverin VSC? to align with Flare?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This where the flare logs get printed they don't get printed through flare its in the toolkit. We are just trying to maintain the functionality here so that current std out experience doesn't change while enabling the logs on disk.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh I see if the tranceServerEnabled then we print logs to VSC
That make sense!
but this is existing right? Why do we need todo add this check again?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thats not how it works currently. We print all logs to VSC currently. If the setting is on, then we just take some of the logs to a different stdout for clean separation.
When interested in writing to the disk. I am trying to maintain the same behavior so that while std out separates based on the setting. The logs keep printing to the same file regardless. Thats why the check check moved to the top.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you point me to the logic, we have today? just for context but not a blocking for this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
packages/amazonq/src/lsp/client.ts
Sure! This file that we have open and commenting on is the logic that governs this. This is where we decide what to put where as far as LSP logs are concerned.