From b3a582fd20c129ff59d4d6724be81544f58e624e Mon Sep 17 00:00:00 2001 From: thegecko Date: Wed, 17 Jan 2024 16:42:04 +0100 Subject: [PATCH] Hide output channel unless used --- src/plugin/logger.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/plugin/logger.ts b/src/plugin/logger.ts index b03afed..bd0271e 100644 --- a/src/plugin/logger.ts +++ b/src/plugin/logger.ts @@ -25,10 +25,7 @@ export enum Verbosity { debug = 4 } -export class Logger { - public static instance = new Logger(); - - protected outputChannel = vscode.window.createOutputChannel(manifest.DISPLAY_NAME); +export abstract class Logger { protected logVerbosity: Verbosity; protected constructor() { @@ -45,6 +42,8 @@ export class Logger { return Verbosity[config as keyof typeof Verbosity]; } + protected abstract logMessage(message: string): void; + // eslint-disable-next-line @typescript-eslint/no-explicit-any public log(verbosity: Verbosity, ...messages: Array): void { if (this.logVerbosity === Verbosity.off || verbosity > this.logVerbosity) { @@ -53,7 +52,7 @@ export class Logger { const result = messages.map(message => typeof message === 'string' ? message : JSON.stringify(message, undefined, '\t')).join(' '); - this.outputChannel.appendLine(result); + this.logMessage(result); } // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -66,4 +65,17 @@ export class Logger { public debug = (...messages: Array): void => this.log(Verbosity.debug, ...messages); } -export const outputChannelLogger = Logger.instance; +class OutputChannelLogger extends Logger { + public static instance = new OutputChannelLogger(); + + protected outputChannel: vscode.OutputChannel | undefined; + + protected override logMessage(message: string): void { + if (!this.outputChannel) { + this.outputChannel = vscode.window.createOutputChannel(manifest.DISPLAY_NAME); + } + this.outputChannel.appendLine(message); + } +} + +export const outputChannelLogger = OutputChannelLogger.instance;