Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds config option to disable colored output #57

Merged
merged 2 commits into from Jan 22, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -19,6 +19,7 @@ Creates a new GoodConsole object with the following arguments:
- `[config]` - optional configuration object with the following available keys
- `format` - [MomentJS](http://momentjs.com/docs/#/displaying/format/) format string. Defaults to 'YYMMDD/HHmmss.SSS'.
- `utc` - boolean controlling Moment using [utc mode](http://momentjs.com/docs/#/parsing/utc/) or not. Defaults to `true`.
- `color` - boolean activating the colored output or not. Defaults to `true`.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would personally phrase it as a boolean specifying whether to output in color (like in the hapi docs)


## Good Console Methods
### `goodconsole.init(stream, emitter, callback)`
Expand Down
14 changes: 10 additions & 4 deletions lib/index.js
Expand Up @@ -11,7 +11,8 @@ var Through = require('through2');
var internals = {
defaults: {
format: 'YYMMDD/HHmmss.SSS',
utc: true
utc: true,
color: true
}
};

Expand Down Expand Up @@ -157,20 +158,25 @@ internals.GoodConsole.prototype._formatWreck = function (event, tags) {

internals.GoodConsole.prototype._formatMethod = function (method) {

var color;
var methodColors = {
get: 32,
delete: 31,
put: 36,
post: 33
};
var color = methodColors[method.toLowerCase()] || 34;
return '\x1b[1;' + color + 'm' + method.toLowerCase() + '\x1b[0m';
var formattedMethod = method.toLowerCase();
if (this._settings.color) {
color = methodColors[method.toLowerCase()] || 34;
formattedMethod = '\x1b[1;' + color + 'm' + formattedMethod + '\x1b[0m';
}
return formattedMethod;
};

internals.GoodConsole.prototype._formatStatusCode = function (statusCode) {

var color;
if (statusCode) {
if (statusCode && this._settings.color) {
color = 32;
if (statusCode >= 500) {
color = 31;
Expand Down
31 changes: 31 additions & 0 deletions test/index.js
Expand Up @@ -287,6 +287,37 @@ describe('GoodConsole', function () {
});
});

it('prints request events with no colors when \'color\' config is set to false', function (done) {

var reporter = new GoodConsole({ response: '*' }, { color: false });
var now = Date.now();
var timeString = Moment(now).format(internals.defaults.format);
var event = Hoek.clone(internals.response);

StandIn.replace(process.stdout, 'write', function (stand, string, enc, callback) {

if (string.indexOf(timeString) === 0) {
stand.restore();
expect(string).to.equal(timeString + ', [response], localhost: head /data {"name":"adam"} 200 (150ms) response payload: {"foo":"bar","value":1}\n');
}
else {
stand.original(string, enc, callback);
}
});

event.timestamp = now;
event.method = 'head';

var s = internals.readStream(done);

reporter.init(s, null, function (err) {

expect(err).to.not.exist();
s.push(event);
s.push(null);
});
});

it('does not log a status code if there is not one attached', function (done) {

var reporter = new GoodConsole({ response: '*' });
Expand Down