Skip to content
This repository was archived by the owner on Sep 1, 2022. It is now read-only.

Commit 81a61ce

Browse files
author
contra
committed
Merge pull request #92 from stringparser/fix-log-formatting
Fixes log formatting and tests logging
2 parents d17e0f9 + c107206 commit 81a61ce

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

lib/log.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ var dateformat = require('dateformat');
33

44
module.exports = function(){
55
var time = '['+chalk.grey(dateformat(new Date(), 'HH:MM:ss'))+']';
6-
var args = Array.prototype.slice.call(arguments);
7-
args.unshift(time);
8-
console.log.apply(console, args);
6+
process.stdout.write(time + ' ');
7+
console.log.apply(console, arguments);
98
return this;
109
};

test/log.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,47 @@ require('mocha');
44

55
describe('log()', function(){
66
it('should work i guess', function(done){
7-
var writtenValue;
7+
var count = 0;
8+
var writtenValue = '';
89

910
// Stub process.stdout.write
1011
var stdout_write = process.stdout.write;
1112
process.stdout.write = function(value) {
12-
writtenValue = value;
13+
writtenValue += value;
14+
if(++count > 2){
15+
// Restore process.stdout.write after test
16+
process.stdout.write = stdout_write;
17+
}
1318
};
1419

1520
util.log(1, 2, 3, 4, 'five');
1621
var time = util.date(new Date(), 'HH:MM:ss');
17-
writtenValue.should.eql('[' + util.colors.grey(time) + '] 1 2 3 4 five\n');
22+
writtenValue.should.eql('[' + util.colors.grey(time) + '] 1 2 3 4 \'five\'\n');
23+
24+
done();
25+
});
26+
27+
it('should accept formatting', function(done){
28+
var count = 0;
29+
var writtenValue = '';
30+
31+
// Stub process.stdout.write
32+
var stdout_write = process.stdout.write;
33+
process.stdout.write = function(value) {
34+
writtenValue += value;
35+
if(++count > 2){
36+
// Restore process.stdout.write after test
37+
process.stdout.write = stdout_write;
38+
}
39+
};
40+
41+
util.log('%s %d %j', 'something', 0.1, {key: 'value'});
42+
var time = util.date(new Date(), 'HH:MM:ss');
43+
writtenValue.should.eql(
44+
'[' + util.colors.grey(time) + '] '+
45+
'something 0.1 {\"key\":\"value\"}\n'
46+
);
1847

19-
// Restore process.stdout.write
20-
process.stdout.write = stdout_write;
2148
done();
2249
});
2350
});

0 commit comments

Comments
 (0)