UNPKG

1.25 kBJavaScriptView Raw
1/**
2 * @copyright Copyright (c) 2019 Maxim Khorin <maksimovichu@gmail.com>
3 */
4'use strict';
5
6const Base = require('../base/Base');
7
8module.exports = class LogType extends Base {
9
10 constructor (config) {
11 super({
12 stores: [],
13 consoleOutput: true,
14 consoleMethod: 'log',
15 dataStringifyOptions: {depth: 10},
16 ...config
17 });
18 this._counter = 0;
19 }
20
21 getCounter () {
22 return this._counter;
23 }
24
25 log (message, data) {
26 data = this.formatData(data);
27 if (this.consoleOutput) {
28 console[this.consoleMethod](this.name +':', message, data);
29 }
30 for (const store of this.stores) {
31 this.logger.stores[store].save(this.name, message, data);
32 }
33 this._counter += 1;
34 this.logger.afterLog(this.name, message, data);
35 }
36
37 formatData (data) {
38 if (typeof data === 'function') {
39 return `[Function: ${data.name}]`;
40 }
41 if (data instanceof Error) {
42 return data.stack;
43 }
44 return data === undefined ? '' : util.inspect(data, this.dataStringifyOptions);
45 }
46};
47
48const util = require('util');
\No newline at end of file