Skip to content

Commit 2d29165

Browse files
committed
feat(logging): Add colors and log-level options to run-command
Add colors and log-level arguments to run argument. Refactor log-setup functions for server and init. Correct bug in server where log-level was ignored before `parseConfig` Closing #1067
1 parent b7d591f commit 2d29165

5 files changed

Lines changed: 32 additions & 18 deletions

File tree

lib/cli.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ var describeRun = function () {
185185
.describe('fail-on-empty-test-suite', 'Fail on empty test suite.')
186186
.describe('no-fail-on-empty-test-suite', 'Do not fail on empty test suite.')
187187
.describe('help', 'Print usage.')
188+
.describe('log-level', '<disable | error | warn | info | debug> Level of logging.')
189+
.describe('colors', 'Use colors when reporting and printing logs.')
190+
.describe('no-colors', 'Do not use colors when reporting or printing logs.')
188191
}
189192

190193
var describeCompletion = function () {

lib/init.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ var exec = require('child_process').exec
66

77
var helper = require('./helper')
88
var logger = require('./logger')
9-
var constant = require('./constants')
109

1110
var log = logger.create('init')
1211

@@ -211,21 +210,13 @@ var processAnswers = function (answers, basePath, testMainFile) {
211210
}
212211

213212
exports.init = function (config) {
214-
var useColors = true
215-
var logLevel = constant.LOG_INFO
213+
logger.setupFromConfig(config)
214+
216215
var colorScheme = COLOR_SCHEME.ON
217216

218217
if (helper.isDefined(config.colors)) {
219218
colorScheme = config.colors ? COLOR_SCHEME.ON : COLOR_SCHEME.OFF
220-
useColors = config.colors
221-
}
222-
223-
if (helper.isDefined(config.logLevel)) {
224-
logLevel = config.logLevel
225219
}
226-
227-
logger.setup(logLevel, useColors)
228-
229220
// need to be registered before creating readlineInterface
230221
process.stdin.on('keypress', function (s, key) {
231222
sm.onKeypress(key)

lib/logger.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,29 @@ var setup = function (level, colors, appenders) {
4343
})
4444
}
4545

46+
// Setup the logger by passing in the config object. The function sets the
47+
// `colors` and `logLevel` if they are defined. It takes two arguments:
48+
//
49+
// setupFromConfig(config, appenders)
50+
//
51+
// * `config`: *Object* The configuration object.
52+
// * `appenders`: *Array* This will be passed as appenders to log4js
53+
// to allow for fine grained configuration of log4js. For more information
54+
// see https://github.com/nomiddlename/log4js-node.
55+
var setupFromConfig = function (config, appenders) {
56+
var useColors = true
57+
var logLevel = constant.LOG_INFO
58+
59+
if (helper.isDefined(config.colors)) {
60+
useColors = config.colors
61+
}
62+
63+
if (helper.isDefined(config.logLevel)) {
64+
logLevel = config.logLevel
65+
}
66+
setup(logLevel, useColors, appenders)
67+
}
68+
4669
// Create a new logger. There are two optional arguments
4770
// * `name`, which defaults to `karma` and
4871
// If the `name = 'socket.io'` this will create a special wrapper
@@ -60,3 +83,4 @@ var create = function (name, level) {
6083

6184
exports.create = create
6285
exports.setup = setup
86+
exports.setupFromConfig = setupFromConfig

lib/runner.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ var parseExitCode = function (buffer, defaultCode, failOnEmptyTestSuite) {
3232

3333
// TODO(vojta): read config file (port, host, urlRoot)
3434
exports.run = function (config, done) {
35+
logger.setupFromConfig(config)
36+
3537
done = helper.isFunction(done) ? done : process.exit
3638
config = cfg.parseConfig(config.configFile, config)
3739

lib/server.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,11 @@ function createSocketIoServer (webServer, executor, config) {
3939
return server
4040
}
4141

42-
function setupLogger (level, colors) {
43-
var logLevel = logLevel || constant.LOG_INFO
44-
var logColors = helper.isDefined(colors) ? colors : true
45-
logger.setup(logLevel, logColors, [constant.CONSOLE_APPENDER])
46-
}
47-
4842
// Constructor
4943
var Server = function (cliOptions, done) {
5044
EventEmitter.call(this)
5145

52-
setupLogger(cliOptions.logLevel, cliOptions.colors)
46+
logger.setupFromConfig(cliOptions)
5347

5448
this.log = logger.create()
5549

0 commit comments

Comments
 (0)