From 35965d9e996d462e471044b338ac038d2787c26d Mon Sep 17 00:00:00 2001 From: Wesley Cho Date: Sat, 14 Jan 2017 11:39:30 -0800 Subject: [PATCH] fix(browser): filter browser logging by level Do not log lower levels if `browserConsoleLogOptions.level` is set Fixes #2228 --- docs/dev/04-public-api.md | 8 ++++++-- lib/constants.js | 8 ++++++++ lib/reporters/base.js | 10 ++++++++-- test/unit/reporters/base.spec.js | 12 ++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/docs/dev/04-public-api.md b/docs/dev/04-public-api.md index 00e6d7528..ee9617a06 100644 --- a/docs/dev/04-public-api.md +++ b/docs/dev/04-public-api.md @@ -136,7 +136,7 @@ stopper.stop({port: 9876}, function(exitCode) { ## karma.config.parseConfig([configFilePath], [cliOptions]) -This function will load given config file and returns a filled config object. +This function will load given config file and returns a filled config object. This can be useful if you want to integrate karma into another tool and want to load the karma config while honoring the karma defaults. For example, the [stryker-karma-runner](https://github.com/stryker-mutator/stryker-karma-runner) uses this to load your karma configuration and use that in the stryker configuration. @@ -145,7 +145,7 @@ uses this to load your karma configuration and use that in the stryker configura const cfg = require('karma').config; const path = require('path'); // Read karma.conf.js, but override port with 1337 -const karmaConfig = cfg.parseConfig(path.resolve('./karma.conf.js'), { port: 1337 } ); +const karmaConfig = cfg.parseConfig(path.resolve('./karma.conf.js'), { port: 1337 } ); ``` ## karma.constants @@ -182,6 +182,10 @@ The value for the log `info` level The value for the log `debug` level +### **constants.LOG_PRIORITIES** + +An array of log levels in descending order, i.e. `LOG_DISABLE`, `LOG_ERROR`, `LOG_WARN`, `LOG_INFO`, and `LOG_DEBUG` + ### **constants.COLOR_PATTERN** The default color pattern for log output diff --git a/lib/constants.js b/lib/constants.js index 6da52abd0..11b5e3ad3 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -15,6 +15,14 @@ exports.LOG_ERROR = 'ERROR' exports.LOG_WARN = 'WARN' exports.LOG_INFO = 'INFO' exports.LOG_DEBUG = 'DEBUG' +exports.LOG_PRIORITIES = [ + exports.LOG_DISABLE, + exports.LOG_ERROR, + exports.LOG_WARN, + exports.LOG_INFO, + exports.LOG_DEBUG, + exports.LOG_DISABLE +] // Default patterns for the pattern layout. exports.COLOR_PATTERN = '%[%d{DATE}:%p [%c]: %]%m' diff --git a/lib/reporters/base.js b/lib/reporters/base.js index 7d936d81f..5ed57ced2 100644 --- a/lib/reporters/base.js +++ b/lib/reporters/base.js @@ -1,5 +1,6 @@ var util = require('util') +var constants = require('../constants') var helper = require('../helper') var BaseReporter = function (formatError, reportSlow, useColors, browserConsoleLogOptions, adapter) { @@ -65,14 +66,19 @@ var BaseReporter = function (formatError, reportSlow, useColors, browserConsoleL this.onBrowserLog = function (browser, log, type) { if (!browserConsoleLogOptions || !browserConsoleLogOptions.terminal) return + type = type.toUpperCase() + if (browserConsoleLogOptions.level) { + var logPriority = constants.LOG_PRIORITIES.indexOf(browserConsoleLogOptions.level.toUpperCase()) + if (constants.LOG_PRIORITIES.indexOf(type) > logPriority) return + } if (!helper.isString(log)) { // TODO(vojta): change util to new syntax (config object) log = util.inspect(log, false, undefined, this.USE_COLORS) } if (this._browsers && this._browsers.length === 1) { - this.writeCommonMsg(util.format(this.LOG_SINGLE_BROWSER, type.toUpperCase(), log)) + this.writeCommonMsg(util.format(this.LOG_SINGLE_BROWSER, type, log)) } else { - this.writeCommonMsg(util.format(this.LOG_MULTI_BROWSER, browser, type.toUpperCase(), log)) + this.writeCommonMsg(util.format(this.LOG_MULTI_BROWSER, browser, type, log)) } } diff --git a/test/unit/reporters/base.spec.js b/test/unit/reporters/base.spec.js index 57813f743..a0c31477b 100644 --- a/test/unit/reporters/base.spec.js +++ b/test/unit/reporters/base.spec.js @@ -83,6 +83,18 @@ describe('reporter', function () { return expect(writeSpy).to.have.been.calledWith('LOG: Message\n') }) + it('should not log if lower priority than browserConsoleLogOptions.level', function () { + var reporter = new m.BaseReporter(null, null, true, { + browserConsoleLogOptions: {level: 'ERROR'} + }, adapter) + var writeSpy = sinon.spy(reporter, 'writeCommonMsg') + + reporter._browsers = ['Chrome'] + reporter.onBrowserLog('Chrome', 'Message', 'LOG') + + return writeSpy.should.have.not.been.called + }) + return it('should format log messages correctly for multi browsers', function () { var writeSpy = sinon.spy(reporter, 'writeCommonMsg')