4 * Log as JSON to stdout/stderr.
7 const common
= require('./common');
9 // This is uncomfortable, but is the simplest way to let logging work for BigInts.
10 // TODO: revisit with better solution
11 BigInt
.prototype.toJSON = function() {
12 return this.toString();
16 Object
.defineProperty(Error
.prototype, 'toJSON', {
20 const dupKey = function (key
) {
21 // eslint-disable-next-line security/detect-object-injection
22 result
[key
] = this[key
];
24 Object
.getOwnPropertyNames(this).forEach(dupKey
, this);
29 const defaultOptions
= {
30 ignoreBelowLevel: 'debug',
35 constructor(options
= {}) {
36 common
.setOptions(this, defaultOptions
, options
);
46 const ignoreLevelIdx
= this.logLevels
.indexOf(this.ignoreBelowLevel
);
47 this.logLevels
.forEach((level
) => {
48 // eslint-disable-next-line security/detect-object-injection
49 this[level
] = (this.logLevels
.indexOf(level
) > ignoreLevelIdx
) ?
51 Logger
.levelTemplateFn(this.backend
, level
);
55 static levelTemplateFn(backend
, level
) {
56 return function (...args
) {
57 // eslint-disable-next-line security/detect-object-injection
58 backend
[level
](Logger
.payload(level
, ...args
));
62 static payload(level
, scope
, message
, data
, ...other
) {
63 return JSON
.stringify({
64 timestamp: Date
.now(),
66 scope: scope
|| '[unknown]',
67 message: message
|| '',
69 ...(other
.length
&& { other
}),
74 module
.exports
= Logger
;