1 |
|
2 |
|
3 |
|
4 | 'use strict';
|
5 |
|
6 | const Base = require('../base/Component');
|
7 |
|
8 | module.exports = class Logger extends Base {
|
9 |
|
10 | static getConstants () {
|
11 | return {
|
12 | EVENT_AFTER_LOG: 'afterLog'
|
13 | };
|
14 | }
|
15 |
|
16 | constructor (config) {
|
17 | super({
|
18 | depends: '#start',
|
19 | level: 'trace',
|
20 | stores: {
|
21 | common: require('./FileLogStore'),
|
22 | error: require('./FileLogStore')
|
23 | },
|
24 | typeNames: ['trace', 'debug', 'info', 'warn', 'error', 'fatal'],
|
25 | types: {
|
26 | error: {stores: ['common', 'error']},
|
27 | fatal: {stores: ['common', 'error']}
|
28 | },
|
29 | LogType: {
|
30 | Class: require('./LogType'),
|
31 | stores: ['common']
|
32 | },
|
33 | consoleOutput: true,
|
34 | errorConsoleLevel: 'warn',
|
35 | ...config
|
36 | });
|
37 | this._levelIndex = this.typeNames.indexOf(this.level);
|
38 | this._errorConsoleIndex = this.typeNames.indexOf(this.errorConsoleLevel);
|
39 |
|
40 | this.createStores();
|
41 | this.createTypes();
|
42 | }
|
43 |
|
44 | async init () {
|
45 | for (const store of Object.values(this.stores)) {
|
46 | await store.init();
|
47 | }
|
48 | }
|
49 |
|
50 | isActiveType (name) {
|
51 | return this.getType(name);
|
52 | }
|
53 |
|
54 | isTrace () {
|
55 | return this.types.trace;
|
56 | }
|
57 |
|
58 | isDebug () {
|
59 | return this.types.debug;
|
60 | }
|
61 |
|
62 | getType (name) {
|
63 | return this.types.hasOwnProperty(name) ? this.types[name] : null;
|
64 | }
|
65 |
|
66 | getStore (name) {
|
67 | return this.stores[name] instanceof LogStore ? this.stores[name] : null;
|
68 | }
|
69 |
|
70 | createStores () {
|
71 | for (const name of Object.keys(this.stores)) {
|
72 | this.stores[name] = this.spawn(this.stores[name], {logger: this, name});
|
73 | }
|
74 | }
|
75 |
|
76 | createTypes () {
|
77 | for (let i = 0; i < this.typeNames.length; ++i) {
|
78 | const name = this.typeNames[i];
|
79 | this.types[name] = this._levelIndex > i ? null : this.createType(name, i);
|
80 | }
|
81 | }
|
82 |
|
83 | createType (name, index) {
|
84 | return this.spawn({
|
85 | ...this.LogType,
|
86 | consoleOutput: this.consoleOutput,
|
87 | consoleMethod: index < this._errorConsoleIndex ? 'log' : 'error',
|
88 | ...this.types[name],
|
89 | logger: this,
|
90 | name
|
91 | });
|
92 | }
|
93 |
|
94 | log (type, message, data) {
|
95 | if (!this.types.hasOwnProperty(type)) {
|
96 | this.logInvalidType(type, message, data);
|
97 | } else if (this.types[type]) {
|
98 | this.types[type].log(message, data);
|
99 | }
|
100 | }
|
101 |
|
102 | logInvalidType (type, message, data) {
|
103 | type = this.wrapClassMessage(`Unknown type: ${type}`);
|
104 | this.types.error
|
105 | ? this.types.error.log(type, {message, data})
|
106 | : console.error(type, message, data);
|
107 | }
|
108 |
|
109 | getCounters (names) {
|
110 | const counters = [];
|
111 | names = Array.isArray(names) ? names : this.typeNames;
|
112 | for (const name of names) {
|
113 | const type = this.getType(name);
|
114 | if (type) {
|
115 | const counter = type.getCounter();
|
116 | if (counter) {
|
117 | counters.push({type: name, counter});
|
118 | }
|
119 | }
|
120 | }
|
121 | return counters;
|
122 | }
|
123 |
|
124 | afterLog (type, message, data) {
|
125 | this.trigger(this.EVENT_AFTER_LOG, {type, message, data});
|
126 | }
|
127 | };
|
128 | module.exports.init();
|
129 |
|
130 | const LogStore = require('./LogStore'); |
\ | No newline at end of file |