1 | const logLevel = require('./lib/log-level');
|
2 | const logFormatter = require('./lib/log-formater');
|
3 | const metricFormatter = require('./lib/metric-formatter');
|
4 | const sender = require('./lib/sender');
|
5 | const util = require('util');
|
6 |
|
7 | class Logger {
|
8 | constructor(config) {
|
9 | this._config = Object.assign({
|
10 | debug: false,
|
11 | logLevel: logLevel.ALL,
|
12 | }, config);
|
13 |
|
14 | this._logs = []
|
15 | this._metrics = [];
|
16 |
|
17 | process.on('beforeExit', () => {
|
18 | if (0 < this._metrics.length) {
|
19 | this._flushMetrics();
|
20 | }
|
21 | });
|
22 | }
|
23 |
|
24 | async debug(message, metadata = {}, type = '', module = '') {
|
25 | await this.log(logLevel.DEBUG, message, metadata, type, module);
|
26 | }
|
27 |
|
28 | async info(message, metadata = {}, type = '', module = '') {
|
29 | await this.log(logLevel.INFO, message, metadata, type, module);
|
30 | }
|
31 |
|
32 | async warn(message, metadata = {}, type = '', module = '') {
|
33 | await this.log(logLevel.WARN, message, metadata, type, module);
|
34 | }
|
35 |
|
36 | async error(message, metadata = {}, type = '', module = '') {
|
37 | await this.log(logLevel.ERROR, message, metadata, type, module);
|
38 | }
|
39 |
|
40 | async log(level, message, metadata, type, module) {
|
41 | if (logLevel.checkLevelEnabled(this._config.logLevel, level) === false) {
|
42 | return false;
|
43 | }
|
44 |
|
45 | this._logs.push(logFormatter.formatLog(level, message, metadata, type, module));
|
46 |
|
47 | await this._flushLogs();
|
48 | }
|
49 |
|
50 | async _flushLogs() {
|
51 | const logs = logFormatter.formatLogs(this._config.logApp, this._logs);
|
52 |
|
53 | try {
|
54 | throw new Error('test');
|
55 | const res = await sender.sendToServer(this._config.logServer, logs);
|
56 | this._outputDebug('[hlg-logger-v2] flush logs success: %s', res);
|
57 | } catch (err) {
|
58 | this._outputDebug('[hlg-logger-v2] flush logs error: %s', err);
|
59 | }
|
60 |
|
61 | this._logs = [];
|
62 | }
|
63 |
|
64 | async addMetric(name, target, labels = {}) {
|
65 | this._metrics.push(metricFormatter.formatMetric(name, target, labels));
|
66 |
|
67 | if (5 <= this._metrics.length) {
|
68 | await this._flushMetrics();
|
69 | }
|
70 | }
|
71 |
|
72 | async _flushMetrics() {
|
73 | const metrics = metricFormatter.formatMetrics(this._config.metricApp, this._metrics);
|
74 |
|
75 | try {
|
76 | const res = await sender.sendToServer(this._config.metricServer, metrics);
|
77 | this._outputDebug('[hlg-logger-v2] flush logs success: %s', res);
|
78 | } catch (err) {
|
79 | this._outputDebug('[hlg-logger-v2] flush metrics error: %s', err);
|
80 | }
|
81 |
|
82 | this._metrics = [];
|
83 | }
|
84 |
|
85 | _outputDebug(message, ...args) {
|
86 | if (this._config.debug) {
|
87 | args = args.map((arg) => {
|
88 | if (arg instanceof Error) {
|
89 | return JSON.stringify({
|
90 | code: arg.code || '',
|
91 | status: arg.status || 0,
|
92 | message: arg.message || '',
|
93 | stack: arg.stack || '',
|
94 | });
|
95 | } else if (arg instanceof Object) {
|
96 | return JSON.stringify(arg);
|
97 | }
|
98 | return arg;
|
99 | });
|
100 |
|
101 | console.debug(util.format(message, args));
|
102 | }
|
103 | }
|
104 | }
|
105 |
|
106 | module.exports = Logger; |
\ | No newline at end of file |