@cichol/logger 2.0.12 → 4.0.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/README.md +4 -4
- package/package.json +15 -33
- package/releases/cjs.js +573 -0
- package/releases/esm.js +526 -0
- package/{releases/cjs → types}/classes/logger.d.ts +0 -0
- package/{releases/cjs → types}/formatters/command-line.d.ts +0 -0
- package/{releases/cjs → types}/formatters/morgan.d.ts +0 -0
- package/{releases/esm → types}/handlers/debug.d.ts +2 -1
- package/{releases/cjs → types}/handlers/error.d.ts +0 -0
- package/{releases/cjs → types}/handlers/fatal.d.ts +0 -0
- package/{releases/cjs → types}/handlers/http.d.ts +2 -1
- package/{releases/cjs → types}/handlers/index.d.ts +0 -0
- package/{releases/esm → types}/handlers/info.d.ts +2 -1
- package/{releases/cjs → types}/handlers/notice.d.ts +2 -1
- package/{releases/cjs → types}/handlers/query.d.ts +0 -0
- package/{releases/cjs → types}/handlers/verbose.d.ts +2 -1
- package/{releases/cjs → types}/handlers/warning.d.ts +2 -1
- package/{releases/cjs → types}/helpers/color-scheme.d.ts +0 -0
- package/{releases/cjs → types}/helpers/constants.d.ts +0 -0
- package/{releases/cjs → types}/helpers/label-decorator.d.ts +0 -0
- package/{releases/cjs → types}/helpers/logging-level.d.ts +0 -0
- package/{releases/cjs → types}/index.d.ts +0 -0
- package/LICENSE +0 -21
- package/releases/cjs/classes/logger.js +0 -144
- package/releases/cjs/formatters/command-line.js +0 -60
- package/releases/cjs/formatters/morgan.js +0 -88
- package/releases/cjs/handlers/debug.d.ts +0 -4
- package/releases/cjs/handlers/debug.js +0 -29
- package/releases/cjs/handlers/error.js +0 -115
- package/releases/cjs/handlers/fatal.js +0 -36
- package/releases/cjs/handlers/http.js +0 -29
- package/releases/cjs/handlers/index.js +0 -41
- package/releases/cjs/handlers/info.d.ts +0 -4
- package/releases/cjs/handlers/info.js +0 -29
- package/releases/cjs/handlers/notice.js +0 -29
- package/releases/cjs/handlers/query.js +0 -41
- package/releases/cjs/handlers/verbose.js +0 -29
- package/releases/cjs/handlers/warning.js +0 -29
- package/releases/cjs/helpers/color-scheme.js +0 -80
- package/releases/cjs/helpers/constants.js +0 -43
- package/releases/cjs/helpers/label-decorator.js +0 -34
- package/releases/cjs/helpers/logging-level.js +0 -28
- package/releases/cjs/index.js +0 -19
- package/releases/esm/classes/logger.d.ts +0 -33
- package/releases/esm/classes/logger.js +0 -120
- package/releases/esm/formatters/command-line.d.ts +0 -2
- package/releases/esm/formatters/command-line.js +0 -39
- package/releases/esm/formatters/morgan.d.ts +0 -8
- package/releases/esm/formatters/morgan.js +0 -67
- package/releases/esm/handlers/debug.js +0 -10
- package/releases/esm/handlers/error.d.ts +0 -13
- package/releases/esm/handlers/error.js +0 -83
- package/releases/esm/handlers/fatal.d.ts +0 -4
- package/releases/esm/handlers/fatal.js +0 -10
- package/releases/esm/handlers/http.d.ts +0 -4
- package/releases/esm/handlers/http.js +0 -10
- package/releases/esm/handlers/index.d.ts +0 -12
- package/releases/esm/handlers/index.js +0 -13
- package/releases/esm/handlers/info.js +0 -10
- package/releases/esm/handlers/notice.d.ts +0 -4
- package/releases/esm/handlers/notice.js +0 -10
- package/releases/esm/handlers/query.d.ts +0 -13
- package/releases/esm/handlers/query.js +0 -21
- package/releases/esm/handlers/verbose.d.ts +0 -4
- package/releases/esm/handlers/verbose.js +0 -10
- package/releases/esm/handlers/warning.d.ts +0 -4
- package/releases/esm/handlers/warning.js +0 -10
- package/releases/esm/helpers/color-scheme.d.ts +0 -12
- package/releases/esm/helpers/color-scheme.js +0 -60
- package/releases/esm/helpers/constants.d.ts +0 -18
- package/releases/esm/helpers/constants.js +0 -22
- package/releases/esm/helpers/label-decorator.d.ts +0 -2
- package/releases/esm/helpers/label-decorator.js +0 -19
- package/releases/esm/helpers/logging-level.d.ts +0 -13
- package/releases/esm/helpers/logging-level.js +0 -18
- package/releases/esm/index.d.ts +0 -1
- package/releases/esm/index.js +0 -2
package/releases/esm.js
ADDED
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
import _Symbol from '@babel/runtime-corejs3/core-js/symbol';
|
|
2
|
+
import _filterInstanceProperty from '@babel/runtime-corejs3/core-js/instance/filter';
|
|
3
|
+
import _mapInstanceProperty from '@babel/runtime-corejs3/core-js/instance/map';
|
|
4
|
+
import _trimInstanceProperty from '@babel/runtime-corejs3/core-js/instance/trim';
|
|
5
|
+
import _Reflect$hasOwnMetadata from '@babel/runtime-corejs3/core-js/reflect/has-own-metadata';
|
|
6
|
+
import _Reflect$getOwnMetadata from '@babel/runtime-corejs3/core-js/reflect/get-own-metadata';
|
|
7
|
+
import _Reflect$defineMetadata from '@babel/runtime-corejs3/core-js/reflect/define-metadata';
|
|
8
|
+
import 'reflect-metadata';
|
|
9
|
+
import { STATUS_CODES, IncomingMessage, ServerResponse } from 'http';
|
|
10
|
+
import { PassThrough } from 'stream';
|
|
11
|
+
import Promise$1 from 'bluebird';
|
|
12
|
+
import lodash from 'lodash';
|
|
13
|
+
import morgan from 'morgan';
|
|
14
|
+
import * as winston from 'winston';
|
|
15
|
+
import { format } from 'winston';
|
|
16
|
+
import _findInstanceProperty from '@babel/runtime-corejs3/core-js/instance/find';
|
|
17
|
+
import _Reflect$apply from '@babel/runtime-corejs3/core-js/reflect/apply';
|
|
18
|
+
import chalk from 'chalk';
|
|
19
|
+
import rTracer from 'cls-rtracer';
|
|
20
|
+
import { DateTime } from 'luxon';
|
|
21
|
+
import _Map from '@babel/runtime-corejs3/core-js/map';
|
|
22
|
+
import process, { version, env } from 'process';
|
|
23
|
+
import _Symbol$for from '@babel/runtime-corejs3/core-js/symbol/for';
|
|
24
|
+
import _startsWithInstanceProperty from '@babel/runtime-corejs3/core-js/instance/starts-with';
|
|
25
|
+
import _sliceInstanceProperty from '@babel/runtime-corejs3/core-js/instance/slice';
|
|
26
|
+
import path from 'path';
|
|
27
|
+
import highlight from 'cli-highlight';
|
|
28
|
+
import prettyBytes from 'pretty-bytes';
|
|
29
|
+
import prettyMs from 'pretty-ms';
|
|
30
|
+
import cluster from 'cluster';
|
|
31
|
+
import 'worker_threads';
|
|
32
|
+
|
|
33
|
+
const LoggingLevel = {
|
|
34
|
+
// Critical levels. (It will crash the process)
|
|
35
|
+
Fatal: 0,
|
|
36
|
+
Error: 1,
|
|
37
|
+
// Warning levels. (It just report warning information, not crash the process)
|
|
38
|
+
Warning: 2,
|
|
39
|
+
// Regular level and aliases.
|
|
40
|
+
Notice: 3,
|
|
41
|
+
Info: 3,
|
|
42
|
+
// Maybe sensitive levels. (In general, does not useful)
|
|
43
|
+
Http: 4,
|
|
44
|
+
Query: 4,
|
|
45
|
+
// Loud levels.
|
|
46
|
+
Verbose: 5,
|
|
47
|
+
Debug: 5
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const colorizerMap = new _Map(); // Helpers.
|
|
51
|
+
|
|
52
|
+
function createScheme(options) {
|
|
53
|
+
const nextIndex = colorizerMap.size;
|
|
54
|
+
const colorizer = chalk.bgHex(options.background).hex(options.foreground);
|
|
55
|
+
colorizerMap.set(nextIndex, colorizer);
|
|
56
|
+
return nextIndex;
|
|
57
|
+
} // Color definitions.
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
const ColorValue = {
|
|
61
|
+
// Basic colors.
|
|
62
|
+
White: '#FFFFFF',
|
|
63
|
+
Gray: '#808080',
|
|
64
|
+
Black: '#000000',
|
|
65
|
+
// Specialized colors.
|
|
66
|
+
Critical: '#D63031',
|
|
67
|
+
Warning: '#FDCB6E',
|
|
68
|
+
Regular: '#0984E3',
|
|
69
|
+
Http: '#6AB04C',
|
|
70
|
+
Query: '#399AF3'
|
|
71
|
+
};
|
|
72
|
+
const ColorScheme = {
|
|
73
|
+
Critical: createScheme({
|
|
74
|
+
background: ColorValue.Critical,
|
|
75
|
+
foreground: ColorValue.Black
|
|
76
|
+
}),
|
|
77
|
+
Warning: createScheme({
|
|
78
|
+
background: ColorValue.Warning,
|
|
79
|
+
foreground: ColorValue.Black
|
|
80
|
+
}),
|
|
81
|
+
Regular: createScheme({
|
|
82
|
+
background: ColorValue.Regular,
|
|
83
|
+
foreground: ColorValue.White
|
|
84
|
+
}),
|
|
85
|
+
Http: createScheme({
|
|
86
|
+
background: ColorValue.Http,
|
|
87
|
+
foreground: ColorValue.Black
|
|
88
|
+
}),
|
|
89
|
+
Query: createScheme({
|
|
90
|
+
background: ColorValue.Query,
|
|
91
|
+
foreground: ColorValue.Black
|
|
92
|
+
}),
|
|
93
|
+
Loud: createScheme({
|
|
94
|
+
background: ColorValue.Black,
|
|
95
|
+
foreground: ColorValue.Gray
|
|
96
|
+
})
|
|
97
|
+
};
|
|
98
|
+
function getChalkOf(colorSet) {
|
|
99
|
+
return colorizerMap.get(colorSet) ?? chalk;
|
|
100
|
+
}
|
|
101
|
+
/* eslint-enable no-use-before-define */
|
|
102
|
+
|
|
103
|
+
// Node.js built-in APIs.
|
|
104
|
+
|
|
105
|
+
const {
|
|
106
|
+
isTTY
|
|
107
|
+
} = process.stdout;
|
|
108
|
+
const isNotOnCLI = !isTTY;
|
|
109
|
+
function decorateLabel(label, scheme) {
|
|
110
|
+
if (isNotOnCLI) {
|
|
111
|
+
return label;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const chalk = getChalkOf(scheme);
|
|
115
|
+
const decoratedLabel = chalk.bold(` ${label} `);
|
|
116
|
+
return decoratedLabel;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Constants.
|
|
120
|
+
const rootPath = process.cwd();
|
|
121
|
+
const conjunction = ` ${chalk.blue('──')} `;
|
|
122
|
+
|
|
123
|
+
const splat = _Symbol$for('splat'); // Handler metadata.
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
const name$8 = 'error';
|
|
127
|
+
const level$8 = LoggingLevel.Error;
|
|
128
|
+
const label$8 = decorateLabel('ERROR', ColorScheme.Critical);
|
|
129
|
+
|
|
130
|
+
function mapLocationOfErrorThrown(filename) {
|
|
131
|
+
let target = path.relative(rootPath, filename);
|
|
132
|
+
|
|
133
|
+
if (_startsWithInstanceProperty(target).call(target, 'node_modules')) {
|
|
134
|
+
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
|
+
);
|
|
136
|
+
} else {
|
|
137
|
+
target = `${chalk.gray(path.dirname(target) + path.sep)}${chalk.bold.underline(path.basename(target))}`;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return target;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function handle$1(data) {
|
|
144
|
+
var _context, _context2, _context3;
|
|
145
|
+
|
|
146
|
+
const {
|
|
147
|
+
[splat]: [error] = []
|
|
148
|
+
} = data;
|
|
149
|
+
|
|
150
|
+
if (!(error instanceof Error)) {
|
|
151
|
+
return undefined;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const {
|
|
155
|
+
name,
|
|
156
|
+
message,
|
|
157
|
+
stack: rawStack = ''
|
|
158
|
+
} = error;
|
|
159
|
+
|
|
160
|
+
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
|
+
let location;
|
|
162
|
+
|
|
163
|
+
if (rawLocation === '<anonymous>') {
|
|
164
|
+
location = chalk.gray(rawLocation);
|
|
165
|
+
} else {
|
|
166
|
+
const [filename, line, column] = rawLocation.split(':');
|
|
167
|
+
|
|
168
|
+
if (path.isAbsolute(filename)) {
|
|
169
|
+
const target = mapLocationOfErrorThrown(path.relative(rootPath, filename));
|
|
170
|
+
const trailing = chalk.gray(`:${line}:${column}`);
|
|
171
|
+
location = target + trailing;
|
|
172
|
+
} else {
|
|
173
|
+
location = chalk.gray(`https://github.com/${chalk.bold.underline('nodejs')}/node/blob/${version}/lib/${filename}#L${line}`); // eslint-disable-line max-len
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return `${contextName}${conjunction}${location}`;
|
|
178
|
+
})).join('\n');
|
|
179
|
+
|
|
180
|
+
const label = decorateLabel(name, ColorScheme.Critical);
|
|
181
|
+
const payload = `${message}\n\n${stack}\n`;
|
|
182
|
+
return {
|
|
183
|
+
label,
|
|
184
|
+
payload
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
var ErrorHandler = /*#__PURE__*/Object.freeze({
|
|
189
|
+
__proto__: null,
|
|
190
|
+
name: name$8,
|
|
191
|
+
level: level$8,
|
|
192
|
+
label: label$8,
|
|
193
|
+
'default': handle$1
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
// Local helpers.
|
|
197
|
+
|
|
198
|
+
const name$7 = 'fatal';
|
|
199
|
+
const level$7 = LoggingLevel.Fatal;
|
|
200
|
+
const label$7 = decorateLabel('FATAL', ColorScheme.Critical);
|
|
201
|
+
|
|
202
|
+
var FatalHandler = /*#__PURE__*/Object.freeze({
|
|
203
|
+
__proto__: null,
|
|
204
|
+
name: name$7,
|
|
205
|
+
level: level$7,
|
|
206
|
+
label: label$7,
|
|
207
|
+
'default': handle$1
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
// Local helpers.
|
|
211
|
+
|
|
212
|
+
const name$6 = 'warning';
|
|
213
|
+
const level$6 = LoggingLevel.Warning;
|
|
214
|
+
const label$6 = decorateLabel('WARNING', ColorScheme.Warning);
|
|
215
|
+
var warning = null;
|
|
216
|
+
|
|
217
|
+
var WarningHandler = /*#__PURE__*/Object.freeze({
|
|
218
|
+
__proto__: null,
|
|
219
|
+
name: name$6,
|
|
220
|
+
level: level$6,
|
|
221
|
+
label: label$6,
|
|
222
|
+
'default': warning
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
// Local helpers.
|
|
226
|
+
|
|
227
|
+
const name$5 = 'notice';
|
|
228
|
+
const level$5 = LoggingLevel.Notice;
|
|
229
|
+
const label$5 = decorateLabel('NOTICE', ColorScheme.Regular);
|
|
230
|
+
var notice = null;
|
|
231
|
+
|
|
232
|
+
var NoticeHandler = /*#__PURE__*/Object.freeze({
|
|
233
|
+
__proto__: null,
|
|
234
|
+
name: name$5,
|
|
235
|
+
level: level$5,
|
|
236
|
+
label: label$5,
|
|
237
|
+
'default': notice
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
// Local helpers.
|
|
241
|
+
|
|
242
|
+
const name$4 = 'info';
|
|
243
|
+
const level$4 = LoggingLevel.Info;
|
|
244
|
+
const label$4 = decorateLabel('INFO', ColorScheme.Regular);
|
|
245
|
+
var info = null;
|
|
246
|
+
|
|
247
|
+
var InfoHandler = /*#__PURE__*/Object.freeze({
|
|
248
|
+
__proto__: null,
|
|
249
|
+
name: name$4,
|
|
250
|
+
level: level$4,
|
|
251
|
+
label: label$4,
|
|
252
|
+
'default': info
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
// Local helpers.
|
|
256
|
+
|
|
257
|
+
const name$3 = 'http';
|
|
258
|
+
const level$3 = LoggingLevel.Http;
|
|
259
|
+
const label$3 = decorateLabel('HTTP', ColorScheme.Http);
|
|
260
|
+
var http = null;
|
|
261
|
+
|
|
262
|
+
var HttpHandler = /*#__PURE__*/Object.freeze({
|
|
263
|
+
__proto__: null,
|
|
264
|
+
name: name$3,
|
|
265
|
+
level: level$3,
|
|
266
|
+
label: label$3,
|
|
267
|
+
'default': http
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
// Highlight.js
|
|
271
|
+
|
|
272
|
+
// Handler metadata.
|
|
273
|
+
const name$2 = 'query';
|
|
274
|
+
const level$2 = LoggingLevel.Query;
|
|
275
|
+
const label$2 = decorateLabel('QUERY', ColorScheme.Query);
|
|
276
|
+
function handle(data) {
|
|
277
|
+
const payload = highlight(data.message, {
|
|
278
|
+
language: 'sql'
|
|
279
|
+
});
|
|
280
|
+
return {
|
|
281
|
+
label: label$2,
|
|
282
|
+
payload
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
var QueryHandler = /*#__PURE__*/Object.freeze({
|
|
287
|
+
__proto__: null,
|
|
288
|
+
name: name$2,
|
|
289
|
+
level: level$2,
|
|
290
|
+
label: label$2,
|
|
291
|
+
'default': handle
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
// Local helpers.
|
|
295
|
+
|
|
296
|
+
const name$1 = 'verbose';
|
|
297
|
+
const level$1 = LoggingLevel.Verbose;
|
|
298
|
+
const label$1 = decorateLabel('VERBOSE', ColorScheme.Loud);
|
|
299
|
+
var verbose = null;
|
|
300
|
+
|
|
301
|
+
var VerboseHandler = /*#__PURE__*/Object.freeze({
|
|
302
|
+
__proto__: null,
|
|
303
|
+
name: name$1,
|
|
304
|
+
level: level$1,
|
|
305
|
+
label: label$1,
|
|
306
|
+
'default': verbose
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
// Local helpers.
|
|
310
|
+
|
|
311
|
+
const name = 'debug';
|
|
312
|
+
const level = LoggingLevel.Debug;
|
|
313
|
+
const label = decorateLabel('DEBUG', ColorScheme.Loud);
|
|
314
|
+
var debug = null;
|
|
315
|
+
|
|
316
|
+
var DebugHandler = /*#__PURE__*/Object.freeze({
|
|
317
|
+
__proto__: null,
|
|
318
|
+
name: name,
|
|
319
|
+
level: level,
|
|
320
|
+
label: label,
|
|
321
|
+
'default': debug
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
// Handler implementations.
|
|
325
|
+
const handlers = [FatalHandler, ErrorHandler, WarningHandler, NoticeHandler, InfoHandler, HttpHandler, QueryHandler, VerboseHandler, DebugHandler];
|
|
326
|
+
|
|
327
|
+
var formatForCLI = format.printf(data => {
|
|
328
|
+
const timestamp = chalk.gray(DateTime.now().toISO({
|
|
329
|
+
includeOffset: true
|
|
330
|
+
}));
|
|
331
|
+
|
|
332
|
+
const handler = _findInstanceProperty(handlers).call(handlers, ({
|
|
333
|
+
name
|
|
334
|
+
}) => name === data.level);
|
|
335
|
+
|
|
336
|
+
const fallback = {
|
|
337
|
+
label: handler?.label,
|
|
338
|
+
payload: data.message
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
const performHandle = handler?.default ?? (() => fallback); // Execute handler and then, get decorated payload.
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
const {
|
|
345
|
+
label,
|
|
346
|
+
payload
|
|
347
|
+
} = _Reflect$apply(performHandle, undefined, [data]) ?? fallback;
|
|
348
|
+
let extraFingerprint;
|
|
349
|
+
|
|
350
|
+
if (handler?.level === LoggingLevel.Http) {
|
|
351
|
+
extraFingerprint = data.transactionId ? `(Transaction-ID = ${data.transactionId})` : `(Request-ID = ${rTracer.id()})`;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
return `${label} ${timestamp}${extraFingerprint ? ` ${extraFingerprint}` : ''}\n${payload}`;
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
// Node.js built-in APIs.
|
|
358
|
+
|
|
359
|
+
const ExecutionMode = {
|
|
360
|
+
DevelopmentMode: 'development',
|
|
361
|
+
ProductionMode: 'production'
|
|
362
|
+
};
|
|
363
|
+
const ClusteringMode = {
|
|
364
|
+
MasterProcess: 0,
|
|
365
|
+
WorkerProcess: 1
|
|
366
|
+
};
|
|
367
|
+
// Exportings.
|
|
368
|
+
const executionMode = env.NODE_ENV ?? ExecutionMode.DevelopmentMode;
|
|
369
|
+
cluster.isPrimary ?? cluster.isMaster ? ClusteringMode.MasterProcess : ClusteringMode.WorkerProcess;
|
|
370
|
+
|
|
371
|
+
// Node.js built-in APIs.
|
|
372
|
+
|
|
373
|
+
// Helpers.
|
|
374
|
+
function decorateStatusCode(statusCode) {
|
|
375
|
+
const groupOfCode = Math.floor(Number(statusCode) / 100) * 100;
|
|
376
|
+
const baseMessage = `${statusCode} ${STATUS_CODES[statusCode]}`;
|
|
377
|
+
|
|
378
|
+
switch (groupOfCode) {
|
|
379
|
+
case 200:
|
|
380
|
+
{
|
|
381
|
+
return chalk.green(baseMessage);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
case 300:
|
|
385
|
+
{
|
|
386
|
+
return chalk.magenta(baseMessage);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
case 400:
|
|
390
|
+
case 500:
|
|
391
|
+
{
|
|
392
|
+
return chalk.red(baseMessage);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
default:
|
|
396
|
+
{
|
|
397
|
+
return chalk.gray(baseMessage);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function formatForMorgan(tokens, request, response) {
|
|
403
|
+
function _(token, ...subArgs) {
|
|
404
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
405
|
+
return tokens[token](request, response, ...subArgs);
|
|
406
|
+
} // Raw values.
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
const method = _('method');
|
|
410
|
+
|
|
411
|
+
const URL = _('url');
|
|
412
|
+
|
|
413
|
+
const httpVersion = `HTTP/${_('http-version')}`;
|
|
414
|
+
const statusCode = Number(_('status'));
|
|
415
|
+
const contentLength = Number(_('res', 'content-length') ?? -1);
|
|
416
|
+
const responseTime = Number(_('response-time') ?? -1); // Decorated values.
|
|
417
|
+
|
|
418
|
+
const $method = chalk.bold(method);
|
|
419
|
+
const $version = chalk.rgb(98, 177, 255)(httpVersion);
|
|
420
|
+
const $statusCode = decorateStatusCode(statusCode);
|
|
421
|
+
const $contentLength = contentLength >= 0 ? prettyBytes(contentLength) : '? B';
|
|
422
|
+
const $responseTime = responseTime >= 0 ? prettyMs(responseTime) : ''; // Additional information.
|
|
423
|
+
|
|
424
|
+
const metadata = `(${$contentLength}, ${$responseTime})`;
|
|
425
|
+
return [`→ ${$method} ${URL} ${$version}`, `← ${$version} ${$statusCode} ${metadata}`].join('\n');
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
var formatForMorgan$1 = executionMode === ExecutionMode.DevelopmentMode ? formatForMorgan : morgan.combined;
|
|
429
|
+
|
|
430
|
+
var _context, _context2;
|
|
431
|
+
|
|
432
|
+
// Transports.
|
|
433
|
+
const {
|
|
434
|
+
Console: ConsoleTransport
|
|
435
|
+
} = winston.transports; // Keys in order to access private properties.
|
|
436
|
+
|
|
437
|
+
const KeyOfInstance = _Symbol('@cichol/logger::classes/logger::Instance');
|
|
438
|
+
|
|
439
|
+
const KeyOfPassport = _Symbol('@cichol/logger::classes/logger::Passport');
|
|
440
|
+
|
|
441
|
+
class Logger {
|
|
442
|
+
#logger = winston.createLogger({
|
|
443
|
+
levels: _filterInstanceProperty(_context = _mapInstanceProperty(_context2 = lodash(handlers)).call(_context2, ({
|
|
444
|
+
name,
|
|
445
|
+
level
|
|
446
|
+
}) => [name, level])).call(_context, ([, level]) => typeof level === 'number').fromPairs().value(),
|
|
447
|
+
level: executionMode === ExecutionMode.ProductionMode ? 'http' : 'verbose',
|
|
448
|
+
transports: [new ConsoleTransport({
|
|
449
|
+
format: formatForCLI
|
|
450
|
+
})],
|
|
451
|
+
exitOnError: false
|
|
452
|
+
});
|
|
453
|
+
#morgan = morgan(formatForMorgan$1, {
|
|
454
|
+
stream: new PassThrough({
|
|
455
|
+
objectMode: true
|
|
456
|
+
}).on('data', data => {
|
|
457
|
+
this.log('http', _trimInstanceProperty(data).call(data));
|
|
458
|
+
})
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
constructor(passport) {
|
|
462
|
+
if (passport !== KeyOfPassport) {
|
|
463
|
+
throw new ReferenceError('Logger class cannot be constructed in directly');
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
static get instance() {
|
|
468
|
+
if (this !== Logger) {
|
|
469
|
+
throw new ReferenceError('Logger class is not inheritable');
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if (_Reflect$hasOwnMetadata(KeyOfInstance, Logger)) {
|
|
473
|
+
return _Reflect$getOwnMetadata(KeyOfInstance, Logger);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
const instance = new Logger(KeyOfPassport);
|
|
477
|
+
|
|
478
|
+
try {
|
|
479
|
+
return instance;
|
|
480
|
+
} finally {
|
|
481
|
+
_Reflect$defineMetadata(KeyOfInstance, instance, Logger);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Logging message with specific level.
|
|
486
|
+
*
|
|
487
|
+
* @param level
|
|
488
|
+
* @param message
|
|
489
|
+
* @param args Additional metadata.
|
|
490
|
+
*/
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
log(...args) {
|
|
494
|
+
const logger = this.#logger;
|
|
495
|
+
const morgan = this.#morgan;
|
|
496
|
+
|
|
497
|
+
switch (true) {
|
|
498
|
+
case args.length >= 2 && args[0] instanceof IncomingMessage && args[1] instanceof ServerResponse:
|
|
499
|
+
{
|
|
500
|
+
const [request, response] = args;
|
|
501
|
+
return Promise$1.fromCallback(callback => morgan(request, response, callback));
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
case args.length >= 1 && args[0] instanceof Error:
|
|
505
|
+
{
|
|
506
|
+
const [error, ...data] = args; // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
507
|
+
|
|
508
|
+
logger.log('error', error.message, error, ...data);
|
|
509
|
+
return undefined;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
default:
|
|
513
|
+
{
|
|
514
|
+
const [level, message, ...data] = args; // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
515
|
+
|
|
516
|
+
logger.log(level, message, ...data);
|
|
517
|
+
return undefined;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
var logger = Logger.instance;
|
|
525
|
+
|
|
526
|
+
export { logger as default };
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2020 Yongbin Min and the other contributors.
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|