-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog-middleware.js
121 lines (108 loc) · 3.31 KB
/
log-middleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import StashKu, { BaseEngine } from '@appku/stashku';
import ConsoleLogger from './console-logger.js';
const SUPPORTED_SEVERITIES = ['debug', 'info', 'warn', 'error'];
/**
* @typedef LogMiddlewareConfiguration
* @property {ConsoleLoggerConfiguration} console
* @property {FileLoggerConfiguration} file
*/
/**
* Middlware that provides logging capabilities.
* @class LogMiddleware
*/
const LogMiddleware = {
/**
* @type {String}
*/
name: '@appku/stashku-log',
/**
* @type {Array}
* @readonly
*/
states: ['log'],
/**
* @type {LogMiddlewareConfiguration}
*/
config: null,
/**
* @type {Array}
*/
loggers: null,
/**
* Returns the loggers of the middleware, constructing the object if necessary (lazy-load).
*
* We lazy load the loggers in case other initialization code is present to set environmental variables.
* @returns {Array}
* @private
*/
getLoggers() {
if (!this.loggers) {
this.loggers = {
console: new ConsoleLogger()
};
}
return this.loggers;
},
/**
* The middleware callback handler that is called from StashKu.
* @param {StashKu} stashKu - The `StashKu` instance making the call.
* @param {BaseEngine} engine - The currently active storage engine.
* @param {String} severity - The severity of the log call, either "debug", "info", "warn", or "error".
* @param {Array} args - The arguments containing information to log.
*/
async callback(stashKu, engine, severity, args) {
//ensure configuration is set from StashKu, and each logger is configured.
if (stashKu.config['@appku/stashku-log']) {
this.config = stashKu.config['@appku/stashku-log'];
let loggers = this.getLoggers();
for (let key in loggers) {
loggers[key].config = Object.assign(loggers[key].config, this.config[key]);
}
}
//make the appropriate call.
if (SUPPORTED_SEVERITIES.indexOf(severity) >= 0) {
this[severity](...args);
}
},
/**
* Writes a "debug" severity log message.
* @param {...any} args - The arguments with information to be logged.
*/
async debug(...args) {
let loggers = this.getLoggers();
for (let key in loggers) {
loggers[key].debug(...args);
}
},
/**
* Writes a "info" severity log message.
* @param {...any} args - The arguments with information to be logged.
*/
async info(...args) {
let loggers = this.getLoggers();
for (let key in loggers) {
loggers[key].info(...args);
}
},
/**
* Writes a "warn" severity log message.
* @param {...any} args - The arguments with information to be logged.
*/
async warn(...args) {
let loggers = this.getLoggers();
for (let key in loggers) {
loggers[key].warn(...args);
}
},
/**
* Writes a "error" severity log message.
* @param {...any} args - The arguments with information to be logged.
*/
async error(...args) {
let loggers = this.getLoggers();
for (let key in loggers) {
loggers[key].error(...args);
}
}
};
export default LogMiddleware;