-
Notifications
You must be signed in to change notification settings - Fork 225
/
_mock_logger.js
116 lines (105 loc) · 2.79 KB
/
_mock_logger.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
/*
* Copyright Elasticsearch B.V. and other contributors where applicable.
* Licensed under the BSD 2-Clause License; you may not use this file except in
* compliance with the BSD 2-Clause License.
*/
'use strict';
const { levels, pino } = require('pino');
/**
* @typedef {Object} LogEntry
* @property {String} type
* @property {String} mergingObject
* @property {String} message
* @property {Array<any>} interpolation
*/
/**
* SomeClass is an example class for my question.
* @class
* @constructor
* @public
*/
class MockLogger {
constructor() {
/**
* someProperty is an example property that is set to `true`
* @type {Array<LogEntry>}
* @public
*/
this.calls = [];
}
/**
* @private
* @param {String} type
* @param {Array<any>} loggerArgs
*/
_log(type, loggerArgs) {
const args = [].slice.call(loggerArgs);
const hasMergingObject = typeof args[0] === 'object';
const mergingObject = hasMergingObject ? args[0] : null;
const message = hasMergingObject ? args[1] : args[0];
const interpolation = args.slice(hasMergingObject ? 2 : 1);
this.calls.push({
type,
mergingObject,
message,
interpolation,
});
}
fatal() {
this._log('fatal', arguments);
}
error() {
this._log('error', arguments);
}
warn() {
this._log('warn', arguments);
}
info() {
this._log('info', arguments);
}
debug() {
this._log('debug', arguments);
}
trace() {
this._log('trace', arguments);
}
}
/**
* Returns a logger which stores the log data into the given array
*
* @param {Array<Object>} calls Array where to put the logs to inspect later
*/
function createMockLogger(calls) {
if (!Array.isArray(calls)) {
throw new Error('Calls parameter must be an array to create a mock logger');
}
return pino(
{ name: 'mock-logger' },
{
// The message received is a serialized object with is a merge of
// - the log level (`level` property) with its numeric value (eg. INFO === 30)
// - the log message (`msg` property) with the interpolated values
// - the logger name (`name` property) with the value `mock-logger`
// - all props of mergingObject if it was passed. See https://getpino.io/#/docs/api?id=logging-method-parameters
write: function (logMsg) {
const logObj = JSON.parse(logMsg);
const logMessage = logObj.msg;
const levelValue = logObj.level;
const levelName = Object.keys(levels.values).find(
(key) => levels.values[key] === levelValue,
);
delete logObj.msg;
delete logObj.level;
calls.push({
type: levelName,
mergingObject: logObj,
message: logMessage,
});
},
},
);
}
module.exports = {
createMockLogger,
MockLogger,
};