Skip to content

Commit

Permalink
fix(browser): filter browser logging by level
Browse files Browse the repository at this point in the history
Do not log lower levels if `browserConsoleLogOptions.level` is set

Fixes  #2228
  • Loading branch information
wesleycho authored and dignifiedquire committed Jan 14, 2017
1 parent 305df2c commit 35965d9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
8 changes: 6 additions & 2 deletions docs/dev/04-public-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
10 changes: 8 additions & 2 deletions lib/reporters/base.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var util = require('util')

var constants = require('../constants')
var helper = require('../helper')

var BaseReporter = function (formatError, reportSlow, useColors, browserConsoleLogOptions, adapter) {
Expand Down Expand Up @@ -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))
}
}

Expand Down
12 changes: 12 additions & 0 deletions test/unit/reporters/base.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down

0 comments on commit 35965d9

Please sign in to comment.