-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (40 loc) · 1.41 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
const { Transform } = require('node:stream');
const build = require('pino-abstract-transport');
const { DEFAULT_LEVELS } = require('pino/lib/constants');
const nums = Object.keys(DEFAULT_LEVELS).reduce((o, k) => {
o[DEFAULT_LEVELS[k]] = k;
return o;
}, {});
const getMessage = (obj) => {
const systemKeys = ['level', 'time', 'pid', 'hostname'];
const { reqId, msg, err } = obj;
if (err) return err;
if (msg && !reqId) return msg;
const keys = Object.keys(obj).filter((key) => !systemKeys.includes(key));
if (!keys.length) return '';
const userObj = keys.reduce((o, key) => {
return (o[key] = obj[key]), o;
}, {});
return JSON.stringify(userObj);
};
/**
* Create a Pino transport that writes to stdout.
*
* The transport expects the source to be a stream of objects with a
* level property. The level property is expected to have a value that
* is in DEFAULT_LEVELS from pino/lib/constants. The transport will
* convert this to an uppercase string and write it to stdout. *
* @return {Transform} A function that takes a source stream and writes
* to stdout.
*/
module.exports = function () {
return build(async function (source) {
for await (const obj of source) {
const result = {};
const level = nums[obj.level] || 'UNSPECIFIED';
result.level = level.toUpperCase();
result.message = getMessage(obj);
process.stdout.write(JSON.stringify(result) + '\n');
}
});
};