-
Notifications
You must be signed in to change notification settings - Fork 0
require setting logger output channel to use it #8
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,7 +1,15 @@ | ||||||||||||||||||||||||
| import * as vscode from 'vscode'; | ||||||||||||||||||||||||
| import { LogLevel, type ILogger } from '../types'; | ||||||||||||||||||||||||
| import { createConsoleLogger } from '../utils'; | ||||||||||||||||||||||||
| import type * as vscode from 'vscode'; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| export const disallowedLogKeys = ['password', 'secret', 'token', 'apiKey', 'apiSecret', 'content']; | ||||||||||||||||||||||||
| export const disallowedLogKeys: Set<string> = new Set([ | ||||||||||||||||||||||||
| 'password', | ||||||||||||||||||||||||
| 'secret', | ||||||||||||||||||||||||
| 'token', | ||||||||||||||||||||||||
| 'apikey', | ||||||||||||||||||||||||
| 'apisecret', | ||||||||||||||||||||||||
| 'content', | ||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| function removePromptsFromData<T>(dictionary: T | undefined | null): T | undefined { | ||||||||||||||||||||||||
| if (dictionary === null || dictionary === undefined) { | ||||||||||||||||||||||||
|
|
@@ -15,12 +23,10 @@ function removePromptsFromData<T>(dictionary: T | undefined | null): T | undefin | |||||||||||||||||||||||
| return dictionary; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const clone = structuredClone(dictionary) as Record<string, unknown>; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| for (const key in clone) { | ||||||||||||||||||||||||
| const value = clone[key]; | ||||||||||||||||||||||||
| if (disallowedLogKeys.includes(key)) { | ||||||||||||||||||||||||
| const clone = structuredClone(dictionary) as Record<string, unknown>; | ||||||||||||||||||||||||
| for (const [key, value] of Object.entries(clone)) { | ||||||||||||||||||||||||
| if (disallowedLogKeys.has(key.toLowerCase())) { | ||||||||||||||||||||||||
| // eslint-disable-next-line @typescript-eslint/no-dynamic-delete | ||||||||||||||||||||||||
| delete clone[key]; | ||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||
|
|
@@ -32,21 +38,21 @@ function removePromptsFromData<T>(dictionary: T | undefined | null): T | undefin | |||||||||||||||||||||||
| clone[key] = removePromptsFromData(value as Record<string, unknown>) as unknown; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| return clone as unknown as T; | ||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||
| console.error('Error processing log data:', error); | ||||||||||||||||||||||||
| return {} as T; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return clone as unknown as T; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * Static logger class for extension-wide logging | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| // eslint-disable-next-line @typescript-eslint/no-extraneous-class | ||||||||||||||||||||||||
| class LoggerImpl { | ||||||||||||||||||||||||
| private static readonly outputChannel: vscode.OutputChannel = | ||||||||||||||||||||||||
| vscode.window.createOutputChannel('IPC'); | ||||||||||||||||||||||||
| // eslint-disable-next-line sonarjs/public-static-readonly | ||||||||||||||||||||||||
| static outputChannel: vscode.OutputChannel | undefined = undefined; | ||||||||||||||||||||||||
| private static readonly consoleLogger = createConsoleLogger('RVW'); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public static debug(message: string, data: Record<string, unknown> | undefined = undefined) { | ||||||||||||||||||||||||
| this.log(LogLevel.DEBUG, message, data); | ||||||||||||||||||||||||
|
|
@@ -65,33 +71,64 @@ class LoggerImpl { | |||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public static dispose() { | ||||||||||||||||||||||||
| this.outputChannel.dispose(); | ||||||||||||||||||||||||
| this.outputChannel?.dispose(); | ||||||||||||||||||||||||
| this.outputChannel = undefined; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| private static log(level: LogLevel, message: string, data: Record<string, unknown> | undefined) { | ||||||||||||||||||||||||
| const timestamp = new Date().toISOString().split('T')[1]; | ||||||||||||||||||||||||
| const levelStr = LogLevel[level] || 'UNKNOWN'; | ||||||||||||||||||||||||
| const cleanedData = removePromptsFromData(data); | ||||||||||||||||||||||||
| const line = `[${timestamp}] [${levelStr}] ${message}`; | ||||||||||||||||||||||||
| if (cleanedData === undefined) { | ||||||||||||||||||||||||
| this.outputChannel.appendLine(line); | ||||||||||||||||||||||||
| if (this.outputChannel === undefined) { | ||||||||||||||||||||||||
| const methodName = LogLevel[level].toLowerCase() as | ||||||||||||||||||||||||
| | undefined | ||||||||||||||||||||||||
| | keyof Omit<ILogger, 'dispose'>; | ||||||||||||||||||||||||
| if (methodName !== undefined && typeof this.consoleLogger[methodName] === 'function') { | ||||||||||||||||||||||||
| if (cleanedData === undefined) { | ||||||||||||||||||||||||
| this.consoleLogger[methodName](`[${timestamp}] ${message}`); | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| this.consoleLogger[methodName](`[${timestamp}] ${message}`, cleanedData); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| this.outputChannel.appendLine(`${line} : ${JSON.stringify(cleanedData)}`); | ||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||
| this.outputChannel.appendLine(`${line} : unserializable data`); | ||||||||||||||||||||||||
| const levelStr = LogLevel[level] || 'UNKNOWN'; | ||||||||||||||||||||||||
| const line = `[${timestamp}] [${levelStr}] ${message}`; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (cleanedData === undefined) { | ||||||||||||||||||||||||
| this.outputChannel.appendLine(line); | ||||||||||||||||||||||||
|
Comment on lines
+96
to
+97
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (bug_risk): Catching all errors when serializing cleanedData may mask unexpected issues. Consider logging the specific error when serialization fails for reasons other than circular references to improve debuggability.
Suggested change
|
||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| this.outputChannel.appendLine(`${line} : ${JSON.stringify(cleanedData)}`); | ||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||
| this.outputChannel.appendLine(`${line} : unserializable data`); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| export const Logger: ILogger = { | ||||||||||||||||||||||||
| debug: (message: string, data?: Record<string, unknown>) => LoggerImpl.debug(message, data), | ||||||||||||||||||||||||
| info: (message: string, data?: Record<string, unknown>) => LoggerImpl.info(message, data), | ||||||||||||||||||||||||
| warn: (message: string, data?: Record<string, unknown>) => LoggerImpl.warn(message, data), | ||||||||||||||||||||||||
| error: (message: string, data?: Record<string, unknown>) => LoggerImpl.error(message, data), | ||||||||||||||||||||||||
| dispose: () => LoggerImpl.dispose(), | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
| export const Logger: ILogger & { | ||||||||||||||||||||||||
| setOutputChannel: (outputChannel: vscode.OutputChannel | undefined) => void; | ||||||||||||||||||||||||
| } = | ||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * Sets the VS Code output channel for the logger. | ||||||||||||||||||||||||
| * The logger takes ownership of the channel and will dispose it | ||||||||||||||||||||||||
| * when `Logger.dispose()` is called or when a new channel is set. | ||||||||||||||||||||||||
| * @param outputChannel The output channel to use for logging. | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| setOutputChannel: (outputChannel: vscode.OutputChannel | undefined) => { | ||||||||||||||||||||||||
| if (LoggerImpl.outputChannel === outputChannel) { | ||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| LoggerImpl.dispose(); | ||||||||||||||||||||||||
| LoggerImpl.outputChannel = outputChannel; | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| debug: (message: string, data?: Record<string, unknown>) => LoggerImpl.debug(message, data), | ||||||||||||||||||||||||
| info: (message: string, data?: Record<string, unknown>) => LoggerImpl.info(message, data), | ||||||||||||||||||||||||
| warn: (message: string, data?: Record<string, unknown>) => LoggerImpl.warn(message, data), | ||||||||||||||||||||||||
| error: (message: string, data?: Record<string, unknown>) => LoggerImpl.error(message, data), | ||||||||||||||||||||||||
| dispose: () => LoggerImpl.dispose(), | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| export const getLogger = (tag: string): ILogger => ({ | ||||||||||||||||||||||||
| debug: (message: string, data?: Record<string, unknown>) => | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.