refactor(ngcc): expose logging level on the logger (#35861)

PR Close #35861
This commit is contained in:
Pete Bacon Darwin 2020-03-05 10:42:28 +00:00 committed by Matias Niemelä
parent 98a9daf4f4
commit bdaab4184d
7 changed files with 33 additions and 21 deletions

View File

@ -8,8 +8,8 @@
import {CachedFileSystem, NodeJSFileSystem, setFileSystem} from '../src/ngtsc/file_system'; import {CachedFileSystem, NodeJSFileSystem, setFileSystem} from '../src/ngtsc/file_system';
import {AsyncNgccOptions, NgccOptions, SyncNgccOptions, mainNgcc} from './src/main'; import {AsyncNgccOptions, NgccOptions, SyncNgccOptions, mainNgcc} from './src/main';
export {ConsoleLogger, LogLevel} from './src/logging/console_logger'; export {ConsoleLogger} from './src/logging/console_logger';
export {Logger} from './src/logging/logger'; export {LogLevel, Logger} from './src/logging/logger';
export {AsyncNgccOptions, NgccOptions, SyncNgccOptions} from './src/main'; export {AsyncNgccOptions, NgccOptions, SyncNgccOptions} from './src/main';
export {PathMappings} from './src/utils'; export {PathMappings} from './src/utils';

View File

@ -10,7 +10,8 @@ import * as yargs from 'yargs';
import {resolve, setFileSystem, CachedFileSystem, NodeJSFileSystem} from '../src/ngtsc/file_system'; import {resolve, setFileSystem, CachedFileSystem, NodeJSFileSystem} from '../src/ngtsc/file_system';
import {mainNgcc} from './src/main'; 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 // CLI entry point
if (require.main === module) { if (require.main === module) {

View File

@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be * 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 * found in the LICENSE file at https://angular.io/license
*/ */
import {Logger} from './logger'; import {LogLevel, Logger} from './logger';
const RESET = '\x1b[0m'; const RESET = '\x1b[0m';
const RED = '\x1b[31m'; const RED = '\x1b[31m';
@ -16,13 +16,6 @@ export const DEBUG = `${BLUE}Debug:${RESET}`;
export const WARN = `${YELLOW}Warning:${RESET}`; export const WARN = `${YELLOW}Warning:${RESET}`;
export const ERROR = `${RED}Error:${RESET}`; export const ERROR = `${RED}Error:${RESET}`;
export enum LogLevel {
debug,
info,
warn,
error,
}
/** /**
* A simple logger that outputs directly to the Console. * A simple logger that outputs directly to the Console.
* *
@ -30,17 +23,17 @@ export enum LogLevel {
* constructor parameter. * constructor parameter.
*/ */
export class ConsoleLogger implements Logger { export class ConsoleLogger implements Logger {
constructor(private logLevel: LogLevel) {} constructor(public level: LogLevel) {}
debug(...args: string[]) { debug(...args: string[]) {
if (this.logLevel <= LogLevel.debug) console.debug(DEBUG, ...args); if (this.level <= LogLevel.debug) console.debug(DEBUG, ...args);
} }
info(...args: string[]) { info(...args: string[]) {
if (this.logLevel <= LogLevel.info) console.info(...args); if (this.level <= LogLevel.info) console.info(...args);
} }
warn(...args: string[]) { warn(...args: string[]) {
if (this.logLevel <= LogLevel.warn) console.warn(WARN, ...args); if (this.level <= LogLevel.warn) console.warn(WARN, ...args);
} }
error(...args: string[]) { error(...args: string[]) {
if (this.logLevel <= LogLevel.error) console.error(ERROR, ...args); if (this.level <= LogLevel.error) console.error(ERROR, ...args);
} }
} }

View File

@ -11,8 +11,16 @@
* output from the standard ConsoleLogger. * output from the standard ConsoleLogger.
*/ */
export interface Logger { export interface Logger {
level: LogLevel;
debug(...args: string[]): void; debug(...args: string[]): void;
info(...args: string[]): void; info(...args: string[]): void;
warn(...args: string[]): void; warn(...args: string[]): void;
error(...args: string[]): void; error(...args: string[]): void;
} }
export enum LogLevel {
debug,
info,
warn,
error,
}

View File

@ -31,8 +31,8 @@ import {LockFileAsync, LockFileSync} from './execution/lock_file';
import {SingleProcessExecutorAsync, SingleProcessExecutorSync} from './execution/single_process_executor'; import {SingleProcessExecutorAsync, SingleProcessExecutorSync} from './execution/single_process_executor';
import {ParallelTaskQueue} from './execution/task_selection/parallel_task_queue'; import {ParallelTaskQueue} from './execution/task_selection/parallel_task_queue';
import {SerialTaskQueue} from './execution/task_selection/serial_task_queue'; import {SerialTaskQueue} from './execution/task_selection/serial_task_queue';
import {ConsoleLogger, LogLevel} from './logging/console_logger'; import {ConsoleLogger} from './logging/console_logger';
import {Logger} from './logging/logger'; import {LogLevel, Logger} from './logging/logger';
import {hasBeenProcessed} from './packages/build_marker'; import {hasBeenProcessed} from './packages/build_marker';
import {NgccConfiguration} from './packages/configuration'; import {NgccConfiguration} from './packages/configuration';
import {EntryPoint, EntryPointJsonProperty, EntryPointPackageJson, SUPPORTED_FORMAT_PROPERTIES, getEntryPointFormat} from './packages/entry_point'; import {EntryPoint, EntryPointJsonProperty, EntryPointPackageJson, SUPPORTED_FORMAT_PROPERTIES, getEntryPointFormat} from './packages/entry_point';

View File

@ -6,10 +6,12 @@
* found in the LICENSE file at https://angular.io/license * 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 { export class MockLogger implements Logger {
logs: {[P in keyof Logger]: string[][]} = { constructor(public level = LogLevel.info) {}
logs: {[P in Exclude<keyof Logger, 'level'>]: string[][]} = {
debug: [], debug: [],
info: [], info: [],
warn: [], warn: [],

View File

@ -5,7 +5,8 @@
* Use of this source code is governed by an MIT-style license that can be * 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 * 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', () => { describe('ConsoleLogger', () => {
it('should pass through calls to Console', () => { it('should pass through calls to Console', () => {
@ -47,4 +48,11 @@ describe('ConsoleLogger', () => {
logger.error('error', 'test'); logger.error('error', 'test');
expect(console.error).toHaveBeenCalledWith(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);
});
}); });