-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHydraExpressLogger.js
73 lines (70 loc) · 1.64 KB
/
HydraExpressLogger.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
'use strict';
const HydraExpressPlugin = require('hydra-express-plugin');
const PinoExpressLogger = require('./PinoExpressLogger');
/**
* @name HydraExpressLogger
* @summary HydraExpressPlugin for logging (adds Express 'log' middleware)
* @extends HydraExpressPlugin
*/
class HydraExpressLogger extends HydraExpressPlugin {
constructor() {
super('logger');
}
/**
* @override
*/
setConfig(serviceConfig) {
super.setConfig(serviceConfig);
this.initLogger();
this.hydraExpress.appLogger = this._logger.logger;
}
/**
* @override
*/
onServiceReady() {
this.hydra.on('log', entry => this._logger.logger[entry.type](entry));
this.hydraExpress.getExpressApp().use(this.getMiddleware());
}
/**
* @override
*/
configChanged(opts) {
this.opts = opts;
this._logger.shutdown(() => this.initLogger());
}
/**
* @name initLogger
* @summary Initialize the logger
*/
initLogger() {
this._logger = new PinoExpressLogger(
Object.assign(
{
augment: {
serviceName: this.hydraConfig.serviceName,
serviceVersion: this.hydraConfig.serviceVersion
}
},
this.opts
));
}
/**
* @name getLogger
* @return {object} The pino logger
*/
getLogger() {
return this._logger.logger;
}
/**
* @name getMiddleware
* @return {object} Logger middleware for express
*/
getMiddleware() {
if (this.middlewareWrapper) {
return this.middlewareWrapper;
}
this.middlewareWrapper = (...args) => this._logger.middleware(...args);
return this.middlewareWrapper;
}
}
module.exports = HydraExpressLogger;