Skip to content

Commit 4a4f293

Browse files
committed
11.0.0
1 parent f2907c1 commit 4a4f293

File tree

11 files changed

+146
-74
lines changed

11 files changed

+146
-74
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.test.log

README.md

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ log with attributes, the order of setters can change.
5050

5151
**NOTE:** `log()` need to put behind of attribute setter(at the end).
5252

53-
`joint()` can connect different style of message in a line.
53+
`append()` can appends the contents in one line. (Note: old `joint` is now deprecated.)
5454

5555
`color()` and `bgColor()` includes: `black`, `red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, `white`.
5656

57+
`reset()` can clear the previous attributes in the same line.
58+
5759
Usage:
5860

5961
```javascript
@@ -63,15 +65,38 @@ logger.log(message)
6365
logger.color('red').bgColor('blue')
6466
.bold().italic().dim().reverse().underscore().strikethrough()
6567
.log(message);
66-
// Joint log
67-
logger.color('red').bold().log(message_style_1).joint()
68-
.bgColor('white').italic().log(message_style_2).joint()
69-
.strikethrough().log(message_style_3);
68+
// use `append`` to joint contents, and use `log` to print out at the end
69+
logger.color('red').bold().append('message_style_1')
70+
.bgColor('white').italic().append('message_style_2')
71+
.strikethrough().log('message_style_3');
72+
73+
// use `reset` to clean the attributes
74+
logger.bgColor('red').append('background red color message')
75+
.reset() // by calling this, background is reset
76+
.log('default background color message');
7077

7178
// log multiple arguments
7279
logger.log(obj1, arr2, str3);
7380
```
7481

82+
### `setLogStream`
83+
84+
You can redirect the logs to the stream.
85+
86+
for example, you can write the log into the file:
87+
88+
```js
89+
fileStream = fs.createWriteStream('test.log'),
90+
logger.setLogStream(fileStream)
91+
92+
logger.log("hi");
93+
logger.error("hello", "world");
94+
95+
fileStream.close()
96+
```
97+
98+
you can use `less -R test.log` to see the result.
99+
75100
### `fontColorLog()`, `bgColorLog()`, `colorLog()`
76101

77102
- `message` here must be a string.
@@ -189,18 +214,6 @@ logger1.info('something happened'); // 2022-08-20T04:56:17.834Z [Test 1] [INFO]
189214
logger2.info('something happened'); // 2022-08-20T04:56:17.835Z [Test 2] [INFO] something happened
190215
```
191216

192-
### Log Files
193-
194-
If you want to save the logs to files, you can use shell pipes:
195-
196-
For example, it saves logs belonged and above `warn` to `log.txt`:
197-
198-
```shell
199-
$ LOGGER=warn node index.js > log.txt
200-
```
201-
202-
To see more example, you can check `./test.js` or run `npm test` to see the result.
203-
204217
### Contribute
205218

206219
Any issues and PRs are very welcome!

change.log.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
11.0.0
2+
- add `append()` and deprecate `joint`, add `reset`, add `setLogStream` to redirect logs to the stream
3+
14
10.0.2
25
- fix typescript namespace issues
36

index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ declare class Logger {
2525

2626
createNamedLogger(name: string): Logger;
2727

28+
setLogStream(stream: Stream): Logger;
29+
2830
setLevelNoColor(): void;
2931

3032
setLevelColor(): void;
@@ -37,6 +39,10 @@ declare class Logger {
3739

3840
joint(): Logger;
3941

42+
append(...args: any[]): Logger;
43+
44+
reset(): Logger;
45+
4046
setDate(callback: Function): void;
4147

4248
getDate(): string;

index.js

Lines changed: 73 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ class Logger {
5353
this.noColor = false;
5454

5555
this._getDate = () => (new Date()).toISOString();
56+
57+
this._customizedConsole = console;
5658
}
5759

58-
createNamedLogger(name){
60+
createNamedLogger(name) {
5961
return new Logger(name)
6062
}
6163

@@ -68,6 +70,14 @@ class Logger {
6870

6971
}
7072

73+
setLogStream(newStream) {
74+
if (newStream.writable) {
75+
this._customizedConsole = new console.Console(newStream);
76+
} else {
77+
throw "invalid writable stream object";
78+
}
79+
}
80+
7181
setLevelNoColor() {
7282
this.noColor = true;
7383
}
@@ -85,36 +95,23 @@ class Logger {
8595
}
8696

8797
log(...args) {
88-
89-
for (const idx in args) {
90-
const arg = args[idx];
91-
if (typeof arg === "string") {
92-
this.command += arg;
93-
} else {
94-
try {
95-
this.command += JSON.stringify(arg);
96-
} catch {
97-
this.command += arg;
98-
}
99-
}
100-
if (args.length > 1 && idx < args.length - 1) {
101-
this.command += " ";
102-
}
103-
}
104-
98+
this.append(...args);
10599
if (!this.noColor) {
106100
this.command += CONFIG.SYSTEM.reset;
107101
}
108-
console.log(this.command);
102+
this._print(this.command);
109103
// Save last command if we need to use for joint
110104
this.lastCommand = this.command;
111105
this.command = '';
112106
return this;
113107
}
114108

109+
// deprecated
115110
joint() {
111+
console.error("node-color-log warning: `joint` is deprecated, please use `append`");
112+
116113
// Clear the last line
117-
console.log(CONFIG.SYSTEM.backoneline + CONFIG.SYSTEM.cleanthisline);
114+
this._print(CONFIG.SYSTEM.backoneline + CONFIG.SYSTEM.cleanthisline);
118115

119116
// Reset the command to let it joint the next
120117
// And print from the position of last line
@@ -137,7 +134,7 @@ class Logger {
137134
}
138135

139136
getPrefix() {
140-
if(this.name) {
137+
if (this.name) {
141138
return `${this._getDate()} [${this.name}]`;
142139
} else {
143140
return this._getDate();
@@ -148,7 +145,7 @@ class Logger {
148145
if (ticket in CONFIG.FONT) {
149146
this.command += CONFIG.FONT[ticket];
150147
} else {
151-
this.warn("node-color-log: Font color not found! Use the default.")
148+
console.error("node-color-log warning: Font color not found! Use the default.")
152149
}
153150
return this;
154151
}
@@ -157,7 +154,7 @@ class Logger {
157154
if (ticket in CONFIG.BACKGROUND) {
158155
this.command += CONFIG.BACKGROUND[ticket];
159156
} else {
160-
this.warn("node-color-log: Background color not found! Use the default.")
157+
console.error("node-color-log warning: Background color not found! Use the default.")
161158
}
162159
return this;
163160
}
@@ -200,12 +197,12 @@ class Logger {
200197
if (ticket in CONFIG.FONT) {
201198
command += CONFIG.FONT[ticket];
202199
} else {
203-
this.warn("node-color-log: Font color not found! Use the default.")
200+
console.error("node-color-log warning: Font color not found! Use the default.")
204201
}
205202
command += text;
206203

207204
command += CONFIG.SYSTEM.reset;
208-
console.log(command);
205+
this._print(command);
209206
}
210207

211208
bgColorLog(ticket, text, setting) {
@@ -216,12 +213,12 @@ class Logger {
216213
if (ticket in CONFIG.BACKGROUND) {
217214
command += CONFIG.BACKGROUND[ticket];
218215
} else {
219-
this.warn("node-color-log: Background color not found! Use the default.")
216+
console.error("node-color-log warning: Background color not found! Use the default.")
220217
}
221218
command += text;
222219

223220
command += CONFIG.SYSTEM.reset;
224-
console.log(command);
221+
this._print(command);
225222
}
226223

227224
colorLog(ticketObj, text, setting) {
@@ -232,18 +229,18 @@ class Logger {
232229
if (ticketObj.font in CONFIG.FONT) {
233230
command += CONFIG.FONT[ticketObj.font];
234231
} else {
235-
this.warn("node-color-log: Font color not found! Use the default.")
232+
console.error("node-color-log warning: Font color not found! Use the default.")
236233
}
237234
if (ticketObj.bg in CONFIG.BACKGROUND) {
238235
command += CONFIG.BACKGROUND[ticketObj.bg]
239236
} else {
240-
this.warn("node-color-log: Background color not found! Use the default.")
237+
console.error("node-color-log warning: Background color not found! Use the default.")
241238
}
242239

243240
command += text;
244241

245242
command += CONFIG.SYSTEM.reset;
246-
console.log(command);
243+
this._print(command);
247244
}
248245

249246
error(...args) {
@@ -255,9 +252,9 @@ class Logger {
255252
this.log(d, " [ERROR] ", ...args);
256253
} else {
257254
const d = this.getPrefix();
258-
this.log(d + " ").joint()
259-
.bgColor('red').log('[ERROR]').joint()
260-
.log(" ").joint()
255+
this.append(d + " ")
256+
.bgColor('red').append('[ERROR]').reset()
257+
.append(" ")
261258
.color('red').log(...args);
262259
}
263260
}
@@ -271,9 +268,9 @@ class Logger {
271268
this.log(d, " [WARN] ", ...args);
272269
} else {
273270
const d = this.getPrefix();
274-
this.log(d + " ").joint()
275-
.bgColor('yellow').color('black').log('[WARN]').joint()
276-
.log(" ").joint()
271+
this.append(d + " ")
272+
.bgColor('yellow').color('black').append('[WARN]').reset()
273+
.append(" ")
277274
.color('yellow').log(...args);
278275
}
279276
}
@@ -287,9 +284,9 @@ class Logger {
287284
this.log(d, " [INFO] ", ...args);
288285
} else {
289286
const d = this.getPrefix();
290-
this.log(d + " ").joint()
291-
.bgColor('green').color('black').log('[INFO]').joint()
292-
.log(" ").joint()
287+
this.append(d + " ")
288+
.bgColor('green').color('black').append('[INFO]').reset()
289+
.append(" ")
293290
.color('green').log(...args);
294291
}
295292
}
@@ -303,9 +300,9 @@ class Logger {
303300
this.log(d, " [DEBUG] ", ...args);
304301
} else {
305302
const d = this.getPrefix();
306-
this.log(d + " ").joint()
307-
.bgColor('cyan').color('black').log("[DEBUG]").joint()
308-
.log(' ').joint()
303+
this.append(d + " ")
304+
.bgColor('cyan').color('black').append("[DEBUG]").reset()
305+
.append(' ')
309306
.color('cyan')
310307
.log(...args);
311308
}
@@ -320,9 +317,9 @@ class Logger {
320317
this.log(d, " [SUCCESS] ", ...args);
321318
} else {
322319
const d = this.getPrefix();
323-
this.log(d + " ").joint()
324-
.bgColor('green').color('black').log("[SUCCESS]").joint()
325-
.log(' ').joint()
320+
this.append(d + " ")
321+
.bgColor('green').color('black').append("[SUCCESS]").reset()
322+
.append(' ')
326323
.color('green')
327324
.log(...args);
328325
}
@@ -336,15 +333,44 @@ class Logger {
336333
if (setting[item] === true) {
337334
command += CONFIG.SYSTEM[item];
338335
} else if (setting[item] !== false) {
339-
this.warn(`node-color-log: The value ${item} should be boolean.`)
336+
console.error(`node-color-log warning: The value ${item} should be boolean.`)
340337
}
341338
} else {
342-
this.warn(`node-color-log: ${item} is not valid in setting.`)
339+
console.error(`node-color-log warning: ${item} is not valid in setting.`)
343340
}
344341
}
345342
return command;
346343
}
347344

345+
// helper function to output the the log to stream
346+
_print(...args) {
347+
this._customizedConsole.log(...args);
348+
}
349+
350+
// helper function to append the command buffer
351+
append(...args) {
352+
for (const idx in args) {
353+
const arg = args[idx];
354+
if (typeof arg === "string") {
355+
this.command += arg;
356+
} else {
357+
try {
358+
this.command += JSON.stringify(arg);
359+
} catch {
360+
this.command += arg;
361+
}
362+
}
363+
if (args.length > 1 && idx < args.length - 1) {
364+
this.command += " ";
365+
}
366+
}
367+
return this;
368+
}
369+
370+
reset() {
371+
this.command += CONFIG.SYSTEM.reset;
372+
return this;
373+
}
348374
}
349375

350376
const logger = new Logger();

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "node-color-log",
3-
"version": "10.0.2",
3+
"version": "11.0.0",
44
"description": "The more powerful JavaScript logger for NodeJS and browsers.",
55
"main": "index.js",
66
"scripts": {
7-
"test": "node test.js && LOGGER=info node test2.js && node test3.js && LOGGER=info node test4.js"
7+
"test": "node test/test.js && LOGGER=info node test/test2.js && node test/test3.js && LOGGER=info node test/test4.js && LOGGER=info node test/test_stream.js"
88
},
99
"repository": {
1010
"type": "git",

0 commit comments

Comments
 (0)