Add setting to control markdown LS log level (#176472)

This commit is contained in:
Matt Bierner
2023-03-07 23:00:34 -08:00
committed by GitHub
parent b2ea6aa9fc
commit da15b03a10
5 changed files with 58 additions and 22 deletions

View File

@@ -438,6 +438,17 @@
"default": "off",
"description": "%markdown.trace.server.desc%"
},
"markdown.server.log": {
"type": "string",
"scope": "window",
"enum": [
"off",
"debug",
"trace"
],
"default": "off",
"description": "%markdown.server.log.desc%"
},
"markdown.editor.drop.enabled": {
"type": "boolean",
"default": true,

View File

@@ -19,6 +19,7 @@
"markdown.showPreviewSecuritySelector.title": "Change Preview Security Settings",
"markdown.trace.extension.desc": "Enable debug logging for the Markdown extension.",
"markdown.trace.server.desc": "Traces the communication between VS Code and the Markdown language server.",
"markdown.server.log.desc": "Controls the logging level of the Markdown language server.",
"markdown.preview.refresh.title": "Refresh Preview",
"markdown.preview.toggleLock.title": "Toggle Preview Locking",
"markdown.findAllFileReferences": "Find File References",

View File

@@ -10,6 +10,10 @@ export type ValidateEnabled = 'ignore' | 'warning' | 'error' | 'hint';
export interface Settings {
readonly markdown: {
readonly server: {
readonly log: 'off' | 'debug' | 'trace';
};
readonly preferredMdPathExtensionStyle: 'auto' | 'includeExtension' | 'removeExtension';
readonly occurrencesHighlight: {

View File

@@ -3,9 +3,11 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ILogger, LogLevel } from 'vscode-markdown-languageservice';
import * as md from 'vscode-markdown-languageservice';
import { ConfigurationManager } from './configuration';
import { Disposable } from './util/dispose';
export class LogFunctionLogger implements ILogger {
export class LogFunctionLogger extends Disposable implements md.ILogger {
private static now(): string {
const now = new Date();
@@ -27,16 +29,35 @@ export class LogFunctionLogger implements ILogger {
return JSON.stringify(data, undefined, 2);
}
constructor(
private readonly _logFn: typeof console.log
) { }
private _logLevel: md.LogLevel;
get level(): LogLevel {
return LogLevel.Debug; // TODO: remove hardcoding
constructor(
private readonly _logFn: typeof console.log,
private readonly _config: ConfigurationManager,
) {
super();
this._register(this._config.onDidChangeConfiguration(() => {
this._logLevel = LogFunctionLogger.readLogLevel(this._config);
}));
this._logLevel = LogFunctionLogger.readLogLevel(this._config);
}
public log(level: LogLevel, message: string, data?: any): void {
if (level < this.level) {
private static readLogLevel(config: ConfigurationManager): md.LogLevel {
switch (config.getSettings()?.markdown.server.log) {
case 'trace': return md.LogLevel.Trace;
case 'debug': return md.LogLevel.Debug;
case 'off':
default:
return md.LogLevel.Off;
}
}
get level(): md.LogLevel { return this._logLevel; }
public log(level: md.LogLevel, message: string, data?: any): void {
if (this.level < level) {
return;
}
@@ -46,11 +67,11 @@ export class LogFunctionLogger implements ILogger {
}
}
private toLevelLabel(level: LogLevel): string {
private toLevelLabel(level: md.LogLevel): string {
switch (level) {
case LogLevel.Off: return 'Off';
case LogLevel.Debug: return 'Debug';
case LogLevel.Trace: return 'Trace';
case md.LogLevel.Off: return 'Off';
case md.LogLevel.Debug: return 'Debug';
case md.LogLevel.Trace: return 'Trace';
}
}
@@ -58,5 +79,3 @@ export class LogFunctionLogger implements ILogger {
this._logFn(value);
}
}
export const consoleLogger = new LogFunctionLogger(console.log);

View File

@@ -22,7 +22,8 @@ interface MdServerInitializationOptions extends LsConfiguration { }
const organizeLinkDefKind = 'source.organizeLinkDefinitions';
export async function startVsCodeServer(connection: Connection) {
const logger = new LogFunctionLogger(connection.console.log.bind(connection.console));
const configurationManager = new ConfigurationManager(connection);
const logger = new LogFunctionLogger(connection.console.log.bind(connection.console), configurationManager);
const parser = new class implements md.IMdParser {
slugifier = md.githubSlugifier;
@@ -41,7 +42,7 @@ export async function startVsCodeServer(connection: Connection) {
return workspace;
};
return startServer(connection, { documents, notebooks, logger, parser, workspaceFactory });
return startServer(connection, { documents, notebooks, configurationManager, logger, parser, workspaceFactory });
}
type WorkspaceFactory = (config: {
@@ -53,6 +54,7 @@ type WorkspaceFactory = (config: {
export async function startServer(connection: Connection, serverConfig: {
documents: TextDocuments<md.ITextDocument>;
notebooks?: NotebookDocuments<md.ITextDocument>;
configurationManager: ConfigurationManager;
logger: md.ILogger;
parser: md.IMdParser;
workspaceFactory: WorkspaceFactory;
@@ -62,7 +64,6 @@ export async function startServer(connection: Connection, serverConfig: {
let mdLs: md.IMdLanguageService | undefined;
connection.onInitialize((params: InitializeParams): InitializeResult => {
const configurationManager = new ConfigurationManager(connection);
const initOptions = params.initializationOptions as MdServerInitializationOptions | undefined;
const mdConfig = getLsConfiguration(initOptions ?? {});
@@ -74,7 +75,7 @@ export async function startServer(connection: Connection, serverConfig: {
logger: serverConfig.logger,
...mdConfig,
get preferredMdPathExtensionStyle() {
switch (configurationManager.getSettings()?.markdown.preferredMdPathExtensionStyle) {
switch (serverConfig.configurationManager.getSettings()?.markdown.preferredMdPathExtensionStyle) {
case 'includeExtension': return md.PreferredMdPathExtensionStyle.includeExtension;
case 'removeExtension': return md.PreferredMdPathExtensionStyle.removeExtension;
case 'auto':
@@ -84,9 +85,9 @@ export async function startServer(connection: Connection, serverConfig: {
}
});
registerCompletionsSupport(connection, documents, mdLs, configurationManager);
registerDocumentHighlightSupport(connection, documents, mdLs, configurationManager);
registerValidateSupport(connection, workspace, documents, mdLs, configurationManager, serverConfig.logger);
registerCompletionsSupport(connection, documents, mdLs, serverConfig.configurationManager);
registerDocumentHighlightSupport(connection, documents, mdLs, serverConfig.configurationManager);
registerValidateSupport(connection, workspace, documents, mdLs, serverConfig.configurationManager, serverConfig.logger);
return {
capabilities: {