From 3a614972886a36bf7284096d68ba3aa0dca08f59 Mon Sep 17 00:00:00 2001 From: Bastien Caudan Date: Mon, 12 Feb 2018 13:47:39 +0100 Subject: [PATCH] feat(logger): Add log level configuration (#1451) --- lib/cli.ts | 4 +++- lib/config.ts | 7 +++++++ lib/logger.ts | 4 +++- spec/unit/logger_test.js | 25 +++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/cli.ts b/lib/cli.ts index c62f3cee8..af7763f44 100644 --- a/lib/cli.ts +++ b/lib/cli.ts @@ -100,6 +100,7 @@ let allowedNames = [ 'frameworkPath', 'elementExplorer', 'debug', + 'logLevel', 'disableChecks', 'browser', 'name', @@ -134,7 +135,8 @@ let optimistOptions: any = { troubleshoot: 'Turn on troubleshooting output', elementExplorer: 'Interactively test Protractor commands', debuggerServerPort: 'Start a debugger server at specified port instead of repl', - disableChecks: 'disable cli checks' + disableChecks: 'Disable cli checks', + logLevel: 'Define Protractor log level [ERROR, WARN, INFO, DEBUG]' }, aliases: { browser: 'capabilities.browserName', diff --git a/lib/config.ts b/lib/config.ts index e07981361..043ed9202 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -565,6 +565,13 @@ export interface Config { */ highlightDelay?: number; + /** + * Protractor log level + * + * default: INFO + */ + logLevel?: 'ERROR'|'WARN'|'INFO'|'DEBUG'; + // --------------------------------------------------------------------------- // ----- The test framework // -------------------------------------------------- diff --git a/lib/logger.ts b/lib/logger.ts index 1de598170..12f007b1e 100644 --- a/lib/logger.ts +++ b/lib/logger.ts @@ -54,6 +54,8 @@ export class Logger { static set(config: Config): void { if (config.troubleshoot) { Logger.logLevel = LogLevel.DEBUG; + } else if (config.logLevel) { + Logger.logLevel = LogLevel[config.logLevel]; } } @@ -139,7 +141,7 @@ export class Logger { } break; default: - throw new Error('Log level undefined'); + throw new Error('Invalid log level'); } } diff --git a/spec/unit/logger_test.js b/spec/unit/logger_test.js index 7da6e2ecd..f73378bf6 100644 --- a/spec/unit/logger_test.js +++ b/spec/unit/logger_test.js @@ -125,4 +125,29 @@ describe('the logger', function() { expect(linesSplit[1]).toContain('{"foo":"bar"} ["foo","bar","foobar"] foobar'); }); }); + + describe('default log level is configurable', function () { + beforeEach(function() { + Logger.logLevel = LogLevel.ERROR; + }); + + afterEach(function() { + Logger.logLevel = LogLevel.DEBUG; + }); + + it('should be configurable statically', function () { + Logger.logLevel = LogLevel.WARN; + expect(Logger.logLevel).toBe(LogLevel.WARN); + }); + + it('should be configurable with "troubleshoot" property', function () { + Logger.set({ troubleshoot: true }); + expect(Logger.logLevel).toBe(LogLevel.DEBUG); + }); + + it('should be configurable with "logLevel" property', function () { + Logger.set({ logLevel: 'WARN' }); + expect(Logger.logLevel).toBe(LogLevel.WARN); + }); + }); });