Skip to content

Commit 2043e39

Browse files
committed
Improved LOGGER design
1 parent c82fb86 commit 2043e39

File tree

16 files changed

+50
-48
lines changed

16 files changed

+50
-48
lines changed

vscode/src/commands/buildOperations.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16-
import { LOGGER } from "../extension";
16+
17+
import { LOGGER } from "../logger";
1718
import { l10n } from "../localiser";
1819
import { extCommands, nbCommands } from "./commands";
1920
import { ICommand } from "./types";

vscode/src/commands/navigation.ts

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

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

vscode/src/commands/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import { commands, OutputChannel, ProgressLocation, Uri, window } from "vscode";
1717
import { nbCommands } from "./commands";
1818
import { ProjectActionParams } from "../lsp/protocol";
1919
import { LanguageClient } from "vscode-languageclient/node";
20-
import { globalVars, LOGGER } from "../extension";
20+
import { globalVars } from "../extension";
2121
import { l10n } from "../localiser";
22-
import { LogLevel } from "../logger";
22+
import { LOGGER } from "../logger";
2323

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

vscode/src/configurations/handlers.ts

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

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

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

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
@@ -14,8 +14,9 @@
1414
limitations under the License.
1515
*/
1616
import { OutputChannel, window } from "vscode";
17+
import { extConstants } from "./constants";
1718

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

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

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

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

4344
public logNoNL(message: string): void {
@@ -55,4 +56,6 @@ export class ExtensionLogger {
5556
public dispose(): void {
5657
this.outChannel.dispose();
5758
}
58-
}
59+
}
60+
61+
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
@@ -14,8 +14,8 @@
1414
limitations under the License.
1515
*/
1616
import { commands } from "vscode";
17-
import { globalVars, LOGGER } from "../extension";
18-
import { LogLevel } from "../logger";
17+
import { globalVars } from "../extension";
18+
import { LOGGER } from "../logger";
1919
import { NbProcessManager } from "./nbProcessManager";
2020
import { clientInit } from "./initializer";
2121
import { NbLanguageClient } from "./nbLanguageClient";
@@ -60,11 +60,11 @@ export class ClientPromise {
6060

6161
public restartExtension = async (nbProcessManager: NbProcessManager | null, notifyKill: boolean) => {
6262
if (this.activationPending) {
63-
LOGGER.log("Server activation requested repeatedly, ignoring...", LogLevel.WARN);
63+
LOGGER.warn("Server activation requested repeatedly, ignoring...");
6464
return;
6565
}
6666
if (!nbProcessManager) {
67-
LOGGER.log("Nbcode Process is null", LogLevel.ERROR);
67+
LOGGER.error("Nbcode Process is null");
6868
return;
6969
}
7070
try {
@@ -73,7 +73,7 @@ export class ClientPromise {
7373
this.initialize();
7474
clientInit();
7575
} catch (error) {
76-
LOGGER.log(`Error during activation: ${error}`, LogLevel.ERROR);
76+
LOGGER.error(`Error during activation: ${error}`);
7777
throw error;
7878
} finally {
7979
this.activationPending = false;

vscode/src/lsp/initializer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*/
1616
import { StreamInfo } from "vscode-languageclient/node";
1717
import { getUserConfigLaunchOptionsDefaults } from "./launchOptions";
18-
import { globalVars, LOGGER } from "../extension";
18+
import { globalVars } from "../extension";
19+
import { LOGGER } from '../logger';
1920
import { configKeys } from "../configurations/configuration";
2021
import { enableDisableModules } from "./utils";
2122
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
@@ -21,7 +21,8 @@ import { isNbJavacDisabledHandler, updateConfigurationValue } from "../../../con
2121
import { l10n } from "../../../localiser";
2222
import { configKeys } from "../../../configurations/configuration";
2323
import { builtInCommands } from "../../../commands/commands";
24-
import { globalVars, LOGGER } from "../../../extension";
24+
import { globalVars } from "../../../extension";
25+
import { LOGGER } from '../../../logger';
2526

2627
const checkInstallNbJavac = (msg: string) => {
2728
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
@@ -14,14 +14,14 @@
1414
limitations under the License.
1515
*/
1616
import { QuickPickItem, Uri, window, workspace, WorkspaceConfiguration } from "vscode";
17-
import { globalVars, LOGGER } from "../../../extension";
17+
import { globalVars } from "../../../extension";
1818
import { notificationOrRequestListenerType } from "../../types";
1919
import { ExecInHtmlPageRequest, HtmlPageRequest, InputBoxRequest, InputBoxStep, MutliStepInputRequest, QuickPickRequest, QuickPickStep, SaveDocumentRequestParams, SaveDocumentsRequest, TextEditorDecorationCreateRequest, UpdateConfigurationRequest } from "../../protocol";
2020
import { InputStep, MultiStepInput } from "../../../utils";
2121
import { runConfigurationUpdateAll } from "../../../views/runConfiguration";
2222
import { isError } from "../../../utils";
2323
import { isString } from "../../../utils";
24-
import { LogLevel } from "../../../logger";
24+
import { LOGGER } from "../../../logger";
2525
import { execInHtmlPage, showHtmlPage } from "../../../webviews/nbWebviewHandler";
2626

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

0 commit comments

Comments
 (0)