Skip to content
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

feat: logger wrapper package #163

Merged
merged 7 commits into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/extension-wrapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"debug",
"trace"
],
"default": "info",
"default": "error",
"description": "The verbosity of logging. The Order is None < fatal < error < warn < info < debug < trace.",
"scope": "window"
},
Expand Down
41 changes: 3 additions & 38 deletions examples/extension-wrapper/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,9 @@
import { readFileSync } from "fs";
import { resolve } from "path";
import { ok } from "assert";
import { ExtensionContext, window, workspace } from "vscode";
import { configureLogger } from "@vscode-logging/wrapper";
import { getLogger, setLogger } from "./logger";
import { ExtensionContext } from "vscode";
import { getLogger, initLogger } from "./logger";
import { registerCommands } from "./commands";

const LOGGING_LEVEL_PROP = "Example_Logging.loggingLevel";
const SOURCE_LOCATION_PROP = "Example_Logging.sourceLocationTracking";

function initLogger(context: ExtensionContext): void {
const meta = JSON.parse(
readFileSync(resolve(context.extensionPath, "package.json"), "utf8")
);

// By asserting the existence of the properties in the package.json
// at runtime, we avoid many copy-pasta mistakes...
const configProps = meta?.contributes?.configuration?.properties;
ok(configProps?.[LOGGING_LEVEL_PROP]);
ok(configProps?.[SOURCE_LOCATION_PROP]);

const extLogger = configureLogger({
extName: meta.displayName,
logPath: context.logPath,
logOutputChannel: window.createOutputChannel(meta.displayName),
// set to `true` if you also want your VSCode extension to log to the console.
logConsole: false,
loggingLevelProp: LOGGING_LEVEL_PROP,
sourceLocationProp: SOURCE_LOCATION_PROP,
subscriptions: context.subscriptions,
onDidChangeConfiguration: workspace.onDidChangeConfiguration,
getConfiguration: workspace.getConfiguration
});

setLogger(extLogger);

registerCommands(context.subscriptions);
}

export function activate(context: ExtensionContext): void {
initLogger(context);
getLogger().info("extension is active, hip hip hurray!");
registerCommands(context.subscriptions);

This comment was marked as resolved.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

}
38 changes: 36 additions & 2 deletions examples/extension-wrapper/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/**
* This file manages the logger's state.
*/
import { readFileSync } from "fs";
import { resolve } from "path";
import { ok } from "assert";
import { ExtensionContext, window, workspace } from "vscode";
import { IChildLogger, IVSCodeExtLogger } from "@vscode-logging/types";
import { NOOP_LOGGER } from "@vscode-logging/wrapper";
import { configureLogger, NOOP_LOGGER } from "@vscode-logging/wrapper";

// On file load we initialize our logger to `NOOP_LOGGER`
// this is done because the "real" logger cannot be initialized during file load.
Expand All @@ -14,6 +18,36 @@ export function getLogger(): IChildLogger {
return loggerImpel;
}

export function setLogger(newLogger: IVSCodeExtLogger): void {
function setLogger(newLogger: IVSCodeExtLogger): void {
loggerImpel = newLogger;
}

const LOGGING_LEVEL_PROP = "Example_Logging.loggingLevel";
const SOURCE_LOCATION_PROP = "Example_Logging.sourceLocationTracking";

export function initLogger(context: ExtensionContext): void {
const meta = JSON.parse(
readFileSync(resolve(context.extensionPath, "package.json"), "utf8")
);

// By asserting the existence of the properties in the package.json
// at runtime, we avoid many copy-pasta mistakes...
const configProps = meta?.contributes?.configuration?.properties;
ok(configProps?.[LOGGING_LEVEL_PROP]);
ok(configProps?.[SOURCE_LOCATION_PROP]);

const extLogger = configureLogger({
extName: meta.displayName,
logPath: context.logPath,
logOutputChannel: window.createOutputChannel(meta.displayName),
// set to `true` if you also want your VSCode extension to log to the console.
logConsole: false,
loggingLevelProp: LOGGING_LEVEL_PROP,
sourceLocationProp: SOURCE_LOCATION_PROP,
subscriptions: context.subscriptions,
onDidChangeConfiguration: workspace.onDidChangeConfiguration,
getConfiguration: workspace.getConfiguration
});

setLogger(extLogger);
}