@cichol/logger 4.0.0-alpha.5 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +10 -10
- package/releases/cjs.js +120 -148
- package/releases/esm.js +79 -80
- package/types/classes/logger.d.ts +0 -33
- package/types/formatters/command-line.d.ts +0 -2
- package/types/formatters/morgan.d.ts +0 -8
- package/types/handlers/debug.d.ts +0 -5
- package/types/handlers/error.d.ts +0 -13
- package/types/handlers/fatal.d.ts +0 -4
- package/types/handlers/http.d.ts +0 -5
- package/types/handlers/index.d.ts +0 -12
- package/types/handlers/info.d.ts +0 -5
- package/types/handlers/notice.d.ts +0 -5
- package/types/handlers/query.d.ts +0 -13
- package/types/handlers/verbose.d.ts +0 -5
- package/types/handlers/warning.d.ts +0 -5
- package/types/helpers/color-scheme.d.ts +0 -12
- package/types/helpers/constants.d.ts +0 -18
- package/types/helpers/label-decorator.d.ts +0 -2
- package/types/helpers/logging-level.d.ts +0 -13
- package/types/index.d.ts +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cichol/logger",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"description": "A logger based on `winston` for personal projects.",
|
|
5
5
|
"homepage": "https://github.com/morrighan/packages",
|
|
6
6
|
"bugs": {
|
|
@@ -33,20 +33,20 @@
|
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"bluebird": "^3.7.2",
|
|
36
|
-
"chalk": "^
|
|
36
|
+
"chalk": "^5.3.0",
|
|
37
37
|
"cli-highlight": "^2.1.11",
|
|
38
|
-
"cls-rtracer": "^2.6.
|
|
38
|
+
"cls-rtracer": "^2.6.3",
|
|
39
39
|
"lodash": "^4.17.21",
|
|
40
|
-
"luxon": "^
|
|
40
|
+
"luxon": "^3.5.0",
|
|
41
41
|
"morgan": "^1.10.0",
|
|
42
|
-
"pretty-bytes": "^
|
|
43
|
-
"pretty-ms": "^
|
|
44
|
-
"reflect-metadata": "^0.
|
|
45
|
-
"winston": "^3.
|
|
42
|
+
"pretty-bytes": "^6.1.1",
|
|
43
|
+
"pretty-ms": "^9.1.0",
|
|
44
|
+
"reflect-metadata": "^0.2.2",
|
|
45
|
+
"winston": "^3.14.2"
|
|
46
46
|
},
|
|
47
47
|
"engines": {
|
|
48
|
-
"node": "^
|
|
49
|
-
"npm": "^8.
|
|
48
|
+
"node": "^20.13.1",
|
|
49
|
+
"npm": "^10.8.0"
|
|
50
50
|
},
|
|
51
51
|
"publishConfig": {
|
|
52
52
|
"registry": "https://registry.npmjs.org/",
|
package/releases/cjs.js
CHANGED
|
@@ -31,10 +31,7 @@ var prettyMs = require('pretty-ms');
|
|
|
31
31
|
var cluster = require('cluster');
|
|
32
32
|
require('worker_threads');
|
|
33
33
|
|
|
34
|
-
function
|
|
35
|
-
|
|
36
|
-
function _interopNamespace(e) {
|
|
37
|
-
if (e && e.__esModule) return e;
|
|
34
|
+
function _interopNamespaceDefault(e) {
|
|
38
35
|
var n = Object.create(null);
|
|
39
36
|
if (e) {
|
|
40
37
|
Object.keys(e).forEach(function (k) {
|
|
@@ -47,35 +44,11 @@ function _interopNamespace(e) {
|
|
|
47
44
|
}
|
|
48
45
|
});
|
|
49
46
|
}
|
|
50
|
-
n
|
|
47
|
+
n.default = e;
|
|
51
48
|
return Object.freeze(n);
|
|
52
49
|
}
|
|
53
50
|
|
|
54
|
-
var
|
|
55
|
-
var _filterInstanceProperty__default = /*#__PURE__*/_interopDefaultLegacy(_filterInstanceProperty);
|
|
56
|
-
var _mapInstanceProperty__default = /*#__PURE__*/_interopDefaultLegacy(_mapInstanceProperty);
|
|
57
|
-
var _trimInstanceProperty__default = /*#__PURE__*/_interopDefaultLegacy(_trimInstanceProperty);
|
|
58
|
-
var _Reflect$hasOwnMetadata__default = /*#__PURE__*/_interopDefaultLegacy(_Reflect$hasOwnMetadata);
|
|
59
|
-
var _Reflect$getOwnMetadata__default = /*#__PURE__*/_interopDefaultLegacy(_Reflect$getOwnMetadata);
|
|
60
|
-
var _Reflect$defineMetadata__default = /*#__PURE__*/_interopDefaultLegacy(_Reflect$defineMetadata);
|
|
61
|
-
var Promise__default = /*#__PURE__*/_interopDefaultLegacy(Promise$1);
|
|
62
|
-
var lodash__default = /*#__PURE__*/_interopDefaultLegacy(lodash);
|
|
63
|
-
var morgan__default = /*#__PURE__*/_interopDefaultLegacy(morgan);
|
|
64
|
-
var winston__namespace = /*#__PURE__*/_interopNamespace(winston);
|
|
65
|
-
var _findInstanceProperty__default = /*#__PURE__*/_interopDefaultLegacy(_findInstanceProperty);
|
|
66
|
-
var _Reflect$apply__default = /*#__PURE__*/_interopDefaultLegacy(_Reflect$apply);
|
|
67
|
-
var chalk__default = /*#__PURE__*/_interopDefaultLegacy(chalk);
|
|
68
|
-
var rTracer__default = /*#__PURE__*/_interopDefaultLegacy(rTracer);
|
|
69
|
-
var _Map__default = /*#__PURE__*/_interopDefaultLegacy(_Map);
|
|
70
|
-
var process__default = /*#__PURE__*/_interopDefaultLegacy(process);
|
|
71
|
-
var _Symbol$for__default = /*#__PURE__*/_interopDefaultLegacy(_Symbol$for);
|
|
72
|
-
var _startsWithInstanceProperty__default = /*#__PURE__*/_interopDefaultLegacy(_startsWithInstanceProperty);
|
|
73
|
-
var _sliceInstanceProperty__default = /*#__PURE__*/_interopDefaultLegacy(_sliceInstanceProperty);
|
|
74
|
-
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
|
75
|
-
var highlight__default = /*#__PURE__*/_interopDefaultLegacy(highlight);
|
|
76
|
-
var prettyBytes__default = /*#__PURE__*/_interopDefaultLegacy(prettyBytes);
|
|
77
|
-
var prettyMs__default = /*#__PURE__*/_interopDefaultLegacy(prettyMs);
|
|
78
|
-
var cluster__default = /*#__PURE__*/_interopDefaultLegacy(cluster);
|
|
51
|
+
var winston__namespace = /*#__PURE__*/_interopNamespaceDefault(winston);
|
|
79
52
|
|
|
80
53
|
const LoggingLevel = {
|
|
81
54
|
// Critical levels. (It will crash the process)
|
|
@@ -94,16 +67,18 @@ const LoggingLevel = {
|
|
|
94
67
|
Debug: 5
|
|
95
68
|
};
|
|
96
69
|
|
|
97
|
-
|
|
70
|
+
// Constants.
|
|
71
|
+
const colorizerMap = new _Map();
|
|
98
72
|
|
|
73
|
+
// Helpers.
|
|
99
74
|
function createScheme(options) {
|
|
100
75
|
const nextIndex = colorizerMap.size;
|
|
101
|
-
const colorizer =
|
|
76
|
+
const colorizer = chalk.bgHex(options.background).hex(options.foreground);
|
|
102
77
|
colorizerMap.set(nextIndex, colorizer);
|
|
103
78
|
return nextIndex;
|
|
104
|
-
}
|
|
105
|
-
|
|
79
|
+
}
|
|
106
80
|
|
|
81
|
+
// Color definitions.
|
|
107
82
|
const ColorValue = {
|
|
108
83
|
// Basic colors.
|
|
109
84
|
White: '#FFFFFF',
|
|
@@ -143,87 +118,77 @@ const ColorScheme = {
|
|
|
143
118
|
})
|
|
144
119
|
};
|
|
145
120
|
function getChalkOf(colorSet) {
|
|
146
|
-
return colorizerMap.get(colorSet) ??
|
|
121
|
+
return colorizerMap.get(colorSet) ?? chalk;
|
|
147
122
|
}
|
|
123
|
+
|
|
148
124
|
/* eslint-enable no-use-before-define */
|
|
149
125
|
|
|
150
126
|
// Node.js built-in APIs.
|
|
151
127
|
|
|
128
|
+
// Constants.
|
|
152
129
|
const {
|
|
153
130
|
isTTY
|
|
154
|
-
} =
|
|
131
|
+
} = process.stdout;
|
|
155
132
|
const isNotOnCLI = !isTTY;
|
|
156
133
|
function decorateLabel(label, scheme) {
|
|
157
134
|
if (isNotOnCLI) {
|
|
158
135
|
return label;
|
|
159
136
|
}
|
|
160
|
-
|
|
161
137
|
const chalk = getChalkOf(scheme);
|
|
162
138
|
const decoratedLabel = chalk.bold(` ${label} `);
|
|
163
139
|
return decoratedLabel;
|
|
164
140
|
}
|
|
165
141
|
|
|
166
|
-
//
|
|
167
|
-
const rootPath = process__default["default"].cwd();
|
|
168
|
-
const conjunction = ` ${chalk__default["default"].blue('──')} `;
|
|
169
|
-
|
|
170
|
-
const splat = _Symbol$for__default["default"]('splat'); // Handler metadata.
|
|
142
|
+
// Type definitions.
|
|
171
143
|
|
|
144
|
+
// Constants.
|
|
145
|
+
const rootPath = process.cwd();
|
|
146
|
+
const conjunction = ` ${chalk.blue('──')} `;
|
|
147
|
+
const splat = _Symbol$for('splat');
|
|
172
148
|
|
|
149
|
+
// Handler metadata.
|
|
173
150
|
const name$8 = 'error';
|
|
174
151
|
const level$8 = LoggingLevel.Error;
|
|
175
152
|
const label$8 = decorateLabel('ERROR', ColorScheme.Critical);
|
|
176
|
-
|
|
177
153
|
function mapLocationOfErrorThrown(filename) {
|
|
178
|
-
let target =
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
target = chalk__default["default"].gray(target).replace(/node_modules[\\/]([^\\/]+)(.*)/, (matches, moduleName, trailing) => `node_modules${path__default["default"].sep}${chalk__default["default"].bold.underline(moduleName)}${chalk__default["default"].gray(trailing)}` // eslint-disable-line max-len
|
|
154
|
+
let target = path.relative(rootPath, filename);
|
|
155
|
+
if (_startsWithInstanceProperty(target).call(target, 'node_modules')) {
|
|
156
|
+
target = chalk.gray(target).replace(/node_modules[\\/]([^\\/]+)(.*)/, (matches, moduleName, trailing) => `node_modules${path.sep}${chalk.bold.underline(moduleName)}${chalk.gray(trailing)}` // eslint-disable-line max-len
|
|
182
157
|
);
|
|
183
158
|
} else {
|
|
184
|
-
target = `${
|
|
159
|
+
target = `${chalk.gray(path.dirname(target) + path.sep)}${chalk.bold.underline(path.basename(target))}`;
|
|
185
160
|
}
|
|
186
|
-
|
|
187
161
|
return target;
|
|
188
162
|
}
|
|
189
|
-
|
|
190
163
|
function handle$1(data) {
|
|
191
164
|
var _context, _context2, _context3;
|
|
192
|
-
|
|
193
165
|
const {
|
|
194
166
|
[splat]: [error] = []
|
|
195
167
|
} = data;
|
|
196
|
-
|
|
197
168
|
if (!(error instanceof Error)) {
|
|
198
169
|
return undefined;
|
|
199
170
|
}
|
|
200
|
-
|
|
201
171
|
const {
|
|
202
172
|
name,
|
|
203
173
|
message,
|
|
204
174
|
stack: rawStack = ''
|
|
205
175
|
} = error;
|
|
206
|
-
|
|
207
|
-
const stack = _mapInstanceProperty__default["default"](_context = _mapInstanceProperty__default["default"](_context2 = _mapInstanceProperty__default["default"](_context3 = _sliceInstanceProperty__default["default"](rawStack).call(rawStack, name.length + message.length + 3).split('\n')).call(_context3, line => line.replace(/^\s+at /g, ''))).call(_context2, line => path__default["default"].isAbsolute(line) ? `<anonymous>${conjunction}${mapLocationOfErrorThrown(line)}` : line)).call(_context, line => line.replace(/^(.+) \((.+)\)$/, (matches, contextName, rawLocation) => {
|
|
176
|
+
const stack = _mapInstanceProperty(_context = _mapInstanceProperty(_context2 = _mapInstanceProperty(_context3 = _sliceInstanceProperty(rawStack).call(rawStack, name.length + message.length + 3).split('\n')).call(_context3, line => line.replace(/^\s+at /g, ''))).call(_context2, line => path.isAbsolute(line) ? `<anonymous>${conjunction}${mapLocationOfErrorThrown(line)}` : line)).call(_context, line => line.replace(/^(.+) \((.+)\)$/, (matches, contextName, rawLocation) => {
|
|
208
177
|
let location;
|
|
209
|
-
|
|
210
178
|
if (rawLocation === '<anonymous>') {
|
|
211
|
-
location =
|
|
179
|
+
location = chalk.gray(rawLocation);
|
|
212
180
|
} else {
|
|
213
181
|
const [filename, line, column] = rawLocation.split(':');
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
const
|
|
217
|
-
const trailing = chalk__default["default"].gray(`:${line}:${column}`);
|
|
182
|
+
if (path.isAbsolute(filename)) {
|
|
183
|
+
const target = mapLocationOfErrorThrown(path.relative(rootPath, filename));
|
|
184
|
+
const trailing = chalk.gray(`:${line}:${column}`);
|
|
218
185
|
location = target + trailing;
|
|
219
186
|
} else {
|
|
220
|
-
location =
|
|
187
|
+
location = chalk.gray(`https://github.com/${chalk.bold.underline('nodejs')}/node/blob/${process.version}/lib/${filename}#L${line}`); // eslint-disable-line max-len
|
|
221
188
|
}
|
|
222
189
|
}
|
|
223
|
-
|
|
224
190
|
return `${contextName}${conjunction}${location}`;
|
|
225
191
|
})).join('\n');
|
|
226
|
-
|
|
227
192
|
const label = decorateLabel(name, ColorScheme.Critical);
|
|
228
193
|
const payload = `${message}\n\n${stack}\n`;
|
|
229
194
|
return {
|
|
@@ -234,28 +199,30 @@ function handle$1(data) {
|
|
|
234
199
|
|
|
235
200
|
var ErrorHandler = /*#__PURE__*/Object.freeze({
|
|
236
201
|
__proto__: null,
|
|
237
|
-
|
|
238
|
-
level: level$8,
|
|
202
|
+
default: handle$1,
|
|
239
203
|
label: label$8,
|
|
240
|
-
|
|
204
|
+
level: level$8,
|
|
205
|
+
name: name$8
|
|
241
206
|
});
|
|
242
207
|
|
|
243
208
|
// Local helpers.
|
|
244
209
|
|
|
210
|
+
// Handler metadata.
|
|
245
211
|
const name$7 = 'fatal';
|
|
246
212
|
const level$7 = LoggingLevel.Fatal;
|
|
247
213
|
const label$7 = decorateLabel('FATAL', ColorScheme.Critical);
|
|
248
214
|
|
|
249
215
|
var FatalHandler = /*#__PURE__*/Object.freeze({
|
|
250
216
|
__proto__: null,
|
|
251
|
-
|
|
252
|
-
level: level$7,
|
|
217
|
+
default: handle$1,
|
|
253
218
|
label: label$7,
|
|
254
|
-
|
|
219
|
+
level: level$7,
|
|
220
|
+
name: name$7
|
|
255
221
|
});
|
|
256
222
|
|
|
257
223
|
// Local helpers.
|
|
258
224
|
|
|
225
|
+
// Handler metadata.
|
|
259
226
|
const name$6 = 'warning';
|
|
260
227
|
const level$6 = LoggingLevel.Warning;
|
|
261
228
|
const label$6 = decorateLabel('WARNING', ColorScheme.Warning);
|
|
@@ -263,14 +230,15 @@ var warning = null;
|
|
|
263
230
|
|
|
264
231
|
var WarningHandler = /*#__PURE__*/Object.freeze({
|
|
265
232
|
__proto__: null,
|
|
266
|
-
|
|
267
|
-
level: level$6,
|
|
233
|
+
default: warning,
|
|
268
234
|
label: label$6,
|
|
269
|
-
|
|
235
|
+
level: level$6,
|
|
236
|
+
name: name$6
|
|
270
237
|
});
|
|
271
238
|
|
|
272
239
|
// Local helpers.
|
|
273
240
|
|
|
241
|
+
// Handler metadata.
|
|
274
242
|
const name$5 = 'notice';
|
|
275
243
|
const level$5 = LoggingLevel.Notice;
|
|
276
244
|
const label$5 = decorateLabel('NOTICE', ColorScheme.Regular);
|
|
@@ -278,14 +246,15 @@ var notice = null;
|
|
|
278
246
|
|
|
279
247
|
var NoticeHandler = /*#__PURE__*/Object.freeze({
|
|
280
248
|
__proto__: null,
|
|
281
|
-
|
|
282
|
-
level: level$5,
|
|
249
|
+
default: notice,
|
|
283
250
|
label: label$5,
|
|
284
|
-
|
|
251
|
+
level: level$5,
|
|
252
|
+
name: name$5
|
|
285
253
|
});
|
|
286
254
|
|
|
287
255
|
// Local helpers.
|
|
288
256
|
|
|
257
|
+
// Handler metadata.
|
|
289
258
|
const name$4 = 'info';
|
|
290
259
|
const level$4 = LoggingLevel.Info;
|
|
291
260
|
const label$4 = decorateLabel('INFO', ColorScheme.Regular);
|
|
@@ -293,14 +262,15 @@ var info = null;
|
|
|
293
262
|
|
|
294
263
|
var InfoHandler = /*#__PURE__*/Object.freeze({
|
|
295
264
|
__proto__: null,
|
|
296
|
-
|
|
297
|
-
level: level$4,
|
|
265
|
+
default: info,
|
|
298
266
|
label: label$4,
|
|
299
|
-
|
|
267
|
+
level: level$4,
|
|
268
|
+
name: name$4
|
|
300
269
|
});
|
|
301
270
|
|
|
302
271
|
// Local helpers.
|
|
303
272
|
|
|
273
|
+
// Handler metadata.
|
|
304
274
|
const name$3 = 'http';
|
|
305
275
|
const level$3 = LoggingLevel.Http;
|
|
306
276
|
const label$3 = decorateLabel('HTTP', ColorScheme.Http);
|
|
@@ -308,20 +278,22 @@ var http = null;
|
|
|
308
278
|
|
|
309
279
|
var HttpHandler = /*#__PURE__*/Object.freeze({
|
|
310
280
|
__proto__: null,
|
|
311
|
-
|
|
312
|
-
level: level$3,
|
|
281
|
+
default: http,
|
|
313
282
|
label: label$3,
|
|
314
|
-
|
|
283
|
+
level: level$3,
|
|
284
|
+
name: name$3
|
|
315
285
|
});
|
|
316
286
|
|
|
317
287
|
// Highlight.js
|
|
318
288
|
|
|
289
|
+
// Type definitions.
|
|
290
|
+
|
|
319
291
|
// Handler metadata.
|
|
320
292
|
const name$2 = 'query';
|
|
321
293
|
const level$2 = LoggingLevel.Query;
|
|
322
294
|
const label$2 = decorateLabel('QUERY', ColorScheme.Query);
|
|
323
295
|
function handle(data) {
|
|
324
|
-
const payload =
|
|
296
|
+
const payload = highlight(data.message, {
|
|
325
297
|
language: 'sql'
|
|
326
298
|
});
|
|
327
299
|
return {
|
|
@@ -332,14 +304,15 @@ function handle(data) {
|
|
|
332
304
|
|
|
333
305
|
var QueryHandler = /*#__PURE__*/Object.freeze({
|
|
334
306
|
__proto__: null,
|
|
335
|
-
|
|
336
|
-
level: level$2,
|
|
307
|
+
default: handle,
|
|
337
308
|
label: label$2,
|
|
338
|
-
|
|
309
|
+
level: level$2,
|
|
310
|
+
name: name$2
|
|
339
311
|
});
|
|
340
312
|
|
|
341
313
|
// Local helpers.
|
|
342
314
|
|
|
315
|
+
// Handler metadata.
|
|
343
316
|
const name$1 = 'verbose';
|
|
344
317
|
const level$1 = LoggingLevel.Verbose;
|
|
345
318
|
const label$1 = decorateLabel('VERBOSE', ColorScheme.Loud);
|
|
@@ -347,14 +320,15 @@ var verbose = null;
|
|
|
347
320
|
|
|
348
321
|
var VerboseHandler = /*#__PURE__*/Object.freeze({
|
|
349
322
|
__proto__: null,
|
|
350
|
-
|
|
351
|
-
level: level$1,
|
|
323
|
+
default: verbose,
|
|
352
324
|
label: label$1,
|
|
353
|
-
|
|
325
|
+
level: level$1,
|
|
326
|
+
name: name$1
|
|
354
327
|
});
|
|
355
328
|
|
|
356
329
|
// Local helpers.
|
|
357
330
|
|
|
331
|
+
// Handler metadata.
|
|
358
332
|
const name = 'debug';
|
|
359
333
|
const level = LoggingLevel.Debug;
|
|
360
334
|
const label = decorateLabel('DEBUG', ColorScheme.Loud);
|
|
@@ -362,47 +336,43 @@ var debug = null;
|
|
|
362
336
|
|
|
363
337
|
var DebugHandler = /*#__PURE__*/Object.freeze({
|
|
364
338
|
__proto__: null,
|
|
365
|
-
|
|
366
|
-
level: level,
|
|
339
|
+
default: debug,
|
|
367
340
|
label: label,
|
|
368
|
-
|
|
341
|
+
level: level,
|
|
342
|
+
name: name
|
|
369
343
|
});
|
|
370
344
|
|
|
371
345
|
// Handler implementations.
|
|
372
346
|
const handlers = [FatalHandler, ErrorHandler, WarningHandler, NoticeHandler, InfoHandler, HttpHandler, QueryHandler, VerboseHandler, DebugHandler];
|
|
373
347
|
|
|
374
348
|
var formatForCLI = winston.format.printf(data => {
|
|
375
|
-
const timestamp =
|
|
349
|
+
const timestamp = chalk.gray(luxon.DateTime.now().toISO({
|
|
376
350
|
includeOffset: true
|
|
377
351
|
}));
|
|
378
|
-
|
|
379
|
-
const handler = _findInstanceProperty__default["default"](handlers).call(handlers, ({
|
|
352
|
+
const handler = _findInstanceProperty(handlers).call(handlers, ({
|
|
380
353
|
name
|
|
381
354
|
}) => name === data.level);
|
|
382
|
-
|
|
383
355
|
const fallback = {
|
|
384
356
|
label: handler?.label,
|
|
385
357
|
payload: data.message
|
|
386
358
|
};
|
|
359
|
+
const performHandle = handler?.default ?? (() => fallback);
|
|
387
360
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
361
|
+
// Execute handler and then, get decorated payload.
|
|
391
362
|
const {
|
|
392
363
|
label,
|
|
393
364
|
payload
|
|
394
|
-
} = _Reflect$
|
|
365
|
+
} = _Reflect$apply(performHandle, undefined, [data]) ?? fallback;
|
|
395
366
|
let extraFingerprint;
|
|
396
|
-
|
|
397
367
|
if (handler?.level === LoggingLevel.Http) {
|
|
398
|
-
extraFingerprint = data.transactionId ? `(Transaction-ID = ${data.transactionId})` : `(Request-ID = ${
|
|
368
|
+
extraFingerprint = data.transactionId ? `(Transaction-ID = ${data.transactionId})` : `(Request-ID = ${rTracer.id()})`;
|
|
399
369
|
}
|
|
400
|
-
|
|
401
370
|
return `${label} ${timestamp}${extraFingerprint ? ` ${extraFingerprint}` : ''}\n${payload}`;
|
|
402
371
|
});
|
|
403
372
|
|
|
404
373
|
// Node.js built-in APIs.
|
|
405
374
|
|
|
375
|
+
// Type definitions.
|
|
406
376
|
const ExecutionMode = {
|
|
407
377
|
DevelopmentMode: 'development',
|
|
408
378
|
ProductionMode: 'production'
|
|
@@ -413,81 +383,78 @@ const ClusteringMode = {
|
|
|
413
383
|
};
|
|
414
384
|
// Exportings.
|
|
415
385
|
const executionMode = process.env.NODE_ENV ?? ExecutionMode.DevelopmentMode;
|
|
416
|
-
|
|
386
|
+
cluster.isPrimary ?? cluster.isMaster ? ClusteringMode.MasterProcess : ClusteringMode.WorkerProcess;
|
|
417
387
|
|
|
418
388
|
// Node.js built-in APIs.
|
|
419
389
|
|
|
390
|
+
// Type definitions.
|
|
391
|
+
|
|
420
392
|
// Helpers.
|
|
421
393
|
function decorateStatusCode(statusCode) {
|
|
422
394
|
const groupOfCode = Math.floor(Number(statusCode) / 100) * 100;
|
|
423
395
|
const baseMessage = `${statusCode} ${http$1.STATUS_CODES[statusCode]}`;
|
|
424
|
-
|
|
425
396
|
switch (groupOfCode) {
|
|
426
397
|
case 200:
|
|
427
398
|
{
|
|
428
|
-
return
|
|
399
|
+
return chalk.green(baseMessage);
|
|
429
400
|
}
|
|
430
|
-
|
|
431
401
|
case 300:
|
|
432
402
|
{
|
|
433
|
-
return
|
|
403
|
+
return chalk.magenta(baseMessage);
|
|
434
404
|
}
|
|
435
|
-
|
|
436
405
|
case 400:
|
|
437
406
|
case 500:
|
|
438
407
|
{
|
|
439
|
-
return
|
|
408
|
+
return chalk.red(baseMessage);
|
|
440
409
|
}
|
|
441
|
-
|
|
442
410
|
default:
|
|
443
411
|
{
|
|
444
|
-
return
|
|
412
|
+
return chalk.gray(baseMessage);
|
|
445
413
|
}
|
|
446
414
|
}
|
|
447
415
|
}
|
|
448
|
-
|
|
449
416
|
function formatForMorgan(tokens, request, response) {
|
|
450
417
|
function _(token, ...subArgs) {
|
|
451
418
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
452
419
|
return tokens[token](request, response, ...subArgs);
|
|
453
|
-
}
|
|
454
|
-
|
|
420
|
+
}
|
|
455
421
|
|
|
422
|
+
// Raw values.
|
|
456
423
|
const method = _('method');
|
|
457
|
-
|
|
458
424
|
const URL = _('url');
|
|
459
|
-
|
|
460
425
|
const httpVersion = `HTTP/${_('http-version')}`;
|
|
461
426
|
const statusCode = Number(_('status'));
|
|
462
427
|
const contentLength = Number(_('res', 'content-length') ?? -1);
|
|
463
|
-
const responseTime = Number(_('response-time') ?? -1);
|
|
428
|
+
const responseTime = Number(_('response-time') ?? -1);
|
|
464
429
|
|
|
465
|
-
|
|
466
|
-
const $
|
|
430
|
+
// Decorated values.
|
|
431
|
+
const $method = chalk.bold(method);
|
|
432
|
+
const $version = chalk.rgb(98, 177, 255)(httpVersion);
|
|
467
433
|
const $statusCode = decorateStatusCode(statusCode);
|
|
468
|
-
const $contentLength = contentLength >= 0 ?
|
|
469
|
-
const $responseTime = responseTime >= 0 ?
|
|
434
|
+
const $contentLength = contentLength >= 0 ? prettyBytes(contentLength) : '? B';
|
|
435
|
+
const $responseTime = responseTime >= 0 ? prettyMs(responseTime) : '';
|
|
470
436
|
|
|
437
|
+
// Additional information.
|
|
471
438
|
const metadata = `(${$contentLength}, ${$responseTime})`;
|
|
472
439
|
return [`→ ${$method} ${URL} ${$version}`, `← ${$version} ${$statusCode} ${metadata}`].join('\n');
|
|
473
440
|
}
|
|
474
|
-
|
|
475
|
-
var formatForMorgan$1 = executionMode === ExecutionMode.DevelopmentMode ? formatForMorgan : morgan__default["default"].combined;
|
|
441
|
+
var formatForMorgan$1 = executionMode === ExecutionMode.DevelopmentMode ? formatForMorgan : morgan.combined;
|
|
476
442
|
|
|
477
443
|
var _context, _context2;
|
|
478
444
|
|
|
445
|
+
// Type definitions.
|
|
446
|
+
|
|
479
447
|
// Transports.
|
|
480
448
|
const {
|
|
481
449
|
Console: ConsoleTransport
|
|
482
|
-
} = winston__namespace.transports;
|
|
483
|
-
|
|
484
|
-
const KeyOfInstance = _Symbol__default["default"]('@cichol/logger::classes/logger::Instance');
|
|
485
|
-
|
|
486
|
-
const KeyOfPassport = _Symbol__default["default"]('@cichol/logger::classes/logger::Passport');
|
|
450
|
+
} = winston__namespace.transports;
|
|
487
451
|
|
|
452
|
+
// Keys in order to access private properties.
|
|
453
|
+
const KeyOfInstance = _Symbol('@cichol/logger::classes/logger::Instance');
|
|
454
|
+
const KeyOfPassport = _Symbol('@cichol/logger::classes/logger::Passport');
|
|
488
455
|
class Logger {
|
|
489
456
|
#logger = winston__namespace.createLogger({
|
|
490
|
-
levels:
|
|
457
|
+
levels: _filterInstanceProperty(_context = _mapInstanceProperty(_context2 = lodash(handlers)).call(_context2, ({
|
|
491
458
|
name,
|
|
492
459
|
level
|
|
493
460
|
}) => [name, level])).call(_context, ([, level]) => typeof level === 'number').fromPairs().value(),
|
|
@@ -497,37 +464,33 @@ class Logger {
|
|
|
497
464
|
})],
|
|
498
465
|
exitOnError: false
|
|
499
466
|
});
|
|
500
|
-
#morgan =
|
|
467
|
+
#morgan = morgan(formatForMorgan$1, {
|
|
501
468
|
stream: new stream.PassThrough({
|
|
502
469
|
objectMode: true
|
|
503
470
|
}).on('data', data => {
|
|
504
|
-
this.log('http',
|
|
471
|
+
this.log('http', _trimInstanceProperty(data).call(data));
|
|
505
472
|
})
|
|
506
473
|
});
|
|
507
|
-
|
|
508
474
|
constructor(passport) {
|
|
509
475
|
if (passport !== KeyOfPassport) {
|
|
510
476
|
throw new ReferenceError('Logger class cannot be constructed in directly');
|
|
511
477
|
}
|
|
512
478
|
}
|
|
513
|
-
|
|
514
479
|
static get instance() {
|
|
515
480
|
if (this !== Logger) {
|
|
516
481
|
throw new ReferenceError('Logger class is not inheritable');
|
|
517
482
|
}
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
return _Reflect$getOwnMetadata__default["default"](KeyOfInstance, Logger);
|
|
483
|
+
if (_Reflect$hasOwnMetadata(KeyOfInstance, Logger)) {
|
|
484
|
+
return _Reflect$getOwnMetadata(KeyOfInstance, Logger);
|
|
521
485
|
}
|
|
522
|
-
|
|
523
486
|
const instance = new Logger(KeyOfPassport);
|
|
524
|
-
|
|
525
487
|
try {
|
|
526
488
|
return instance;
|
|
527
489
|
} finally {
|
|
528
|
-
_Reflect$
|
|
490
|
+
_Reflect$defineMetadata(KeyOfInstance, instance, Logger);
|
|
529
491
|
}
|
|
530
492
|
}
|
|
493
|
+
|
|
531
494
|
/**
|
|
532
495
|
* Logging message with specific level.
|
|
533
496
|
*
|
|
@@ -536,38 +499,47 @@ class Logger {
|
|
|
536
499
|
* @param args Additional metadata.
|
|
537
500
|
*/
|
|
538
501
|
|
|
502
|
+
/**
|
|
503
|
+
* Logging HTTP I/O.
|
|
504
|
+
*
|
|
505
|
+
* @param request Node.js built-in `IncomingMessage` instance from `http` module.
|
|
506
|
+
* @param response Node.js built-in `ServerResponse` instance from `http` module.
|
|
507
|
+
*/
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* Logging errors in detail.
|
|
511
|
+
*
|
|
512
|
+
* @param error
|
|
513
|
+
*/
|
|
539
514
|
|
|
540
515
|
log(...args) {
|
|
541
516
|
const logger = this.#logger;
|
|
542
517
|
const morgan = this.#morgan;
|
|
543
|
-
|
|
544
518
|
switch (true) {
|
|
545
519
|
case args.length >= 2 && args[0] instanceof http$1.IncomingMessage && args[1] instanceof http$1.ServerResponse:
|
|
546
520
|
{
|
|
547
521
|
const [request, response] = args;
|
|
548
|
-
return
|
|
522
|
+
return Promise$1.fromCallback(callback => morgan(request, response, callback));
|
|
549
523
|
}
|
|
550
|
-
|
|
551
524
|
case args.length >= 1 && args[0] instanceof Error:
|
|
552
525
|
{
|
|
553
|
-
const [error, ...data] = args;
|
|
526
|
+
const [error, ...data] = args;
|
|
554
527
|
|
|
528
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
555
529
|
logger.log('error', error.message, error, ...data);
|
|
556
530
|
return undefined;
|
|
557
531
|
}
|
|
558
|
-
|
|
559
532
|
default:
|
|
560
533
|
{
|
|
561
|
-
const [level, message, ...data] = args;
|
|
534
|
+
const [level, message, ...data] = args;
|
|
562
535
|
|
|
536
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
563
537
|
logger.log(level, message, ...data);
|
|
564
538
|
return undefined;
|
|
565
539
|
}
|
|
566
540
|
}
|
|
567
541
|
}
|
|
568
|
-
|
|
569
542
|
}
|
|
570
|
-
|
|
571
543
|
var logger = Logger.instance;
|
|
572
544
|
|
|
573
545
|
module.exports = logger;
|
package/releases/esm.js
CHANGED
|
@@ -47,16 +47,18 @@ const LoggingLevel = {
|
|
|
47
47
|
Debug: 5
|
|
48
48
|
};
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
// Constants.
|
|
51
|
+
const colorizerMap = new _Map();
|
|
51
52
|
|
|
53
|
+
// Helpers.
|
|
52
54
|
function createScheme(options) {
|
|
53
55
|
const nextIndex = colorizerMap.size;
|
|
54
56
|
const colorizer = chalk.bgHex(options.background).hex(options.foreground);
|
|
55
57
|
colorizerMap.set(nextIndex, colorizer);
|
|
56
58
|
return nextIndex;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
+
}
|
|
59
60
|
|
|
61
|
+
// Color definitions.
|
|
60
62
|
const ColorValue = {
|
|
61
63
|
// Basic colors.
|
|
62
64
|
White: '#FFFFFF',
|
|
@@ -98,10 +100,12 @@ const ColorScheme = {
|
|
|
98
100
|
function getChalkOf(colorSet) {
|
|
99
101
|
return colorizerMap.get(colorSet) ?? chalk;
|
|
100
102
|
}
|
|
103
|
+
|
|
101
104
|
/* eslint-enable no-use-before-define */
|
|
102
105
|
|
|
103
106
|
// Node.js built-in APIs.
|
|
104
107
|
|
|
108
|
+
// Constants.
|
|
105
109
|
const {
|
|
106
110
|
isTTY
|
|
107
111
|
} = process.stdout;
|
|
@@ -110,61 +114,51 @@ function decorateLabel(label, scheme) {
|
|
|
110
114
|
if (isNotOnCLI) {
|
|
111
115
|
return label;
|
|
112
116
|
}
|
|
113
|
-
|
|
114
117
|
const chalk = getChalkOf(scheme);
|
|
115
118
|
const decoratedLabel = chalk.bold(` ${label} `);
|
|
116
119
|
return decoratedLabel;
|
|
117
120
|
}
|
|
118
121
|
|
|
122
|
+
// Type definitions.
|
|
123
|
+
|
|
119
124
|
// Constants.
|
|
120
125
|
const rootPath = process.cwd();
|
|
121
126
|
const conjunction = ` ${chalk.blue('──')} `;
|
|
127
|
+
const splat = _Symbol$for('splat');
|
|
122
128
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
129
|
+
// Handler metadata.
|
|
126
130
|
const name$8 = 'error';
|
|
127
131
|
const level$8 = LoggingLevel.Error;
|
|
128
132
|
const label$8 = decorateLabel('ERROR', ColorScheme.Critical);
|
|
129
|
-
|
|
130
133
|
function mapLocationOfErrorThrown(filename) {
|
|
131
134
|
let target = path.relative(rootPath, filename);
|
|
132
|
-
|
|
133
135
|
if (_startsWithInstanceProperty(target).call(target, 'node_modules')) {
|
|
134
136
|
target = chalk.gray(target).replace(/node_modules[\\/]([^\\/]+)(.*)/, (matches, moduleName, trailing) => `node_modules${path.sep}${chalk.bold.underline(moduleName)}${chalk.gray(trailing)}` // eslint-disable-line max-len
|
|
135
137
|
);
|
|
136
138
|
} else {
|
|
137
139
|
target = `${chalk.gray(path.dirname(target) + path.sep)}${chalk.bold.underline(path.basename(target))}`;
|
|
138
140
|
}
|
|
139
|
-
|
|
140
141
|
return target;
|
|
141
142
|
}
|
|
142
|
-
|
|
143
143
|
function handle$1(data) {
|
|
144
144
|
var _context, _context2, _context3;
|
|
145
|
-
|
|
146
145
|
const {
|
|
147
146
|
[splat]: [error] = []
|
|
148
147
|
} = data;
|
|
149
|
-
|
|
150
148
|
if (!(error instanceof Error)) {
|
|
151
149
|
return undefined;
|
|
152
150
|
}
|
|
153
|
-
|
|
154
151
|
const {
|
|
155
152
|
name,
|
|
156
153
|
message,
|
|
157
154
|
stack: rawStack = ''
|
|
158
155
|
} = error;
|
|
159
|
-
|
|
160
156
|
const stack = _mapInstanceProperty(_context = _mapInstanceProperty(_context2 = _mapInstanceProperty(_context3 = _sliceInstanceProperty(rawStack).call(rawStack, name.length + message.length + 3).split('\n')).call(_context3, line => line.replace(/^\s+at /g, ''))).call(_context2, line => path.isAbsolute(line) ? `<anonymous>${conjunction}${mapLocationOfErrorThrown(line)}` : line)).call(_context, line => line.replace(/^(.+) \((.+)\)$/, (matches, contextName, rawLocation) => {
|
|
161
157
|
let location;
|
|
162
|
-
|
|
163
158
|
if (rawLocation === '<anonymous>') {
|
|
164
159
|
location = chalk.gray(rawLocation);
|
|
165
160
|
} else {
|
|
166
161
|
const [filename, line, column] = rawLocation.split(':');
|
|
167
|
-
|
|
168
162
|
if (path.isAbsolute(filename)) {
|
|
169
163
|
const target = mapLocationOfErrorThrown(path.relative(rootPath, filename));
|
|
170
164
|
const trailing = chalk.gray(`:${line}:${column}`);
|
|
@@ -173,10 +167,8 @@ function handle$1(data) {
|
|
|
173
167
|
location = chalk.gray(`https://github.com/${chalk.bold.underline('nodejs')}/node/blob/${version}/lib/${filename}#L${line}`); // eslint-disable-line max-len
|
|
174
168
|
}
|
|
175
169
|
}
|
|
176
|
-
|
|
177
170
|
return `${contextName}${conjunction}${location}`;
|
|
178
171
|
})).join('\n');
|
|
179
|
-
|
|
180
172
|
const label = decorateLabel(name, ColorScheme.Critical);
|
|
181
173
|
const payload = `${message}\n\n${stack}\n`;
|
|
182
174
|
return {
|
|
@@ -187,28 +179,30 @@ function handle$1(data) {
|
|
|
187
179
|
|
|
188
180
|
var ErrorHandler = /*#__PURE__*/Object.freeze({
|
|
189
181
|
__proto__: null,
|
|
190
|
-
|
|
191
|
-
level: level$8,
|
|
182
|
+
default: handle$1,
|
|
192
183
|
label: label$8,
|
|
193
|
-
|
|
184
|
+
level: level$8,
|
|
185
|
+
name: name$8
|
|
194
186
|
});
|
|
195
187
|
|
|
196
188
|
// Local helpers.
|
|
197
189
|
|
|
190
|
+
// Handler metadata.
|
|
198
191
|
const name$7 = 'fatal';
|
|
199
192
|
const level$7 = LoggingLevel.Fatal;
|
|
200
193
|
const label$7 = decorateLabel('FATAL', ColorScheme.Critical);
|
|
201
194
|
|
|
202
195
|
var FatalHandler = /*#__PURE__*/Object.freeze({
|
|
203
196
|
__proto__: null,
|
|
204
|
-
|
|
205
|
-
level: level$7,
|
|
197
|
+
default: handle$1,
|
|
206
198
|
label: label$7,
|
|
207
|
-
|
|
199
|
+
level: level$7,
|
|
200
|
+
name: name$7
|
|
208
201
|
});
|
|
209
202
|
|
|
210
203
|
// Local helpers.
|
|
211
204
|
|
|
205
|
+
// Handler metadata.
|
|
212
206
|
const name$6 = 'warning';
|
|
213
207
|
const level$6 = LoggingLevel.Warning;
|
|
214
208
|
const label$6 = decorateLabel('WARNING', ColorScheme.Warning);
|
|
@@ -216,14 +210,15 @@ var warning = null;
|
|
|
216
210
|
|
|
217
211
|
var WarningHandler = /*#__PURE__*/Object.freeze({
|
|
218
212
|
__proto__: null,
|
|
219
|
-
|
|
220
|
-
level: level$6,
|
|
213
|
+
default: warning,
|
|
221
214
|
label: label$6,
|
|
222
|
-
|
|
215
|
+
level: level$6,
|
|
216
|
+
name: name$6
|
|
223
217
|
});
|
|
224
218
|
|
|
225
219
|
// Local helpers.
|
|
226
220
|
|
|
221
|
+
// Handler metadata.
|
|
227
222
|
const name$5 = 'notice';
|
|
228
223
|
const level$5 = LoggingLevel.Notice;
|
|
229
224
|
const label$5 = decorateLabel('NOTICE', ColorScheme.Regular);
|
|
@@ -231,14 +226,15 @@ var notice = null;
|
|
|
231
226
|
|
|
232
227
|
var NoticeHandler = /*#__PURE__*/Object.freeze({
|
|
233
228
|
__proto__: null,
|
|
234
|
-
|
|
235
|
-
level: level$5,
|
|
229
|
+
default: notice,
|
|
236
230
|
label: label$5,
|
|
237
|
-
|
|
231
|
+
level: level$5,
|
|
232
|
+
name: name$5
|
|
238
233
|
});
|
|
239
234
|
|
|
240
235
|
// Local helpers.
|
|
241
236
|
|
|
237
|
+
// Handler metadata.
|
|
242
238
|
const name$4 = 'info';
|
|
243
239
|
const level$4 = LoggingLevel.Info;
|
|
244
240
|
const label$4 = decorateLabel('INFO', ColorScheme.Regular);
|
|
@@ -246,14 +242,15 @@ var info = null;
|
|
|
246
242
|
|
|
247
243
|
var InfoHandler = /*#__PURE__*/Object.freeze({
|
|
248
244
|
__proto__: null,
|
|
249
|
-
|
|
250
|
-
level: level$4,
|
|
245
|
+
default: info,
|
|
251
246
|
label: label$4,
|
|
252
|
-
|
|
247
|
+
level: level$4,
|
|
248
|
+
name: name$4
|
|
253
249
|
});
|
|
254
250
|
|
|
255
251
|
// Local helpers.
|
|
256
252
|
|
|
253
|
+
// Handler metadata.
|
|
257
254
|
const name$3 = 'http';
|
|
258
255
|
const level$3 = LoggingLevel.Http;
|
|
259
256
|
const label$3 = decorateLabel('HTTP', ColorScheme.Http);
|
|
@@ -261,14 +258,16 @@ var http = null;
|
|
|
261
258
|
|
|
262
259
|
var HttpHandler = /*#__PURE__*/Object.freeze({
|
|
263
260
|
__proto__: null,
|
|
264
|
-
|
|
265
|
-
level: level$3,
|
|
261
|
+
default: http,
|
|
266
262
|
label: label$3,
|
|
267
|
-
|
|
263
|
+
level: level$3,
|
|
264
|
+
name: name$3
|
|
268
265
|
});
|
|
269
266
|
|
|
270
267
|
// Highlight.js
|
|
271
268
|
|
|
269
|
+
// Type definitions.
|
|
270
|
+
|
|
272
271
|
// Handler metadata.
|
|
273
272
|
const name$2 = 'query';
|
|
274
273
|
const level$2 = LoggingLevel.Query;
|
|
@@ -285,14 +284,15 @@ function handle(data) {
|
|
|
285
284
|
|
|
286
285
|
var QueryHandler = /*#__PURE__*/Object.freeze({
|
|
287
286
|
__proto__: null,
|
|
288
|
-
|
|
289
|
-
level: level$2,
|
|
287
|
+
default: handle,
|
|
290
288
|
label: label$2,
|
|
291
|
-
|
|
289
|
+
level: level$2,
|
|
290
|
+
name: name$2
|
|
292
291
|
});
|
|
293
292
|
|
|
294
293
|
// Local helpers.
|
|
295
294
|
|
|
295
|
+
// Handler metadata.
|
|
296
296
|
const name$1 = 'verbose';
|
|
297
297
|
const level$1 = LoggingLevel.Verbose;
|
|
298
298
|
const label$1 = decorateLabel('VERBOSE', ColorScheme.Loud);
|
|
@@ -300,14 +300,15 @@ var verbose = null;
|
|
|
300
300
|
|
|
301
301
|
var VerboseHandler = /*#__PURE__*/Object.freeze({
|
|
302
302
|
__proto__: null,
|
|
303
|
-
|
|
304
|
-
level: level$1,
|
|
303
|
+
default: verbose,
|
|
305
304
|
label: label$1,
|
|
306
|
-
|
|
305
|
+
level: level$1,
|
|
306
|
+
name: name$1
|
|
307
307
|
});
|
|
308
308
|
|
|
309
309
|
// Local helpers.
|
|
310
310
|
|
|
311
|
+
// Handler metadata.
|
|
311
312
|
const name = 'debug';
|
|
312
313
|
const level = LoggingLevel.Debug;
|
|
313
314
|
const label = decorateLabel('DEBUG', ColorScheme.Loud);
|
|
@@ -315,10 +316,10 @@ var debug = null;
|
|
|
315
316
|
|
|
316
317
|
var DebugHandler = /*#__PURE__*/Object.freeze({
|
|
317
318
|
__proto__: null,
|
|
318
|
-
|
|
319
|
-
level: level,
|
|
319
|
+
default: debug,
|
|
320
320
|
label: label,
|
|
321
|
-
|
|
321
|
+
level: level,
|
|
322
|
+
name: name
|
|
322
323
|
});
|
|
323
324
|
|
|
324
325
|
// Handler implementations.
|
|
@@ -328,34 +329,30 @@ var formatForCLI = format.printf(data => {
|
|
|
328
329
|
const timestamp = chalk.gray(DateTime.now().toISO({
|
|
329
330
|
includeOffset: true
|
|
330
331
|
}));
|
|
331
|
-
|
|
332
332
|
const handler = _findInstanceProperty(handlers).call(handlers, ({
|
|
333
333
|
name
|
|
334
334
|
}) => name === data.level);
|
|
335
|
-
|
|
336
335
|
const fallback = {
|
|
337
336
|
label: handler?.label,
|
|
338
337
|
payload: data.message
|
|
339
338
|
};
|
|
339
|
+
const performHandle = handler?.default ?? (() => fallback);
|
|
340
340
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
341
|
+
// Execute handler and then, get decorated payload.
|
|
344
342
|
const {
|
|
345
343
|
label,
|
|
346
344
|
payload
|
|
347
345
|
} = _Reflect$apply(performHandle, undefined, [data]) ?? fallback;
|
|
348
346
|
let extraFingerprint;
|
|
349
|
-
|
|
350
347
|
if (handler?.level === LoggingLevel.Http) {
|
|
351
348
|
extraFingerprint = data.transactionId ? `(Transaction-ID = ${data.transactionId})` : `(Request-ID = ${rTracer.id()})`;
|
|
352
349
|
}
|
|
353
|
-
|
|
354
350
|
return `${label} ${timestamp}${extraFingerprint ? ` ${extraFingerprint}` : ''}\n${payload}`;
|
|
355
351
|
});
|
|
356
352
|
|
|
357
353
|
// Node.js built-in APIs.
|
|
358
354
|
|
|
355
|
+
// Type definitions.
|
|
359
356
|
const ExecutionMode = {
|
|
360
357
|
DevelopmentMode: 'development',
|
|
361
358
|
ProductionMode: 'production'
|
|
@@ -370,74 +367,71 @@ cluster.isPrimary ?? cluster.isMaster ? ClusteringMode.MasterProcess : Clusterin
|
|
|
370
367
|
|
|
371
368
|
// Node.js built-in APIs.
|
|
372
369
|
|
|
370
|
+
// Type definitions.
|
|
371
|
+
|
|
373
372
|
// Helpers.
|
|
374
373
|
function decorateStatusCode(statusCode) {
|
|
375
374
|
const groupOfCode = Math.floor(Number(statusCode) / 100) * 100;
|
|
376
375
|
const baseMessage = `${statusCode} ${STATUS_CODES[statusCode]}`;
|
|
377
|
-
|
|
378
376
|
switch (groupOfCode) {
|
|
379
377
|
case 200:
|
|
380
378
|
{
|
|
381
379
|
return chalk.green(baseMessage);
|
|
382
380
|
}
|
|
383
|
-
|
|
384
381
|
case 300:
|
|
385
382
|
{
|
|
386
383
|
return chalk.magenta(baseMessage);
|
|
387
384
|
}
|
|
388
|
-
|
|
389
385
|
case 400:
|
|
390
386
|
case 500:
|
|
391
387
|
{
|
|
392
388
|
return chalk.red(baseMessage);
|
|
393
389
|
}
|
|
394
|
-
|
|
395
390
|
default:
|
|
396
391
|
{
|
|
397
392
|
return chalk.gray(baseMessage);
|
|
398
393
|
}
|
|
399
394
|
}
|
|
400
395
|
}
|
|
401
|
-
|
|
402
396
|
function formatForMorgan(tokens, request, response) {
|
|
403
397
|
function _(token, ...subArgs) {
|
|
404
398
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
405
399
|
return tokens[token](request, response, ...subArgs);
|
|
406
|
-
}
|
|
407
|
-
|
|
400
|
+
}
|
|
408
401
|
|
|
402
|
+
// Raw values.
|
|
409
403
|
const method = _('method');
|
|
410
|
-
|
|
411
404
|
const URL = _('url');
|
|
412
|
-
|
|
413
405
|
const httpVersion = `HTTP/${_('http-version')}`;
|
|
414
406
|
const statusCode = Number(_('status'));
|
|
415
407
|
const contentLength = Number(_('res', 'content-length') ?? -1);
|
|
416
|
-
const responseTime = Number(_('response-time') ?? -1);
|
|
408
|
+
const responseTime = Number(_('response-time') ?? -1);
|
|
417
409
|
|
|
410
|
+
// Decorated values.
|
|
418
411
|
const $method = chalk.bold(method);
|
|
419
412
|
const $version = chalk.rgb(98, 177, 255)(httpVersion);
|
|
420
413
|
const $statusCode = decorateStatusCode(statusCode);
|
|
421
414
|
const $contentLength = contentLength >= 0 ? prettyBytes(contentLength) : '? B';
|
|
422
|
-
const $responseTime = responseTime >= 0 ? prettyMs(responseTime) : '';
|
|
415
|
+
const $responseTime = responseTime >= 0 ? prettyMs(responseTime) : '';
|
|
423
416
|
|
|
417
|
+
// Additional information.
|
|
424
418
|
const metadata = `(${$contentLength}, ${$responseTime})`;
|
|
425
419
|
return [`→ ${$method} ${URL} ${$version}`, `← ${$version} ${$statusCode} ${metadata}`].join('\n');
|
|
426
420
|
}
|
|
427
|
-
|
|
428
421
|
var formatForMorgan$1 = executionMode === ExecutionMode.DevelopmentMode ? formatForMorgan : morgan.combined;
|
|
429
422
|
|
|
430
423
|
var _context, _context2;
|
|
431
424
|
|
|
425
|
+
// Type definitions.
|
|
426
|
+
|
|
432
427
|
// Transports.
|
|
433
428
|
const {
|
|
434
429
|
Console: ConsoleTransport
|
|
435
|
-
} = winston.transports;
|
|
430
|
+
} = winston.transports;
|
|
436
431
|
|
|
432
|
+
// Keys in order to access private properties.
|
|
437
433
|
const KeyOfInstance = _Symbol('@cichol/logger::classes/logger::Instance');
|
|
438
|
-
|
|
439
434
|
const KeyOfPassport = _Symbol('@cichol/logger::classes/logger::Passport');
|
|
440
|
-
|
|
441
435
|
class Logger {
|
|
442
436
|
#logger = winston.createLogger({
|
|
443
437
|
levels: _filterInstanceProperty(_context = _mapInstanceProperty(_context2 = lodash(handlers)).call(_context2, ({
|
|
@@ -457,30 +451,26 @@ class Logger {
|
|
|
457
451
|
this.log('http', _trimInstanceProperty(data).call(data));
|
|
458
452
|
})
|
|
459
453
|
});
|
|
460
|
-
|
|
461
454
|
constructor(passport) {
|
|
462
455
|
if (passport !== KeyOfPassport) {
|
|
463
456
|
throw new ReferenceError('Logger class cannot be constructed in directly');
|
|
464
457
|
}
|
|
465
458
|
}
|
|
466
|
-
|
|
467
459
|
static get instance() {
|
|
468
460
|
if (this !== Logger) {
|
|
469
461
|
throw new ReferenceError('Logger class is not inheritable');
|
|
470
462
|
}
|
|
471
|
-
|
|
472
463
|
if (_Reflect$hasOwnMetadata(KeyOfInstance, Logger)) {
|
|
473
464
|
return _Reflect$getOwnMetadata(KeyOfInstance, Logger);
|
|
474
465
|
}
|
|
475
|
-
|
|
476
466
|
const instance = new Logger(KeyOfPassport);
|
|
477
|
-
|
|
478
467
|
try {
|
|
479
468
|
return instance;
|
|
480
469
|
} finally {
|
|
481
470
|
_Reflect$defineMetadata(KeyOfInstance, instance, Logger);
|
|
482
471
|
}
|
|
483
472
|
}
|
|
473
|
+
|
|
484
474
|
/**
|
|
485
475
|
* Logging message with specific level.
|
|
486
476
|
*
|
|
@@ -489,38 +479,47 @@ class Logger {
|
|
|
489
479
|
* @param args Additional metadata.
|
|
490
480
|
*/
|
|
491
481
|
|
|
482
|
+
/**
|
|
483
|
+
* Logging HTTP I/O.
|
|
484
|
+
*
|
|
485
|
+
* @param request Node.js built-in `IncomingMessage` instance from `http` module.
|
|
486
|
+
* @param response Node.js built-in `ServerResponse` instance from `http` module.
|
|
487
|
+
*/
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* Logging errors in detail.
|
|
491
|
+
*
|
|
492
|
+
* @param error
|
|
493
|
+
*/
|
|
492
494
|
|
|
493
495
|
log(...args) {
|
|
494
496
|
const logger = this.#logger;
|
|
495
497
|
const morgan = this.#morgan;
|
|
496
|
-
|
|
497
498
|
switch (true) {
|
|
498
499
|
case args.length >= 2 && args[0] instanceof IncomingMessage && args[1] instanceof ServerResponse:
|
|
499
500
|
{
|
|
500
501
|
const [request, response] = args;
|
|
501
502
|
return Promise$1.fromCallback(callback => morgan(request, response, callback));
|
|
502
503
|
}
|
|
503
|
-
|
|
504
504
|
case args.length >= 1 && args[0] instanceof Error:
|
|
505
505
|
{
|
|
506
|
-
const [error, ...data] = args;
|
|
506
|
+
const [error, ...data] = args;
|
|
507
507
|
|
|
508
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
508
509
|
logger.log('error', error.message, error, ...data);
|
|
509
510
|
return undefined;
|
|
510
511
|
}
|
|
511
|
-
|
|
512
512
|
default:
|
|
513
513
|
{
|
|
514
|
-
const [level, message, ...data] = args;
|
|
514
|
+
const [level, message, ...data] = args;
|
|
515
515
|
|
|
516
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
516
517
|
logger.log(level, message, ...data);
|
|
517
518
|
return undefined;
|
|
518
519
|
}
|
|
519
520
|
}
|
|
520
521
|
}
|
|
521
|
-
|
|
522
522
|
}
|
|
523
|
-
|
|
524
523
|
var logger = Logger.instance;
|
|
525
524
|
|
|
526
525
|
export { logger as default };
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import 'reflect-metadata';
|
|
3
|
-
import { IncomingMessage as HttpRequest, ServerResponse as HttpResponse } from 'http';
|
|
4
|
-
import Promise from 'bluebird';
|
|
5
|
-
import type { LoggingLevelName } from '../handlers';
|
|
6
|
-
declare class Logger {
|
|
7
|
-
#private;
|
|
8
|
-
private constructor();
|
|
9
|
-
static get instance(): Logger;
|
|
10
|
-
/**
|
|
11
|
-
* Logging message with specific level.
|
|
12
|
-
*
|
|
13
|
-
* @param level
|
|
14
|
-
* @param message
|
|
15
|
-
* @param args Additional metadata.
|
|
16
|
-
*/
|
|
17
|
-
log(level: LoggingLevelName, message: string, ...args: any[]): void;
|
|
18
|
-
/**
|
|
19
|
-
* Logging HTTP I/O.
|
|
20
|
-
*
|
|
21
|
-
* @param request Node.js built-in `IncomingMessage` instance from `http` module.
|
|
22
|
-
* @param response Node.js built-in `ServerResponse` instance from `http` module.
|
|
23
|
-
*/
|
|
24
|
-
log(request: HttpRequest, response: HttpResponse): Promise<void>;
|
|
25
|
-
/**
|
|
26
|
-
* Logging errors in detail.
|
|
27
|
-
*
|
|
28
|
-
* @param error
|
|
29
|
-
*/
|
|
30
|
-
log(error: Error): void;
|
|
31
|
-
}
|
|
32
|
-
declare const _default: Logger;
|
|
33
|
-
export default _default;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import { IncomingMessage as HttpRequest, ServerResponse as HttpResponse } from 'http';
|
|
3
|
-
import morgan from 'morgan';
|
|
4
|
-
declare module 'morgan' {
|
|
5
|
-
const combined: morgan.FormatFn;
|
|
6
|
-
}
|
|
7
|
-
declare const _default: morgan.FormatFn<HttpRequest, HttpResponse>;
|
|
8
|
-
export default _default;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
declare type LoggingData = {
|
|
2
|
-
level: string;
|
|
3
|
-
message: string;
|
|
4
|
-
} & Record<PropertyKey, any>;
|
|
5
|
-
declare type HandledData = {
|
|
6
|
-
label: string;
|
|
7
|
-
payload: string;
|
|
8
|
-
};
|
|
9
|
-
export declare const name = "error";
|
|
10
|
-
export declare const level: 1;
|
|
11
|
-
export declare const label: "ERROR";
|
|
12
|
-
export default function handle(data: LoggingData): HandledData | undefined;
|
|
13
|
-
export {};
|
package/types/handlers/http.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import * as FatalHandler from './fatal';
|
|
2
|
-
import * as ErrorHandler from './error';
|
|
3
|
-
import * as WarningHandler from './warning';
|
|
4
|
-
import * as NoticeHandler from './notice';
|
|
5
|
-
import * as InfoHandler from './info';
|
|
6
|
-
import * as HttpHandler from './http';
|
|
7
|
-
import * as QueryHandler from './query';
|
|
8
|
-
import * as VerboseHandler from './verbose';
|
|
9
|
-
import * as DebugHandler from './debug';
|
|
10
|
-
declare const handlers: (typeof FatalHandler | typeof ErrorHandler | typeof WarningHandler | typeof NoticeHandler | typeof InfoHandler | typeof HttpHandler | typeof QueryHandler | typeof VerboseHandler | typeof DebugHandler)[];
|
|
11
|
-
export declare type LoggingLevelName = (typeof handlers extends (infer T)[] ? T : never)['name'];
|
|
12
|
-
export default handlers;
|
package/types/handlers/info.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
declare type LoggingData = {
|
|
2
|
-
level: string;
|
|
3
|
-
message: string;
|
|
4
|
-
} & Record<PropertyKey, any>;
|
|
5
|
-
declare type HandledData = {
|
|
6
|
-
label: string;
|
|
7
|
-
payload: string;
|
|
8
|
-
};
|
|
9
|
-
export declare const name = "query";
|
|
10
|
-
export declare const level: 4;
|
|
11
|
-
export declare const label: "QUERY";
|
|
12
|
-
export default function handle(data: LoggingData): HandledData | undefined;
|
|
13
|
-
export {};
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { Chalk } from 'chalk';
|
|
2
|
-
declare const ColorScheme: {
|
|
3
|
-
readonly Critical: number;
|
|
4
|
-
readonly Warning: number;
|
|
5
|
-
readonly Regular: number;
|
|
6
|
-
readonly Http: number;
|
|
7
|
-
readonly Query: number;
|
|
8
|
-
readonly Loud: number;
|
|
9
|
-
};
|
|
10
|
-
declare type ColorScheme = typeof ColorScheme[keyof typeof ColorScheme];
|
|
11
|
-
export declare function getChalkOf(colorSet: ColorScheme): Chalk;
|
|
12
|
-
export default ColorScheme;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export declare const ExecutionMode: {
|
|
2
|
-
readonly DevelopmentMode: "development";
|
|
3
|
-
readonly ProductionMode: "production";
|
|
4
|
-
};
|
|
5
|
-
export declare const ClusteringMode: {
|
|
6
|
-
readonly MasterProcess: 0;
|
|
7
|
-
readonly WorkerProcess: 1;
|
|
8
|
-
};
|
|
9
|
-
export declare const ThreadingMode: {
|
|
10
|
-
readonly MainThread: 0;
|
|
11
|
-
readonly WorkerThread: 1;
|
|
12
|
-
};
|
|
13
|
-
export declare type ExecutionMode = typeof ExecutionMode[keyof typeof ExecutionMode];
|
|
14
|
-
export declare type ClusteringMode = typeof ClusteringMode[keyof typeof ClusteringMode];
|
|
15
|
-
export declare type ThreadingMode = typeof ThreadingMode[keyof typeof ThreadingMode];
|
|
16
|
-
export declare const executionMode: ExecutionMode;
|
|
17
|
-
export declare const clusteringMode: ClusteringMode;
|
|
18
|
-
export declare const threadingMode: ThreadingMode;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
declare const LoggingLevel: {
|
|
2
|
-
readonly Fatal: 0;
|
|
3
|
-
readonly Error: 1;
|
|
4
|
-
readonly Warning: 2;
|
|
5
|
-
readonly Notice: 3;
|
|
6
|
-
readonly Info: 3;
|
|
7
|
-
readonly Http: 4;
|
|
8
|
-
readonly Query: 4;
|
|
9
|
-
readonly Verbose: 5;
|
|
10
|
-
readonly Debug: 5;
|
|
11
|
-
};
|
|
12
|
-
declare type LoggingLevel = typeof LoggingLevel[keyof typeof LoggingLevel];
|
|
13
|
-
export default LoggingLevel;
|
package/types/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { default } from './classes/logger';
|