Skip to content

Commit 35965d9

Browse files
wesleychodignifiedquire
authored andcommitted
fix(browser): filter browser logging by level
Do not log lower levels if `browserConsoleLogOptions.level` is set Fixes #2228
1 parent 305df2c commit 35965d9

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

docs/dev/04-public-api.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ stopper.stop({port: 9876}, function(exitCode) {
136136

137137
## karma.config.parseConfig([configFilePath], [cliOptions])
138138

139-
This function will load given config file and returns a filled config object.
139+
This function will load given config file and returns a filled config object.
140140
This can be useful if you want to integrate karma into another tool and want to load
141141
the karma config while honoring the karma defaults. For example, the [stryker-karma-runner](https://github.com/stryker-mutator/stryker-karma-runner)
142142
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
145145
const cfg = require('karma').config;
146146
const path = require('path');
147147
// Read karma.conf.js, but override port with 1337
148-
const karmaConfig = cfg.parseConfig(path.resolve('./karma.conf.js'), { port: 1337 } );
148+
const karmaConfig = cfg.parseConfig(path.resolve('./karma.conf.js'), { port: 1337 } );
149149
```
150150

151151
## karma.constants
@@ -182,6 +182,10 @@ The value for the log `info` level
182182

183183
The value for the log `debug` level
184184

185+
### **constants.LOG_PRIORITIES**
186+
187+
An array of log levels in descending order, i.e. `LOG_DISABLE`, `LOG_ERROR`, `LOG_WARN`, `LOG_INFO`, and `LOG_DEBUG`
188+
185189
### **constants.COLOR_PATTERN**
186190

187191
The default color pattern for log output

lib/constants.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ exports.LOG_ERROR = 'ERROR'
1515
exports.LOG_WARN = 'WARN'
1616
exports.LOG_INFO = 'INFO'
1717
exports.LOG_DEBUG = 'DEBUG'
18+
exports.LOG_PRIORITIES = [
19+
exports.LOG_DISABLE,
20+
exports.LOG_ERROR,
21+
exports.LOG_WARN,
22+
exports.LOG_INFO,
23+
exports.LOG_DEBUG,
24+
exports.LOG_DISABLE
25+
]
1826

1927
// Default patterns for the pattern layout.
2028
exports.COLOR_PATTERN = '%[%d{DATE}:%p [%c]: %]%m'

lib/reporters/base.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var util = require('util')
22

3+
var constants = require('../constants')
34
var helper = require('../helper')
45

56
var BaseReporter = function (formatError, reportSlow, useColors, browserConsoleLogOptions, adapter) {
@@ -65,14 +66,19 @@ var BaseReporter = function (formatError, reportSlow, useColors, browserConsoleL
6566

6667
this.onBrowserLog = function (browser, log, type) {
6768
if (!browserConsoleLogOptions || !browserConsoleLogOptions.terminal) return
69+
type = type.toUpperCase()
70+
if (browserConsoleLogOptions.level) {
71+
var logPriority = constants.LOG_PRIORITIES.indexOf(browserConsoleLogOptions.level.toUpperCase())
72+
if (constants.LOG_PRIORITIES.indexOf(type) > logPriority) return
73+
}
6874
if (!helper.isString(log)) {
6975
// TODO(vojta): change util to new syntax (config object)
7076
log = util.inspect(log, false, undefined, this.USE_COLORS)
7177
}
7278
if (this._browsers && this._browsers.length === 1) {
73-
this.writeCommonMsg(util.format(this.LOG_SINGLE_BROWSER, type.toUpperCase(), log))
79+
this.writeCommonMsg(util.format(this.LOG_SINGLE_BROWSER, type, log))
7480
} else {
75-
this.writeCommonMsg(util.format(this.LOG_MULTI_BROWSER, browser, type.toUpperCase(), log))
81+
this.writeCommonMsg(util.format(this.LOG_MULTI_BROWSER, browser, type, log))
7682
}
7783
}
7884

test/unit/reporters/base.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ describe('reporter', function () {
8383
return expect(writeSpy).to.have.been.calledWith('LOG: Message\n')
8484
})
8585

86+
it('should not log if lower priority than browserConsoleLogOptions.level', function () {
87+
var reporter = new m.BaseReporter(null, null, true, {
88+
browserConsoleLogOptions: {level: 'ERROR'}
89+
}, adapter)
90+
var writeSpy = sinon.spy(reporter, 'writeCommonMsg')
91+
92+
reporter._browsers = ['Chrome']
93+
reporter.onBrowserLog('Chrome', 'Message', 'LOG')
94+
95+
return writeSpy.should.have.not.been.called
96+
})
97+
8698
return it('should format log messages correctly for multi browsers', function () {
8799
var writeSpy = sinon.spy(reporter, 'writeCommonMsg')
88100

0 commit comments

Comments
 (0)