|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/** |
| 4 | + * Module dependencies. |
| 5 | + */ |
| 6 | + |
1 | 7 | const chalk = require('chalk')
|
2 |
| -const env = require('./env') |
3 |
| - |
4 |
| -const logger = {} |
5 |
| - |
6 |
| -const logTypes = { |
7 |
| - success: { |
8 |
| - color: 'green', |
9 |
| - label: 'DONE' |
10 |
| - }, |
11 |
| - error: { |
12 |
| - color: 'red', |
13 |
| - label: 'FAIL' |
14 |
| - }, |
15 |
| - warn: { |
16 |
| - color: 'yellow', |
17 |
| - label: 'WARN' |
18 |
| - }, |
19 |
| - tip: { |
20 |
| - color: 'cyan', |
21 |
| - label: 'TIP' |
22 |
| - }, |
23 |
| - wait: { |
24 |
| - color: 'blue', |
25 |
| - label: 'WAIT' |
| 8 | + |
| 9 | +class Logger { |
| 10 | + constructor (options) { |
| 11 | + this.options = Object.assign( |
| 12 | + { |
| 13 | + logLevel: 3 |
| 14 | + }, |
| 15 | + options |
| 16 | + ) |
26 | 17 | }
|
27 |
| -} |
28 | 18 |
|
29 |
| -const getLoggerFn = (color, label) => (msg, log = true) => { |
30 |
| - let newLine = false |
31 |
| - if (msg.startsWith('\n')) { |
32 |
| - if (log) msg = msg.slice(1) |
33 |
| - newLine = true |
| 19 | + setOptions (options) { |
| 20 | + Object.assign(this.options, options) |
| 21 | + } |
| 22 | + |
| 23 | + // level: 4 |
| 24 | + debug (...args) { |
| 25 | + if (this.options.logLevel < 4) { |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + this.status('magenta', 'debug', ...args) |
34 | 30 | }
|
35 |
| - msg = chalk.reset.inverse.bold[color](` ${label} `) + ' ' + msg |
36 |
| - if (log) { |
37 |
| - console.log(newLine ? '\n' + msg : msg) |
38 |
| - } else { |
39 |
| - return msg |
| 31 | + |
| 32 | + // level: 2 |
| 33 | + warn (...args) { |
| 34 | + if (this.options.logLevel < 2) { |
| 35 | + return |
| 36 | + } |
| 37 | + console.warn(chalk.yellow('warning'), ...args) |
40 | 38 | }
|
41 |
| -} |
42 | 39 |
|
43 |
| -for (const type in logTypes) { |
44 |
| - const { color, label } = logTypes[type] |
45 |
| - logger[type] = getLoggerFn(color, label) |
46 |
| -} |
| 40 | + // level: 1 |
| 41 | + error (...args) { |
| 42 | + if (this.options.logLevel < 1) { |
| 43 | + return |
| 44 | + } |
| 45 | + process.exitCode = process.exitCode || 1 |
| 46 | + console.error(chalk.red('error'), ...args) |
| 47 | + } |
| 48 | + |
| 49 | + // level: 3 |
| 50 | + success (...args) { |
| 51 | + this.status('green', 'success', ...args) |
| 52 | + } |
| 53 | + |
| 54 | + // level: 3 |
| 55 | + tip (...args) { |
| 56 | + this.status('blue', 'tip', ...args) |
| 57 | + } |
47 | 58 |
|
48 |
| -const debugFn = getLoggerFn('magenta', 'DEBUG') |
| 59 | + // level: 3 |
| 60 | + info (...args) { |
| 61 | + this.status('cyan', 'info', ...args) |
| 62 | + } |
| 63 | + |
| 64 | + wait (...args) { |
| 65 | + this.status('cyan', 'wait', ...args) |
| 66 | + } |
49 | 67 |
|
50 |
| -logger.debug = function (msg) { |
51 |
| - if (env.isDebug) { |
52 |
| - debugFn(msg) |
| 68 | + // level: 3 |
| 69 | + status (color, label, ...args) { |
| 70 | + if (this.options.logLevel < 3) { |
| 71 | + return |
| 72 | + } |
| 73 | + console.log(chalk[color](label), ...args) |
53 | 74 | }
|
54 | 75 | }
|
55 | 76 |
|
56 |
| -module.exports = logger |
| 77 | +/** |
| 78 | + * Expose a logger instance. |
| 79 | + */ |
| 80 | + |
| 81 | +module.exports = new Logger() |
| 82 | + |
0 commit comments