UNPKG

1.18 kBJavaScriptView Raw
1'use strict'
2
3const chalk = require('chalk')
4const logUpdate = require('log-update')
5
6const ERR = chalk.red('err')
7const DBG = chalk.green('dbg')
8
9class 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
68module.exports = Logger