From 2d2916551453724c6aa7ddd1dcb2dd9c6f9da7e2 Mon Sep 17 00:00:00 2001 From: Christian Budde Christensen Date: Tue, 5 Jan 2016 19:16:12 +0100 Subject: [PATCH] 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 --- lib/cli.js | 3 +++ lib/init.js | 13 ++----------- lib/logger.js | 24 ++++++++++++++++++++++++ lib/runner.js | 2 ++ lib/server.js | 8 +------- 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/lib/cli.js b/lib/cli.js index 520dd43d3..d8198abcf 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -185,6 +185,9 @@ var describeRun = function () { .describe('fail-on-empty-test-suite', 'Fail on empty test suite.') .describe('no-fail-on-empty-test-suite', 'Do not fail on empty test suite.') .describe('help', 'Print usage.') + .describe('log-level', ' Level of logging.') + .describe('colors', 'Use colors when reporting and printing logs.') + .describe('no-colors', 'Do not use colors when reporting or printing logs.') } var describeCompletion = function () { diff --git a/lib/init.js b/lib/init.js index 51e1eed7e..a257e05b2 100755 --- a/lib/init.js +++ b/lib/init.js @@ -6,7 +6,6 @@ var exec = require('child_process').exec var helper = require('./helper') var logger = require('./logger') -var constant = require('./constants') var log = logger.create('init') @@ -211,21 +210,13 @@ var processAnswers = function (answers, basePath, testMainFile) { } exports.init = function (config) { - var useColors = true - var logLevel = constant.LOG_INFO + logger.setupFromConfig(config) + var colorScheme = COLOR_SCHEME.ON if (helper.isDefined(config.colors)) { colorScheme = config.colors ? COLOR_SCHEME.ON : COLOR_SCHEME.OFF - useColors = config.colors - } - - if (helper.isDefined(config.logLevel)) { - logLevel = config.logLevel } - - logger.setup(logLevel, useColors) - // need to be registered before creating readlineInterface process.stdin.on('keypress', function (s, key) { sm.onKeypress(key) diff --git a/lib/logger.js b/lib/logger.js index 455081dbe..210242996 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -43,6 +43,29 @@ var setup = function (level, colors, appenders) { }) } +// Setup the logger by passing in the config object. The function sets the +// `colors` and `logLevel` if they are defined. It takes two arguments: +// +// setupFromConfig(config, appenders) +// +// * `config`: *Object* The configuration object. +// * `appenders`: *Array* This will be passed as appenders to log4js +// to allow for fine grained configuration of log4js. For more information +// see https://github.com/nomiddlename/log4js-node. +var setupFromConfig = function (config, appenders) { + var useColors = true + var logLevel = constant.LOG_INFO + + if (helper.isDefined(config.colors)) { + useColors = config.colors + } + + if (helper.isDefined(config.logLevel)) { + logLevel = config.logLevel + } + setup(logLevel, useColors, appenders) +} + // Create a new logger. There are two optional arguments // * `name`, which defaults to `karma` and // If the `name = 'socket.io'` this will create a special wrapper @@ -60,3 +83,4 @@ var create = function (name, level) { exports.create = create exports.setup = setup +exports.setupFromConfig = setupFromConfig diff --git a/lib/runner.js b/lib/runner.js index 41c43bf75..45c8a2467 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -32,6 +32,8 @@ var parseExitCode = function (buffer, defaultCode, failOnEmptyTestSuite) { // TODO(vojta): read config file (port, host, urlRoot) exports.run = function (config, done) { + logger.setupFromConfig(config) + done = helper.isFunction(done) ? done : process.exit config = cfg.parseConfig(config.configFile, config) diff --git a/lib/server.js b/lib/server.js index 892a1b55e..4c8084419 100644 --- a/lib/server.js +++ b/lib/server.js @@ -39,17 +39,11 @@ function createSocketIoServer (webServer, executor, config) { return server } -function setupLogger (level, colors) { - var logLevel = logLevel || constant.LOG_INFO - var logColors = helper.isDefined(colors) ? colors : true - logger.setup(logLevel, logColors, [constant.CONSOLE_APPENDER]) -} - // Constructor var Server = function (cliOptions, done) { EventEmitter.call(this) - setupLogger(cliOptions.logLevel, cliOptions.colors) + logger.setupFromConfig(cliOptions) this.log = logger.create()