1 | const _bindAll = require('lodash.bindall');
|
2 | const _forEach = require('lodash.foreach');
|
3 | const chalk = require('chalk');
|
4 |
|
5 | class Level {
|
6 | constructor(level, name, ascii, exit = false) {
|
7 | _bindAll(this, 'log');
|
8 | this.level = level;
|
9 | this.name = name;
|
10 | this.ascii = ascii;
|
11 | this.exit = exit;
|
12 | }
|
13 |
|
14 | static create(...args) {
|
15 | return new Level(...args);
|
16 | }
|
17 |
|
18 | log(...args) {
|
19 | return console.log.apply(console, [this.ascii].concat(args));
|
20 | }
|
21 | }
|
22 |
|
23 | const LEVELS = {
|
24 | fatal: Level.create(0, 'FATAL', chalk.black.bgRed('FATAL'), true),
|
25 | error: Level.create(1, 'Error', chalk.red('ERROR')),
|
26 | warn: Level.create(2, 'WARNING', chalk.yellow('WARNING')),
|
27 | info: Level.create(3, 'INFO', chalk.green('INFO')),
|
28 | debug: Level.create(4, 'DEBUG', chalk.reset('DEBUG')),
|
29 | trace: Level.create(5, 'TRACE', chalk.white('TRACE'))
|
30 | };
|
31 |
|
32 | class Logger {
|
33 | constructor() {
|
34 | this.addLoggingMethods();
|
35 | }
|
36 |
|
37 | static create() {
|
38 | return new Logger();
|
39 | }
|
40 |
|
41 | addLoggingMethods() {
|
42 | _forEach(LEVELS, (value, name) => {
|
43 | if (Object.prototype.hasOwnProperty.call(LEVELS, name)) {
|
44 | this[name] = LEVELS[name].log;
|
45 | }
|
46 | });
|
47 | }
|
48 | }
|
49 |
|
50 | module.exports = Logger;
|