|
1 | 1 | /* eslint-disable no-param-reassign */ |
2 | 2 | /* eslint no-console: off */ |
| 3 | +import _ from 'lodash' |
| 4 | + |
3 | 5 | import chalkExt from './chalkExt' |
4 | 6 |
|
| 7 | +// TODO: use static props -> update babel! |
5 | 8 | class Logger { |
6 | | - constructor() { |
7 | | - this.options = { |
8 | | - prefix: '{yellow [StackR23]}', |
9 | | - debug: { |
10 | | - color: 'cyan' |
11 | | - }, |
12 | | - error: { |
13 | | - color: 'red' |
14 | | - }, |
15 | | - success: { |
16 | | - color: 'green', |
17 | | - }, |
18 | | - } |
19 | | - } |
20 | | - |
21 | 9 |
|
22 | | - setPrefix(prefix) { |
23 | | - this.options.prefix = prefix || '' |
| 10 | + defaults = { |
| 11 | + prefix: '{bold.yellow [StackR23] }', |
| 12 | + log: {style: 'reset', prefix: 'bold.yellow LOG - '}, |
| 13 | + debug: {style: 'cyan', prefix: '{bold.cyan DEBUG: }'}, |
| 14 | + error: {style: 'red', prefix: '{bold.red ERROR: }'}, |
| 15 | + success: {style: 'green', prefix: '{bold.green SUCCESS: }'} |
24 | 16 | } |
25 | 17 |
|
26 | | - log(str, typePrefix, styleType, styleString) { |
27 | | - if (arguments.length === 1) { |
28 | | - console.log(chalkExt`{bold ${this.options.prefix}} ${str}`) |
29 | | - |
30 | | - return true |
31 | | - } |
| 18 | + constructor(options) { |
| 19 | + this.options = _.merge(this.defaults, options) |
| 20 | + console.log('this.defaults :>> ', this.defaults) |
| 21 | + console.log('this.options :>> ', this.options) |
| 22 | + } |
32 | 23 |
|
33 | | - if (arguments.length === 2) { |
34 | | - const type = typePrefix |
35 | | - const {color, prefix, colorType} = this.options[type] |
| 24 | + log(str, type = 'log', styleCustom) { |
| 25 | + const {prefix, [type]: typeOptions} = this.options |
36 | 26 |
|
37 | | - typePrefix = prefix || type |
38 | | - styleType = colorType || color |
39 | | - styleString = color |
| 27 | + if (arguments.length === 1) { |
| 28 | + console.log(chalkExt`${prefix}${str}`) |
| 29 | + return |
40 | 30 | } |
41 | 31 |
|
42 | 32 | console.log( |
43 | | - chalkExt`{${styleType} {bold ${this.options.prefix} ${typePrefix}:} {${styleString} ${str}}}` |
| 33 | + (prefix ? chalkExt`${prefix}` : '') + |
| 34 | + (typeOptions.prefix ? chalkExt`${typeOptions.prefix}` : '') + |
| 35 | + chalkExt`{${typeOptions.style} ${str}}` |
44 | 36 | ) |
45 | | - |
46 | | - return true |
47 | 37 | } |
48 | 38 |
|
49 | | - dir = arg => console.dir(...arg) |
50 | | - |
51 | 39 | debug = str => this.log(str, 'debug') |
52 | | - |
53 | | - error = str => this.log(str, 'ERROR', `${this.options.error.color}Bright.bgBlack`, this.options.error.color) |
54 | | - |
55 | | - success = str => this.log(str, 'SUCCESS', `${this.options.success.color}Bright`, this.options.success.color) |
| 40 | + error = str => this.log(str, 'error') |
| 41 | + success = str => this.log(str, 'success') |
56 | 42 | } |
57 | 43 |
|
| 44 | +export {Logger} |
58 | 45 | export default new Logger() |
0 commit comments