1 | 'use strict'
|
2 |
|
3 | const Console = require('console').Console
|
4 |
|
5 | class Log extends Console {
|
6 | static getInstance () {
|
7 | Log.instance = Log.instance || new Log(process.stdout, process.stderr)
|
8 | return Log.instance
|
9 | }
|
10 |
|
11 | constructor (stdout, stderr) {
|
12 | super(stdout, stderr)
|
13 | this.prefix = ''
|
14 | }
|
15 |
|
16 | enableDebug () {
|
17 | this.isDebug = true
|
18 | }
|
19 |
|
20 | disableDebug () {
|
21 | this.isDebug = false
|
22 | }
|
23 |
|
24 | _format (args, color) {
|
25 | args = Array.apply(null, args)
|
26 | this.prefix && args.unshift(this.prefix.slice(0, -1))
|
27 |
|
28 | return args.map(arg => {
|
29 | if (typeof arg === 'string') {
|
30 | arg = arg.replace(/\n/g, '\n' + this.prefix)
|
31 | arg = color ? color(arg) : arg
|
32 | }
|
33 |
|
34 | return arg
|
35 | })
|
36 | }
|
37 |
|
38 | gray (text) {
|
39 | return `\x1b[90m${text}\x1b[0m`
|
40 | }
|
41 |
|
42 | group () {
|
43 | this.prefix += ' '
|
44 | }
|
45 |
|
46 | groupEnd () {
|
47 | this.prefix = this.prefix.slice(0, -2)
|
48 | }
|
49 |
|
50 | info () {
|
51 | this.log.apply(this, this._format(arguments))
|
52 | }
|
53 |
|
54 | debug () {
|
55 | this.isDebug && super.log.apply(this, this._format(arguments, this.gray))
|
56 | }
|
57 | }
|
58 |
|
59 | module.exports = Log.getInstance()
|