Skip to content

Commit

Permalink
feat(logging): Add colors and log-level options to run-command
Browse files Browse the repository at this point in the history
 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 karma-runner#1067
  • Loading branch information
budde377 committed Jan 20, 2016
1 parent b7d591f commit 2d29165
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 18 deletions.
3 changes: 3 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', '<disable | error | warn | info | debug> 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 () {
Expand Down
13 changes: 2 additions & 11 deletions lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -60,3 +83,4 @@ var create = function (name, level) {

exports.create = create
exports.setup = setup
exports.setupFromConfig = setupFromConfig
2 changes: 2 additions & 0 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 1 addition & 7 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 2d29165

Please sign in to comment.