1 | 'use strict'
|
2 |
|
3 | const chalk = require('chalk')
|
4 | const logUpdate = require('log-update')
|
5 |
|
6 | const ERR = chalk.red('err')
|
7 | const DBG = chalk.green('dbg')
|
8 |
|
9 | class Logger {
|
10 | static getInstance () {
|
11 | Logger.instance = Logger.instance || new Logger()
|
12 | return Logger.instance
|
13 | }
|
14 |
|
15 | enableDebug () {
|
16 | this.isDebug = true
|
17 | }
|
18 |
|
19 | clearStatus (clearLine = true) {
|
20 | logUpdate.clear()
|
21 | }
|
22 |
|
23 | dbg (name, msg) {
|
24 | if (!this.isDebug) {
|
25 | return
|
26 | }
|
27 |
|
28 | this.clearStatus()
|
29 | this.getLines(msg).forEach(line => {
|
30 | console.log(`${chalk.blue(name)} ${DBG}`, line)
|
31 | })
|
32 |
|
33 | this.printStatus()
|
34 | }
|
35 |
|
36 | err (name, msg) {
|
37 | this.clearStatus()
|
38 | this.getLines(msg).forEach(line => {
|
39 | console.error(`${chalk.blue(name)} ${ERR}`, line)
|
40 | })
|
41 |
|
42 | this.printStatus()
|
43 | }
|
44 |
|
45 | getLines (msg) {
|
46 | if (Buffer.isBuffer(msg)) {
|
47 | const trimmed = msg.toString('utf8').trim()
|
48 | return trimmed ? trimmed.split('\n') : []
|
49 | } else {
|
50 | return [msg]
|
51 | }
|
52 | }
|
53 |
|
54 | printStatus (status) {
|
55 | if (status) {
|
56 | this.status = status
|
57 | }
|
58 |
|
59 | this.isDebug || logUpdate(this.status)
|
60 | }
|
61 |
|
62 | end () {
|
63 | logUpdate.done()
|
64 | console.log('')
|
65 | }
|
66 | }
|
67 |
|
68 | module.exports = Logger
|