-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
69 lines (62 loc) · 2.44 KB
/
index.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
const pino = require("pino");
const { serializeToString, serializeError, serializeLogLevel } = require("./utils/serializers");
/**
* Replace the standard serializer to be able to transform each log
* Pino Documentation: https://getpino.io/#/docs/api?id=serializerssymbolfor39pino39-function
*/
const serializers = {};
serializers[Symbol.for('pino.*')] = (obj) =>{
Object.keys(obj).forEach(key => {
serializeToString(key, obj);
});
return obj;
}
/**
* Factory to create the logger
*
* @param {Object} props Logger initial configuration
* @param {string} props.type Name of application is using the Logger
* @param {string} props.context Which Logger user want to create: "app" or "access"
* @param {Object} options Logger options
* @param {string} options.minLoglevel minimum level of log enabled: "trace", "debug", "info", "warn", "error", and "fatal"
* @param {string} options.destination path of logs destination. If not passed STDOUT is the default one
*/
const createLogger = (props, { minLoglevel, destination } = {}) => {
const options = {
messageKey: "message",
base: null,
timestamp: true,
level: minLoglevel ? minLoglevel.toLowerCase() : "info"
}
/**
* Add the serializeToString for application logger
* We don't do this inside the Proxy because we want
* to be able to stringify all the fields, included
* the ones passed in the initialization that are not
* available in the pino.write
*/
const context = props.context.valueOf(); // We clone the value of the props and not use it directly to avoid that this variable has impacted by the serializations
if (context === "app") {
options.serializers = serializers;
}
const destOut = destination ? pino.destination(destination) : null;
const customPino = new Proxy(pino(options, destOut), {
get: function (target, prop) {
// Proxy the logger and intercept the 'write' fnc to be able to customize the logs
if(prop.toString() === "Symbol(pino.write)"){
return function () {
serializeError(arguments);
if (context === "app") {
serializeLogLevel(arguments);
}
target[prop].apply(this, arguments);
}
}
return target[prop];
}
});
return customPino.child(props);
};
module.exports = {
createLogger
};