Skip to content

Commit dfcc337

Browse files
committed
Improved LOGGER design
1 parent 1caeb49 commit dfcc337

File tree

16 files changed

+49
-48
lines changed

16 files changed

+49
-48
lines changed

vscode/src/commands/buildOperations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License.
1515
*/
1616

17-
import { LOGGER } from "../extension";
17+
import { LOGGER } from "../logger";
1818
import { l10n } from "../localiser";
1919
import { extCommands, nbCommands } from "./commands";
2020
import { ICommand } from "./types";

vscode/src/commands/navigation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import { l10n } from "../localiser";
2020
import * as path from 'path';
2121
import { ICommand } from "./types";
2222
import { LanguageClient } from "vscode-languageclient/node";
23-
import { globalVars, LOGGER } from "../extension";
23+
import { globalVars } from "../extension";
24+
import { LOGGER } from '../logger';
2425
import { getContextUri, isNbCommandRegistered, wrapCommandWithProgress } from "./utils";
2526

2627
const goToTest = async (ctx: any) => {

vscode/src/commands/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import { commands, OutputChannel, ProgressLocation, Uri, window } from "vscode";
1818
import { nbCommands } from "./commands";
1919
import { ProjectActionParams } from "../lsp/protocol";
2020
import { LanguageClient } from "vscode-languageclient/node";
21-
import { globalVars, LOGGER } from "../extension";
21+
import { globalVars } from "../extension";
2222
import { l10n } from "../localiser";
23-
import { LogLevel } from "../logger";
23+
import { LOGGER } from "../logger";
2424

2525
export const getContextUri = (ctx: any): Uri | undefined => {
2626
if (ctx?.fsPath) {
@@ -99,14 +99,14 @@ export const wrapCommandWithProgress = (lsCommand: string, title: string, log?:
9999
resolve(res);
100100
} else {
101101
if (log) {
102-
LOGGER.log(`Command ${lsCommand} takes too long to start`, LogLevel.ERROR);
102+
LOGGER.error(`Command ${lsCommand} takes too long to start`);
103103
}
104104
reject(res);
105105
}
106106
}, humanVisibleDelay);
107107
} catch (err: any) {
108108
if (log) {
109-
LOGGER.log(`command ${lsCommand} executed with error: ${JSON.stringify(err)}`, LogLevel.ERROR);
109+
LOGGER.error(`command ${lsCommand} executed with error: ${JSON.stringify(err)}`);
110110
}
111111
}
112112
} else {

vscode/src/configurations/handlers.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import { extensions, workspace } from "vscode";
1818
import { builtInConfigKeys, configKeys } from "./configuration";
1919
import { extConstants, NODE_WINDOWS_LABEL } from "../constants";
2020
import * as os from 'os';
21-
import { globalVars, LOGGER } from "../extension";
22-
import { LogLevel } from "../logger";
21+
import { globalVars } from "../extension";
22+
import { LOGGER } from "../logger";
2323
import * as path from 'path';
2424
import * as fs from 'fs';
2525

@@ -54,7 +54,7 @@ export const projectSearchRootsValueHandler = (): string => {
5454
try {
5555
projectSearchRoots = os.homedir() as string;
5656
} catch (err: any) {
57-
LOGGER.log(`Failed to obtain the user home directory due to: ${err}`, LogLevel.ERROR);
57+
LOGGER.error(`Failed to obtain the user home directory due to: ${err}`);
5858
}
5959
if (!projectSearchRoots) {
6060
projectSearchRoots = os.type() === NODE_WINDOWS_LABEL ? '%USERPROFILE%' : '$HOME'; // The launcher script may perform the env variable substitution
@@ -84,7 +84,7 @@ export const lspServerVmOptionsHandler = (): string[] => {
8484
}
8585

8686
export const isDarkColorThemeHandler = (): boolean => {
87-
const themeName: string = getBuiltinConfigurationValue(builtInConfigKeys.vscodeTheme);
87+
const themeName: string = getBuiltinConfigurationValue(builtInConfigKeys.vscodeTheme);
8888
if (!themeName) {
8989
return false;
9090
}

vscode/src/extension.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import * as launchConfigurations from './launchConfigurations';
3333
import { extConstants } from './constants';
3434
import { ExtensionInfo } from './extensionInfo';
3535
import { ClientPromise } from './lsp/clientPromise';
36-
import { ExtensionLogger } from './logger';
3736
import { NbProcessManager } from './lsp/nbProcessManager';
3837
import { clientInit } from './lsp/initializer';
3938
import { subscribeCommands } from './commands/register';
@@ -43,7 +42,6 @@ import { registerConfigChangeListeners } from './configurations/listener';
4342
import { registerFileProviders } from './lsp/listeners/textDocumentContentProvider';
4443
import { createViews } from './views/initializer';
4544

46-
export let LOGGER: ExtensionLogger;
4745
export namespace globalVars {
4846
export const listeners = new Map<string, string[]>();
4947
export let extensionInfo: ExtensionInfo;
@@ -61,7 +59,6 @@ export namespace globalVars {
6159
export function activate(context: ExtensionContext): VSNetBeansAPI {
6260
globalVars.clientPromise = new ClientPromise();
6361
globalVars.extensionInfo = new ExtensionInfo(context);
64-
LOGGER = new ExtensionLogger(extConstants.SERVER_NAME);
6562

6663
globalVars.clientPromise.initialize();
6764
registerConfigChangeListeners(context);

vscode/src/logger.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
*/
1616

1717
import { OutputChannel, window } from "vscode";
18+
import { extConstants } from "./constants";
1819

19-
export enum LogLevel {
20+
enum LogLevel {
2021
INFO = 'INFO',
2122
WARN = 'WARN',
2223
ERROR = 'ERROR',
@@ -29,16 +30,16 @@ export class ExtensionLogger {
2930
this.outChannel = window.createOutputChannel(channelName);
3031
}
3132

32-
public log(message: string, level: LogLevel = LogLevel.INFO): void {
33-
this.outChannel.appendLine(`[${level}]: ${message}`);
33+
public log(message: string): void {
34+
this.outChannel.appendLine(`[${LogLevel.INFO}]: ${message}`);
3435
}
3536

3637
public warn(message: string): void {
37-
this.log(message, LogLevel.WARN);
38+
this.outChannel.appendLine(`[${LogLevel.WARN}]: ${message}`);
3839
}
3940

4041
public error(message: string): void {
41-
this.log(message, LogLevel.ERROR);
42+
this.outChannel.appendLine(`[${LogLevel.ERROR}]: ${message}`);
4243
}
4344

4445
public logNoNL(message: string): void {
@@ -56,4 +57,6 @@ export class ExtensionLogger {
5657
public dispose(): void {
5758
this.outChannel.dispose();
5859
}
59-
}
60+
}
61+
62+
export const LOGGER = new ExtensionLogger(extConstants.SERVER_NAME);

vscode/src/lsp/clientPromise.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616

1717
import { commands } from "vscode";
18-
import { globalVars, LOGGER } from "../extension";
19-
import { LogLevel } from "../logger";
18+
import { globalVars } from "../extension";
19+
import { LOGGER } from "../logger";
2020
import { NbProcessManager } from "./nbProcessManager";
2121
import { clientInit } from "./initializer";
2222
import { NbLanguageClient } from "./nbLanguageClient";
@@ -61,11 +61,11 @@ export class ClientPromise {
6161

6262
public restartExtension = async (nbProcessManager: NbProcessManager | null, notifyKill: boolean) => {
6363
if (this.activationPending) {
64-
LOGGER.log("Server activation requested repeatedly, ignoring...", LogLevel.WARN);
64+
LOGGER.warn("Server activation requested repeatedly, ignoring...");
6565
return;
6666
}
6767
if (!nbProcessManager) {
68-
LOGGER.log("Nbcode Process is null", LogLevel.ERROR);
68+
LOGGER.error("Nbcode Process is null");
6969
return;
7070
}
7171
try {
@@ -74,7 +74,7 @@ export class ClientPromise {
7474
this.initialize();
7575
clientInit();
7676
} catch (error) {
77-
LOGGER.log(`Error during activation: ${error}`, LogLevel.ERROR);
77+
LOGGER.error(`Error during activation: ${error}`);
7878
throw error;
7979
} finally {
8080
this.activationPending = false;

vscode/src/lsp/initializer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
import { StreamInfo } from "vscode-languageclient/node";
1818
import { getUserConfigLaunchOptionsDefaults } from "./launchOptions";
19-
import { globalVars, LOGGER } from "../extension";
19+
import { globalVars } from "../extension";
20+
import { LOGGER } from '../logger';
2021
import { configKeys } from "../configurations/configuration";
2122
import { enableDisableModules } from "./utils";
2223
import * as net from 'net';

vscode/src/lsp/listeners/notifications/handlers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import { isNbJavacDisabledHandler, updateConfigurationValue } from "../../../con
2222
import { l10n } from "../../../localiser";
2323
import { configKeys } from "../../../configurations/configuration";
2424
import { builtInCommands } from "../../../commands/commands";
25-
import { globalVars, LOGGER } from "../../../extension";
25+
import { globalVars } from "../../../extension";
26+
import { LOGGER } from '../../../logger';
2627

2728
const checkInstallNbJavac = (msg: string) => {
2829
const NO_JAVA_SUPPORT = "Cannot initialize Java support";

vscode/src/lsp/listeners/requests/handlers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
*/
1616

1717
import { QuickPickItem, Uri, window, workspace, WorkspaceConfiguration } from "vscode";
18-
import { globalVars, LOGGER } from "../../../extension";
18+
import { globalVars } from "../../../extension";
1919
import { notificationOrRequestListenerType } from "../../types";
2020
import { ExecInHtmlPageRequest, HtmlPageRequest, InputBoxRequest, InputBoxStep, MutliStepInputRequest, QuickPickRequest, QuickPickStep, SaveDocumentRequestParams, SaveDocumentsRequest, TextEditorDecorationCreateRequest, UpdateConfigurationRequest } from "../../protocol";
2121
import { InputStep, MultiStepInput } from "../../../utils";
2222
import { runConfigurationUpdateAll } from "../../../views/runConfiguration";
2323
import { isError } from "../../../utils";
2424
import { isString } from "../../../utils";
25-
import { LogLevel } from "../../../logger";
25+
import { LOGGER } from "../../../logger";
2626
import { execInHtmlPage, showHtmlPage } from "../../../webviews/nbWebviewHandler";
2727

2828
const textEditorDecorationCreateRequestHandler = (param: any) => {
@@ -105,7 +105,7 @@ const updateConfigRequestHandler = async (param: any) => {
105105
runConfigurationUpdateAll();
106106
});
107107
} catch (err) {
108-
LOGGER.log("Failed to update configuration. Reason: " + (isString(err) ? err : isError(err) ? err.message : "error"), LogLevel.ERROR);
108+
LOGGER.error("Failed to update configuration. Reason: " + (isString(err) ? err : isError(err) ? err.message : "error"));
109109
}
110110
}
111111
}

0 commit comments

Comments
 (0)