Skip to content

Commit

Permalink
Log to the TS server log too. (runem#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
rictic authored Feb 4, 2022
1 parent 9a97f32 commit 88c989a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/ts-lit-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function init({ typescript }: { typescript: typeof ts }): tsServer.server.Plugin
if ((info.languageService as any)[tsHtmlPluginSymbol] != null) {
return info.languageService;
}
logger.setTsServerLogging(info.project.projectService.logger);

// Save the current working directory
info.config.cwd = info.config.cwd || info.project.getCurrentDirectory();
Expand Down
32 changes: 17 additions & 15 deletions packages/ts-lit-plugin/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { appendFileSync, writeFileSync } from "fs";
import { DefaultLitAnalyzerLogger, LitAnalyzerLoggerLevel } from "lit-analyzer";
import { join } from "path";
import { inspect } from "util";
import * as tsServer from "typescript/lib/tsserverlibrary.js";

const LOG_FILE_NAME = "lit-plugin.log";

Expand All @@ -13,6 +14,17 @@ const LOG_FILE_NAME = "lit-plugin.log";
export class Logger extends DefaultLitAnalyzerLogger {
level = LitAnalyzerLoggerLevel.OFF;

private tsLogger: tsServer.server.Logger | undefined = undefined;

/**
* Call this with the TS Server's logger so that we can log to the TS server logs.
*
* Access in VS Code via > TypeScript: Open TS Server log
*/
setTsServerLogging(tsLogger: tsServer.server.Logger | undefined): void {
this.tsLogger = tsLogger;
}

/**
* Logs if this.level >= DEBUG
* @param args
Expand Down Expand Up @@ -68,25 +80,15 @@ export class Logger extends DefaultLitAnalyzerLogger {
private appendLogWithLevel(level: LitAnalyzerLoggerLevel, ...args: any[]) {
if (this.level >= level) {
const prefix = this.getLogLevelPrefix(level);
this.appendLog(prefix, ...args);
}
}

/**
* Appends a log entry to the log file.
* @param prefix
* @param args
*/
private appendLog(prefix: string, ...args: any[]) {
appendFileSync(
this.logPath,
`${prefix}${inspect(args, {
const message = inspect(args, {
colors: true,
depth: 6,
breakLength: 50,
maxArrayLength: 10
})}\n`
);
});
appendFileSync(this.logPath, `${prefix}${message}\n`);
this.tsLogger?.msg(`[ts-lit-plugin] ${message}`, level === LitAnalyzerLoggerLevel.ERROR ? tsServer.server.Msg.Err : tsServer.server.Msg.Info);
}
}

private getLogLevelPrefix(level: LitAnalyzerLoggerLevel) {
Expand Down

0 comments on commit 88c989a

Please sign in to comment.