From e82f56b96f2beff420354dbb10ad84a99ef03325 Mon Sep 17 00:00:00 2001 From: Keen Yee Liau Date: Thu, 12 Sep 2019 11:37:04 -0700 Subject: [PATCH] feat(language-service): enable logging on TypeScriptHost (#32645) This PR adds loggin methods to TypeScriptHost so that proper logging to file could be done. Three new methods are added: log(), error(), and debug(). PR Close #32645 --- .../language-service/src/typescript_host.ts | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/packages/language-service/src/typescript_host.ts b/packages/language-service/src/typescript_host.ts index 9fc09b9e8b..5d0032650c 100644 --- a/packages/language-service/src/typescript_host.ts +++ b/packages/language-service/src/typescript_host.ts @@ -9,6 +9,7 @@ import {AotSummaryResolver, CompileDirectiveSummary, CompileMetadataResolver, CompileNgModuleMetadata, CompilePipeSummary, CompilerConfig, DirectiveNormalizer, DirectiveResolver, DomElementSchemaRegistry, FormattedError, FormattedMessageChain, HtmlParser, I18NHtmlParser, JitSummaryResolver, Lexer, NgAnalyzedModules, NgModuleResolver, ParseTreeResult, Parser, PipeResolver, ResourceLoader, StaticReflector, StaticSymbol, StaticSymbolCache, StaticSymbolResolver, TemplateParser, analyzeNgModules, createOfflineCompileUrlResolver, isFormattedError} from '@angular/compiler'; import {SchemaMetadata, ViewEncapsulation, ɵConsole as Console} from '@angular/core'; import * as ts from 'typescript'; +import * as tss from 'typescript/lib/tsserverlibrary'; import {AstResult, isAstResult} from './common'; import {createLanguageService} from './language_service'; @@ -525,6 +526,45 @@ export class TypeScriptServiceHost implements LanguageServiceHost { }; } } + + /** + * Log the specified `msg` to file at INFO level. If logging is not enabled + * this method is a no-op. + * @param msg Log message + */ + log(msg: string) { + if (this.tsLsHost.log) { + this.tsLsHost.log(msg); + } + } + + /** + * Log the specified `msg` to file at ERROR level. If logging is not enabled + * this method is a no-op. + * @param msg error message + */ + error(msg: string) { + if (this.tsLsHost.error) { + this.tsLsHost.error(msg); + } + } + + /** + * Log debugging info to file at INFO level, only if verbose setting is turned + * on. Otherwise, this method is a no-op. + * @param msg debugging message + */ + debug(msg: string) { + const project = this.tsLsHost as tss.server.Project; + if (!project.projectService) { + // tsLsHost is not a Project + return; + } + const {logger} = project.projectService; + if (logger.hasLevel(tss.server.LogLevel.verbose)) { + logger.info(msg); + } + } } function findSuitableDefaultModule(modules: NgAnalyzedModules): CompileNgModuleMetadata|undefined {