Skip to content

Commit ba8309a

Browse files
Named loggers (#26)
* Adding error catching to json stringify, adding test * Adding support for named loggers
1 parent 316ace4 commit ba8309a

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

index.d.ts

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

2222
setLevel(level: LEVEL): void;
2323

24+
createNamedLogger(name: string): Logger;
25+
2426
setLevelNoColor(): void;
2527

2628
setLevelColor(): void;

index.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ const CONFIG = {
3636
const LEVELS = ["success", "debug", "info", "warn", "error", "disable"];
3737

3838
class Logger {
39-
constructor() {
39+
constructor(name) {
4040
// Current command
4141
this.command = '';
4242
// Last line
4343
this.lastCommand = '';
4444

45+
this.name = name || ""
46+
4547
// set level from env
4648
const level = process.env.LOGGER;
4749
if (this.isLevelValid(level)) {
@@ -53,6 +55,10 @@ class Logger {
5355
this._getDate = () => (new Date()).toISOString();
5456
}
5557

58+
createNamedLogger(name){
59+
return new Logger(name)
60+
}
61+
5662
setLevel(level) {
5763
if (this.isLevelValid(level)) {
5864
this.level = level;
@@ -130,8 +136,12 @@ class Logger {
130136
this._getDate = callback;
131137
}
132138

133-
getDate() {
134-
return this._getDate();
139+
getPrefix() {
140+
if(this.name) {
141+
return `${this._getDate()} [${this.name}]`;
142+
} else {
143+
return this._getDate();
144+
}
135145
}
136146

137147
color(ticket) {
@@ -241,10 +251,10 @@ class Logger {
241251
return;
242252

243253
if (this.noColor) {
244-
const d = this.getDate();
254+
const d = this.getPrefix();
245255
this.log(d, " [ERROR] ", ...args);
246256
} else {
247-
const d = this.getDate();
257+
const d = this.getPrefix();
248258
this.log(d + " ").joint()
249259
.bgColor('red').log('[ERROR]').joint()
250260
.log(" ").joint()
@@ -257,10 +267,10 @@ class Logger {
257267
return;
258268

259269
if (this.noColor) {
260-
const d = this.getDate();
270+
const d = this.getPrefix();
261271
this.log(d, " [WARN] ", ...args);
262272
} else {
263-
const d = this.getDate();
273+
const d = this.getPrefix();
264274
this.log(d + " ").joint()
265275
.bgColor('yellow').color('black').log('[WARN]').joint()
266276
.log(" ").joint()
@@ -273,10 +283,10 @@ class Logger {
273283
return;
274284

275285
if (this.noColor) {
276-
const d = this.getDate();
286+
const d = this.getPrefix();
277287
this.log(d, " [INFO] ", ...args);
278288
} else {
279-
const d = this.getDate();
289+
const d = this.getPrefix();
280290
this.log(d + " ").joint()
281291
.bgColor('green').color('black').log('[INFO]').joint()
282292
.log(" ").joint()
@@ -289,10 +299,10 @@ class Logger {
289299
return;
290300

291301
if (this.noColor) {
292-
const d = this.getDate();
302+
const d = this.getPrefix();
293303
this.log(d, " [DEBUG] ", ...args);
294304
} else {
295-
const d = this.getDate();
305+
const d = this.getPrefix();
296306
this.log(d + " ").joint()
297307
.bgColor('cyan').color('black').log("[DEBUG]").joint()
298308
.log(' ').joint()
@@ -306,10 +316,10 @@ class Logger {
306316
return;
307317

308318
if (this.noColor) {
309-
const d = this.getDate();
319+
const d = this.getPrefix();
310320
this.log(d, " [SUCCESS] ", ...args);
311321
} else {
312-
const d = this.getDate();
322+
const d = this.getPrefix();
313323
this.log(d + " ").joint()
314324
.bgColor('green').color('black').log("[SUCCESS]").joint()
315325
.log(' ').joint()

test4.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const logger = require('./index').createNamedLogger("Test 4");
2+
3+
// $ LOGGER=info node test2.js
4+
logger.log('****************************************');
5+
logger.log('*** Test Environment Variable LOGGER ***');
6+
logger.log('****************************************');
7+
logger.log("LOGGER=info, debug level will not show");
8+
logger.error('error show');
9+
logger.warn('warn show');
10+
logger.info('info show');
11+
logger.debug('debug will not show');
12+
logger.success('success show');

0 commit comments

Comments
 (0)