Skip to content

Commit e0b5684

Browse files
amandeepmittalphated
authored andcommitted
New: Add console methods such as warn, info, dir (#8)
1 parent 5e0e177 commit e0b5684

File tree

5 files changed

+173
-5
lines changed

5 files changed

+173
-5
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2015 Blaine Bublitz
3+
Copyright (c) 2015 Blaine Bublitz <blaine.bublitz@gmail.com>
44
Based on gulp-util, copyright 2014 Fractal <contact@wearefractal.com>
55

66
Permission is hereby granted, free of charge, to any person obtaining a copy

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,23 @@ current time in HH:MM:ss format.
2727

2828
### `log.error(msg...)`
2929

30-
Logs ths message as if you called `console.error` but prefixes the output with the
30+
Logs the message as if you called `console.error` but prefixes the output with the
31+
current time in HH:MM:ss format.
32+
33+
### `log.warn(msg...)`
34+
35+
Logs the message as if you called `console.warn` but prefixes the output with the
36+
current time in HH:MM:ss format.
37+
38+
39+
### `log.info(msg...)`
40+
41+
Logs the message as if you called `console.info` but prefixes the output with the
42+
current time in HH:MM:ss format.
43+
44+
### `log.dir(msg...)`
45+
46+
Logs the message as if you called `console.dir` but prefixes the output with the
3147
current time in HH:MM:ss format.
3248

3349
## License

index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,27 @@ function log(){
1616
return this;
1717
}
1818

19+
function info(){
20+
var time = getTimestamp();
21+
process.stdout.write(time + ' ');
22+
console.info.apply(console, arguments);
23+
return this;
24+
}
25+
26+
function dir(){
27+
var time = getTimestamp();
28+
process.stdout.write(time + ' ');
29+
console.dir.apply(console, arguments);
30+
return this;
31+
}
32+
33+
function warn(){
34+
var time = getTimestamp();
35+
process.stderr.write(time + ' ');
36+
console.warn.apply(console, arguments);
37+
return this;
38+
}
39+
1940
function error(){
2041
var time = getTimestamp();
2142
process.stderr.write(time + ' ');
@@ -24,4 +45,7 @@ function error(){
2445
}
2546

2647
module.exports = log;
48+
module.exports.info = info;
49+
module.exports.dir = dir;
50+
module.exports.warn = warn;
2751
module.exports.error = error;

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
"name": "fancy-log",
33
"version": "1.2.0",
44
"description": "Log things, prefixed with a timestamp",
5-
"author": "Blaine Bublitz <blaine@iceddev.com> (http://iceddev.com)",
6-
"contributors": [],
7-
"repository": "phated/fancy-log",
5+
"author": "Blaine Bublitz <blaine.bublitz@gmail.com>",
6+
"contributors": [
7+
"Aman Mittal (http://amandeepmittal.github.io/)"
8+
],
9+
"repository": "js-cli/fancy-log",
810
"license": "MIT",
911
"engines": {
1012
"node": ">= 0.10"

test.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
/*
33
Initial code from https://github.com/gulpjs/gulp-util/blob/v3.0.6/test/log.js
44
*/
5+
var util = require('util');
6+
57
var lab = exports.lab = require('lab').script();
68
var code = require('code');
79
var chalk = require('chalk');
@@ -55,6 +57,130 @@ lab.describe('log()', function(){
5557
});
5658
});
5759

60+
lab.describe('log.info()', function(){
61+
62+
var stdout_write = process.stdout.write;
63+
var writtenValue = '';
64+
65+
function writeSpy(value) {
66+
writtenValue += value;
67+
}
68+
69+
lab.afterEach(function(done){
70+
writtenValue = '';
71+
done();
72+
});
73+
74+
lab.it('should work i guess', function(done){
75+
// Stub process.stdout.write
76+
process.stdout.write = writeSpy;
77+
78+
log.info(1, 2, 3, 4, 'five');
79+
var time = timestamp('HH:mm:ss');
80+
code.expect(writtenValue).equals('[' + chalk.grey(time) + '] 1 2 3 4 \'five\'\n');
81+
82+
// Restore process.stdout.write after test
83+
process.stdout.write = stdout_write;
84+
85+
done();
86+
});
87+
88+
lab.it('should accept formatting', function(done){
89+
// Stub process.stdout.write
90+
process.stdout.write = writeSpy;
91+
92+
log.info('%s %d %j', 'something', 0.1, {key: 'value'});
93+
var time = timestamp('HH:mm:ss');
94+
code.expect(writtenValue).equals(
95+
'[' + chalk.grey(time) + '] '+
96+
'something 0.1 {\"key\":\"value\"}\n'
97+
);
98+
99+
// Restore process.stdout.write after test
100+
process.stdout.write = stdout_write;
101+
102+
done();
103+
});
104+
});
105+
106+
lab.describe('log.dir()', function(){
107+
108+
var stdout_write = process.stdout.write;
109+
var writtenValue = '';
110+
111+
function writeSpy(value) {
112+
writtenValue += value;
113+
}
114+
115+
lab.afterEach(function(done){
116+
writtenValue = '';
117+
done();
118+
});
119+
120+
lab.it('should format an object with util.inspect', function(done){
121+
// Stub process.stdout.write
122+
process.stdout.write = writeSpy;
123+
124+
log.dir({key: 'value'});
125+
var time = timestamp('HH:mm:ss');
126+
code.expect(writtenValue).equals(
127+
'[' + chalk.grey(time) + '] '+
128+
util.inspect({key:'value'}) + '\n'
129+
);
130+
131+
// Restore process.stdout.write after test
132+
process.stdout.write = stdout_write;
133+
134+
done();
135+
});
136+
});
137+
138+
lab.describe('log.warn()', function(){
139+
140+
var stderr_write = process.stderr.write;
141+
var writtenValue = '';
142+
143+
function writeSpy(value) {
144+
writtenValue += value;
145+
}
146+
147+
lab.afterEach(function(done){
148+
writtenValue = '';
149+
done();
150+
});
151+
152+
lab.it('should work i guess', function(done){
153+
// Stub process.stderr.write
154+
process.stderr.write = writeSpy;
155+
156+
log.warn(1, 2, 3, 4, 'five');
157+
var time = timestamp('HH:mm:ss');
158+
code.expect(writtenValue).equals('[' + chalk.grey(time) + '] 1 2 3 4 \'five\'\n');
159+
160+
// Restore process.stderr.write after test
161+
process.stderr.write = stderr_write;
162+
163+
done();
164+
});
165+
166+
lab.it('should accept formatting', function(done){
167+
// Stub process.stderr.write
168+
process.stderr.write = writeSpy;
169+
170+
log.warn('%s %d %j', 'something', 0.1, {key: 'value'});
171+
var time = timestamp('HH:mm:ss');
172+
code.expect(writtenValue).equals(
173+
'[' + chalk.grey(time) + '] '+
174+
'something 0.1 {\"key\":\"value\"}\n'
175+
);
176+
177+
// Restore process.stderr.write after test
178+
process.stderr.write = stderr_write;
179+
180+
done();
181+
});
182+
});
183+
58184
lab.describe('log.error()', function(){
59185

60186
var stderr_write = process.stderr.write;

0 commit comments

Comments
 (0)