UNPKG

1.14 kBPlain TextView Raw
1let gutil = require('gulp-util')
2let c = gutil.colors;
3let pkg = require('../package.json');
4
5enum LEVEL {
6 INFO,
7 DEBUG,
8 ERROR
9}
10
11class Logger {
12
13 name;
14 logger;
15 version;
16 silent;
17
18 constructor() {
19 this.name = pkg.name;
20 this.version = pkg.version;
21 this.logger = gutil.log;
22 this.silent = true;
23 }
24
25 info(...args) {
26 if(!this.silent) return;
27 this.logger(
28 this.format(LEVEL.INFO, ...args)
29 );
30 }
31
32 error(...args) {
33 if(!this.silent) return;
34 this.logger(
35 this.format(LEVEL.ERROR, ...args)
36 );
37 }
38
39 debug(...args) {
40 if(!this.silent) return;
41 this.logger(
42 this.format(LEVEL.DEBUG, ...args)
43 );
44 }
45
46 private format(level, ...args) {
47
48 let pad = (s, l, c='') => {
49 return s + Array( Math.max(0, l - s.length + 1)).join( c )
50 };
51
52 let msg = args.join(' ');
53 if(args.length > 1) {
54 msg = `${ pad(args.shift(), 15, ' ') }: ${ args.join(' ') }`;
55 }
56
57
58 switch(level) {
59 case LEVEL.INFO:
60 msg = c.green(msg);
61 break;
62
63 case LEVEL.DEBUG:
64 msg = c.cyan(msg);
65 break;
66
67 case LEVEL.ERROR:
68 msg = c.red(msg);
69 break;
70 }
71
72 return [
73 msg
74 ].join('');
75 }
76}
77
78export let logger = new Logger();