From bdaab4184da7b7fab89ed1fa2e8b8a2b6cdf781a Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Thu, 5 Mar 2020 10:42:28 +0000 Subject: [PATCH] refactor(ngcc): expose logging level on the logger (#35861) PR Close #35861 --- packages/compiler-cli/ngcc/index.ts | 4 ++-- packages/compiler-cli/ngcc/main-ngcc.ts | 3 ++- .../ngcc/src/logging/console_logger.ts | 19 ++++++------------- .../compiler-cli/ngcc/src/logging/logger.ts | 8 ++++++++ packages/compiler-cli/ngcc/src/main.ts | 4 ++-- .../ngcc/test/helpers/mock_logger.ts | 6 ++++-- .../ngcc/test/logging/console_logger_spec.ts | 10 +++++++++- 7 files changed, 33 insertions(+), 21 deletions(-) diff --git a/packages/compiler-cli/ngcc/index.ts b/packages/compiler-cli/ngcc/index.ts index 90d58bb7d1..0c1a5810b0 100644 --- a/packages/compiler-cli/ngcc/index.ts +++ b/packages/compiler-cli/ngcc/index.ts @@ -8,8 +8,8 @@ import {CachedFileSystem, NodeJSFileSystem, setFileSystem} from '../src/ngtsc/file_system'; import {AsyncNgccOptions, NgccOptions, SyncNgccOptions, mainNgcc} from './src/main'; -export {ConsoleLogger, LogLevel} from './src/logging/console_logger'; -export {Logger} from './src/logging/logger'; +export {ConsoleLogger} from './src/logging/console_logger'; +export {LogLevel, Logger} from './src/logging/logger'; export {AsyncNgccOptions, NgccOptions, SyncNgccOptions} from './src/main'; export {PathMappings} from './src/utils'; diff --git a/packages/compiler-cli/ngcc/main-ngcc.ts b/packages/compiler-cli/ngcc/main-ngcc.ts index 3864c70496..e3a7f7d2d7 100644 --- a/packages/compiler-cli/ngcc/main-ngcc.ts +++ b/packages/compiler-cli/ngcc/main-ngcc.ts @@ -10,7 +10,8 @@ import * as yargs from 'yargs'; import {resolve, setFileSystem, CachedFileSystem, NodeJSFileSystem} from '../src/ngtsc/file_system'; import {mainNgcc} from './src/main'; -import {ConsoleLogger, LogLevel} from './src/logging/console_logger'; +import {ConsoleLogger} from './src/logging/console_logger'; +import {LogLevel} from './src/logging/logger'; // CLI entry point if (require.main === module) { diff --git a/packages/compiler-cli/ngcc/src/logging/console_logger.ts b/packages/compiler-cli/ngcc/src/logging/console_logger.ts index 7f358a13b6..040bce9513 100644 --- a/packages/compiler-cli/ngcc/src/logging/console_logger.ts +++ b/packages/compiler-cli/ngcc/src/logging/console_logger.ts @@ -5,7 +5,7 @@ * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ -import {Logger} from './logger'; +import {LogLevel, Logger} from './logger'; const RESET = '\x1b[0m'; const RED = '\x1b[31m'; @@ -16,13 +16,6 @@ export const DEBUG = `${BLUE}Debug:${RESET}`; export const WARN = `${YELLOW}Warning:${RESET}`; export const ERROR = `${RED}Error:${RESET}`; -export enum LogLevel { - debug, - info, - warn, - error, -} - /** * A simple logger that outputs directly to the Console. * @@ -30,17 +23,17 @@ export enum LogLevel { * constructor parameter. */ export class ConsoleLogger implements Logger { - constructor(private logLevel: LogLevel) {} + constructor(public level: LogLevel) {} debug(...args: string[]) { - if (this.logLevel <= LogLevel.debug) console.debug(DEBUG, ...args); + if (this.level <= LogLevel.debug) console.debug(DEBUG, ...args); } info(...args: string[]) { - if (this.logLevel <= LogLevel.info) console.info(...args); + if (this.level <= LogLevel.info) console.info(...args); } warn(...args: string[]) { - if (this.logLevel <= LogLevel.warn) console.warn(WARN, ...args); + if (this.level <= LogLevel.warn) console.warn(WARN, ...args); } error(...args: string[]) { - if (this.logLevel <= LogLevel.error) console.error(ERROR, ...args); + if (this.level <= LogLevel.error) console.error(ERROR, ...args); } } diff --git a/packages/compiler-cli/ngcc/src/logging/logger.ts b/packages/compiler-cli/ngcc/src/logging/logger.ts index ed260081ee..cf50fff76a 100644 --- a/packages/compiler-cli/ngcc/src/logging/logger.ts +++ b/packages/compiler-cli/ngcc/src/logging/logger.ts @@ -11,8 +11,16 @@ * output from the standard ConsoleLogger. */ export interface Logger { + level: LogLevel; debug(...args: string[]): void; info(...args: string[]): void; warn(...args: string[]): void; error(...args: string[]): void; } + +export enum LogLevel { + debug, + info, + warn, + error, +} diff --git a/packages/compiler-cli/ngcc/src/main.ts b/packages/compiler-cli/ngcc/src/main.ts index ee21f09487..135b2ae812 100644 --- a/packages/compiler-cli/ngcc/src/main.ts +++ b/packages/compiler-cli/ngcc/src/main.ts @@ -31,8 +31,8 @@ import {LockFileAsync, LockFileSync} from './execution/lock_file'; import {SingleProcessExecutorAsync, SingleProcessExecutorSync} from './execution/single_process_executor'; import {ParallelTaskQueue} from './execution/task_selection/parallel_task_queue'; import {SerialTaskQueue} from './execution/task_selection/serial_task_queue'; -import {ConsoleLogger, LogLevel} from './logging/console_logger'; -import {Logger} from './logging/logger'; +import {ConsoleLogger} from './logging/console_logger'; +import {LogLevel, Logger} from './logging/logger'; import {hasBeenProcessed} from './packages/build_marker'; import {NgccConfiguration} from './packages/configuration'; import {EntryPoint, EntryPointJsonProperty, EntryPointPackageJson, SUPPORTED_FORMAT_PROPERTIES, getEntryPointFormat} from './packages/entry_point'; diff --git a/packages/compiler-cli/ngcc/test/helpers/mock_logger.ts b/packages/compiler-cli/ngcc/test/helpers/mock_logger.ts index e82e77005e..bc686a994f 100644 --- a/packages/compiler-cli/ngcc/test/helpers/mock_logger.ts +++ b/packages/compiler-cli/ngcc/test/helpers/mock_logger.ts @@ -6,10 +6,12 @@ * found in the LICENSE file at https://angular.io/license */ -import {Logger} from '../../src/logging/logger'; +import {LogLevel, Logger} from '../../src/logging/logger'; export class MockLogger implements Logger { - logs: {[P in keyof Logger]: string[][]} = { + constructor(public level = LogLevel.info) {} + + logs: {[P in Exclude]: string[][]} = { debug: [], info: [], warn: [], diff --git a/packages/compiler-cli/ngcc/test/logging/console_logger_spec.ts b/packages/compiler-cli/ngcc/test/logging/console_logger_spec.ts index 76b9bd8ed7..68125ebe7f 100644 --- a/packages/compiler-cli/ngcc/test/logging/console_logger_spec.ts +++ b/packages/compiler-cli/ngcc/test/logging/console_logger_spec.ts @@ -5,7 +5,8 @@ * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ -import {ConsoleLogger, DEBUG, ERROR, LogLevel, WARN} from '../../src/logging/console_logger'; +import {ConsoleLogger, DEBUG, ERROR, WARN} from '../../src/logging/console_logger'; +import {LogLevel} from '../../src/logging/logger'; describe('ConsoleLogger', () => { it('should pass through calls to Console', () => { @@ -47,4 +48,11 @@ describe('ConsoleLogger', () => { logger.error('error', 'test'); expect(console.error).toHaveBeenCalledWith(ERROR, 'error', 'test'); }); + + it('should expose the logging level', () => { + expect(new ConsoleLogger(LogLevel.debug).level).toEqual(LogLevel.debug); + expect(new ConsoleLogger(LogLevel.info).level).toEqual(LogLevel.info); + expect(new ConsoleLogger(LogLevel.warn).level).toEqual(LogLevel.warn); + expect(new ConsoleLogger(LogLevel.error).level).toEqual(LogLevel.error); + }); });