Skip to content

Commit

Permalink
Fix colorize not working in non-TTY environments
Browse files Browse the repository at this point in the history
  • Loading branch information
fiznool committed Nov 18, 2015
1 parent edbf0d2 commit bf8d133
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/winston/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

var colors = require('colors/safe');

// Fix colors not appearing in non-tty environments
colors.enabled = true;

var config = exports,
allColors = exports.allColors = {};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"stack-trace": "0.0.x"
},
"devDependencies": {
"cross-spawn-async": "^2.0.0",
"hock": "1.x.x",
"std-mocks": "~1.0.0",
"vows": "0.7.x"
Expand Down
43 changes: 43 additions & 0 deletions test/colorize-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* colorize-test.js: Tests for colorizing in non-TTY environments.
*
* (C) 2015 Tom Spencer
* MIT LICENSE
*
*/

var path = require('path'),
vows = require('vows'),
assert = require('assert'),
winston = require('../lib/winston'),
spawn = require('cross-spawn-async');

var spawnTest = function(colorize, cb) {
var data = '';
var ps = spawn(process.execPath, [path.join(__dirname, 'fixtures', 'scripts', 'colorize.js'), colorize], { stdio: 'pipe' });

ps.stdout.on('data', function(buf) {
data += buf.toString();
});

ps.on('close', function() {
cb(null, data);
});
};

vows.describe('winston/colorize').addBatch({
"When using winston in a non-TTY environment": {
"the logger when setup with colorize: true": {
topic: function() { spawnTest(true, this.callback) },
"should colorize": function (log) {
assert.strictEqual(log, '\u001b[32minfo\u001b[39m: Simply a test\n');
}
},
"the logger when setup with colorize: false": {
topic: function() { spawnTest(false, this.callback) },
"should not colorize": function (log) {
assert.strictEqual(log, 'info: Simply a test\n');
}
}
}
}).export(module);
16 changes: 16 additions & 0 deletions test/fixtures/scripts/colorize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* colorize.js: A test fixture for logging colorized messages
*
* (C) 2015 Tom Spencer
* MIT LICENCE
*
*/

var winston = require('../../../lib/winston');

var logger = new (winston.Logger)({
transports: [
new winston.transports.Console({ colorize: process.argv[2] === 'true' })
]
});
logger.info('Simply a test');

0 comments on commit bf8d133

Please sign in to comment.