@contrast/agent 4.16.2 → 4.17.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.
Files changed (141) hide show
  1. package/esm.mjs +21 -11
  2. package/lib/assess/policy/propagators.json +4 -0
  3. package/lib/assess/policy/signatures.json +5 -0
  4. package/lib/assess/propagators/fastify-static/allowed-path.js +85 -0
  5. package/lib/contrast.js +1 -1
  6. package/lib/core/rewrite/index.js +2 -2
  7. package/node_modules/@colors/colors/LICENSE +26 -0
  8. package/node_modules/@colors/colors/README.md +219 -0
  9. package/node_modules/@colors/colors/examples/normal-usage.js +83 -0
  10. package/node_modules/@colors/colors/examples/safe-string.js +80 -0
  11. package/node_modules/@colors/colors/index.d.ts +136 -0
  12. package/node_modules/@colors/colors/lib/colors.js +211 -0
  13. package/node_modules/@colors/colors/lib/custom/trap.js +46 -0
  14. package/node_modules/@colors/colors/lib/custom/zalgo.js +110 -0
  15. package/node_modules/@colors/colors/lib/extendStringPrototype.js +110 -0
  16. package/node_modules/@colors/colors/lib/index.js +13 -0
  17. package/node_modules/@colors/colors/lib/maps/america.js +10 -0
  18. package/node_modules/@colors/colors/lib/maps/rainbow.js +12 -0
  19. package/node_modules/@colors/colors/lib/maps/random.js +11 -0
  20. package/node_modules/@colors/colors/lib/maps/zebra.js +5 -0
  21. package/node_modules/@colors/colors/lib/styles.js +95 -0
  22. package/node_modules/@colors/colors/lib/system/has-flag.js +35 -0
  23. package/node_modules/@colors/colors/lib/system/supports-colors.js +151 -0
  24. package/node_modules/@colors/colors/package.json +49 -0
  25. package/node_modules/@colors/colors/safe.d.ts +48 -0
  26. package/node_modules/@colors/colors/safe.js +10 -0
  27. package/node_modules/@colors/colors/themes/generic-logging.js +12 -0
  28. package/node_modules/@dabh/diagnostics/README.md +16 -16
  29. package/node_modules/@dabh/diagnostics/package.json +9 -9
  30. package/node_modules/color/README.md +9 -0
  31. package/node_modules/color/index.js +4 -1
  32. package/node_modules/color/package.json +8 -8
  33. package/node_modules/color-string/README.md +6 -2
  34. package/node_modules/color-string/index.js +21 -13
  35. package/node_modules/color-string/package.json +4 -4
  36. package/node_modules/colorspace/package.json +5 -5
  37. package/node_modules/fecha/dist/fecha.min.js +1 -1
  38. package/node_modules/fecha/dist/fecha.min.js.map +1 -0
  39. package/node_modules/fecha/lib/fecha.d.ts +2 -2
  40. package/node_modules/fecha/lib/fecha.js +35 -18
  41. package/node_modules/fecha/lib/fecha.js.map +1 -0
  42. package/node_modules/fecha/lib/fecha.umd.js +35 -18
  43. package/node_modules/fecha/lib/fecha.umd.js.map +1 -0
  44. package/node_modules/fecha/package.json +9 -10
  45. package/node_modules/fecha/src/fecha.ts +524 -0
  46. package/node_modules/logform/.eslintrc +1 -1
  47. package/node_modules/logform/CHANGELOG.md +24 -0
  48. package/node_modules/logform/README.md +15 -17
  49. package/node_modules/logform/browser.js +3 -1
  50. package/node_modules/logform/cli.js +1 -1
  51. package/node_modules/logform/colorize.js +2 -2
  52. package/node_modules/logform/dist/browser.js +8 -2
  53. package/node_modules/logform/dist/cli.js +2 -2
  54. package/node_modules/logform/dist/colorize.js +5 -5
  55. package/node_modules/logform/dist/errors.js +1 -1
  56. package/node_modules/logform/dist/format.js +9 -5
  57. package/node_modules/logform/dist/index.js +53 -20
  58. package/node_modules/logform/dist/json.js +7 -6
  59. package/node_modules/logform/dist/logstash.js +1 -1
  60. package/node_modules/logform/dist/pad-levels.js +4 -4
  61. package/node_modules/logform/dist/printf.js +1 -1
  62. package/node_modules/logform/dist/simple.js +1 -1
  63. package/node_modules/logform/dist/splat.js +2 -2
  64. package/node_modules/logform/dist/uncolorize.js +1 -1
  65. package/node_modules/logform/errors.js +1 -1
  66. package/node_modules/logform/index.d.ts +36 -0
  67. package/node_modules/logform/index.js +19 -20
  68. package/node_modules/logform/json.js +7 -7
  69. package/node_modules/logform/logstash.js +1 -1
  70. package/node_modules/logform/package.json +10 -9
  71. package/node_modules/logform/simple.js +1 -1
  72. package/node_modules/logform/uncolorize.js +1 -1
  73. package/node_modules/safe-stable-stringify/CHANGELOG.md +75 -0
  74. package/node_modules/safe-stable-stringify/LICENSE +21 -0
  75. package/node_modules/safe-stable-stringify/esm/package.json +4 -0
  76. package/node_modules/safe-stable-stringify/esm/wrapper.js +6 -0
  77. package/node_modules/safe-stable-stringify/index.d.ts +18 -0
  78. package/node_modules/safe-stable-stringify/index.js +618 -0
  79. package/node_modules/safe-stable-stringify/package.json +69 -0
  80. package/node_modules/safe-stable-stringify/readme.md +170 -0
  81. package/node_modules/safe-stable-stringify/tsconfig.json +22 -0
  82. package/node_modules/winston/LICENSE +18 -18
  83. package/node_modules/winston/README.md +1231 -1230
  84. package/node_modules/winston/dist/winston/common.js +10 -10
  85. package/node_modules/winston/dist/winston/config/index.js +17 -17
  86. package/node_modules/winston/dist/winston/container.js +46 -46
  87. package/node_modules/winston/dist/winston/create-logger.js +28 -24
  88. package/node_modules/winston/dist/winston/exception-handler.js +49 -49
  89. package/node_modules/winston/dist/winston/exception-stream.js +27 -27
  90. package/node_modules/winston/dist/winston/logger.js +152 -138
  91. package/node_modules/winston/dist/winston/profiler.js +22 -22
  92. package/node_modules/winston/dist/winston/rejection-handler.js +54 -54
  93. package/node_modules/winston/dist/winston/tail-file.js +14 -14
  94. package/node_modules/winston/dist/winston/transports/console.js +31 -31
  95. package/node_modules/winston/dist/winston/transports/file.js +101 -101
  96. package/node_modules/winston/dist/winston/transports/http.js +119 -41
  97. package/node_modules/winston/dist/winston/transports/index.js +17 -17
  98. package/node_modules/winston/dist/winston/transports/stream.js +24 -24
  99. package/node_modules/winston/dist/winston.js +91 -97
  100. package/node_modules/winston/index.d.ts +213 -193
  101. package/node_modules/winston/lib/winston/common.js +61 -61
  102. package/node_modules/winston/lib/winston/config/index.d.ts +99 -98
  103. package/node_modules/winston/lib/winston/config/index.js +35 -35
  104. package/node_modules/winston/lib/winston/container.js +114 -114
  105. package/node_modules/winston/lib/winston/create-logger.js +104 -104
  106. package/node_modules/winston/lib/winston/exception-handler.js +245 -245
  107. package/node_modules/winston/lib/winston/exception-stream.js +54 -54
  108. package/node_modules/winston/lib/winston/logger.js +676 -667
  109. package/node_modules/winston/lib/winston/profiler.js +51 -51
  110. package/node_modules/winston/lib/winston/rejection-handler.js +251 -251
  111. package/node_modules/winston/lib/winston/tail-file.js +124 -124
  112. package/node_modules/winston/lib/winston/transports/console.js +117 -117
  113. package/node_modules/winston/lib/winston/transports/file.js +695 -695
  114. package/node_modules/winston/lib/winston/transports/http.js +267 -202
  115. package/node_modules/winston/lib/winston/transports/index.d.ts +103 -100
  116. package/node_modules/winston/lib/winston/transports/index.js +56 -56
  117. package/node_modules/winston/lib/winston/transports/stream.js +63 -63
  118. package/node_modules/winston/lib/winston.js +176 -182
  119. package/node_modules/winston/node_modules/winston-transport/.babelrc +3 -0
  120. package/node_modules/winston/node_modules/winston-transport/.eslintrc +7 -0
  121. package/node_modules/winston/node_modules/winston-transport/.gitattributes +1 -0
  122. package/node_modules/winston/node_modules/winston-transport/.nyc_output/c3d7ddb9-cc26-466b-a4f6-993ad69e86f6.json +1 -0
  123. package/node_modules/winston/node_modules/winston-transport/.nyc_output/processinfo/c3d7ddb9-cc26-466b-a4f6-993ad69e86f6.json +1 -0
  124. package/node_modules/winston/node_modules/winston-transport/.nyc_output/processinfo/index.json +1 -0
  125. package/node_modules/winston/node_modules/winston-transport/CHANGELOG.md +126 -0
  126. package/node_modules/winston/node_modules/winston-transport/LICENSE +22 -0
  127. package/node_modules/winston/node_modules/winston-transport/README.md +50 -0
  128. package/node_modules/winston/node_modules/winston-transport/dist/index.js +215 -0
  129. package/node_modules/winston/node_modules/winston-transport/dist/legacy.js +116 -0
  130. package/node_modules/winston/node_modules/winston-transport/index.d.ts +39 -0
  131. package/node_modules/winston/node_modules/winston-transport/index.js +215 -0
  132. package/node_modules/winston/node_modules/winston-transport/legacy.js +119 -0
  133. package/node_modules/winston/node_modules/winston-transport/package.json +56 -0
  134. package/node_modules/winston/package.json +76 -75
  135. package/package.json +6 -4
  136. package/node_modules/@dabh/diagnostics/example.png +0 -0
  137. package/node_modules/color-string/CHANGELOG.md +0 -18
  138. package/node_modules/colorspace/test.js +0 -14
  139. package/node_modules/fecha/CHANGELOG.md +0 -67
  140. package/node_modules/logform/.travis.yml +0 -17
  141. package/node_modules/winston/CHANGELOG.md +0 -560
@@ -1,667 +1,676 @@
1
- /**
2
- * logger.js: TODO: add file header description.
3
- *
4
- * (C) 2010 Charlie Robbins
5
- * MIT LICENCE
6
- */
7
-
8
- 'use strict';
9
-
10
- const { Stream, Transform } = require('readable-stream');
11
- const asyncForEach = require('async/forEach');
12
- const { LEVEL, SPLAT } = require('triple-beam');
13
- const isStream = require('is-stream');
14
- const ExceptionHandler = require('./exception-handler');
15
- const RejectionHandler = require('./rejection-handler');
16
- const LegacyTransportStream = require('winston-transport/legacy');
17
- const Profiler = require('./profiler');
18
- const { warn } = require('./common');
19
- const config = require('./config');
20
-
21
- /**
22
- * Captures the number of format (i.e. %s strings) in a given string.
23
- * Based on `util.format`, see Node.js source:
24
- * https://github.com/nodejs/node/blob/b1c8f15c5f169e021f7c46eb7b219de95fe97603/lib/util.js#L201-L230
25
- * @type {RegExp}
26
- */
27
- const formatRegExp = /%[scdjifoO%]/g;
28
-
29
- /**
30
- * TODO: add class description.
31
- * @type {Logger}
32
- * @extends {Transform}
33
- */
34
- class Logger extends Transform {
35
- /**
36
- * Constructor function for the Logger object responsible for persisting log
37
- * messages and metadata to one or more transports.
38
- * @param {!Object} options - foo
39
- */
40
- constructor(options) {
41
- super({ objectMode: true });
42
- this.configure(options);
43
- }
44
-
45
- child(defaultRequestMetadata) {
46
- const logger = this;
47
- return Object.create(logger, {
48
- write: {
49
- value: function (info) {
50
- const infoClone = Object.assign(
51
- {},
52
- defaultRequestMetadata,
53
- info
54
- );
55
-
56
- // Object.assign doesn't copy inherited Error
57
- // properties so we have to do that explicitly
58
- //
59
- // Remark (indexzero): we should remove this
60
- // since the errors format will handle this case.
61
- //
62
- if (info instanceof Error) {
63
- infoClone.stack = info.stack;
64
- infoClone.message = info.message;
65
- }
66
-
67
- logger.write(infoClone);
68
- }
69
- }
70
- });
71
- }
72
-
73
- /**
74
- * This will wholesale reconfigure this instance by:
75
- * 1. Resetting all transports. Older transports will be removed implicitly.
76
- * 2. Set all other options including levels, colors, rewriters, filters,
77
- * exceptionHandlers, etc.
78
- * @param {!Object} options - TODO: add param description.
79
- * @returns {undefined}
80
- */
81
- configure({
82
- silent,
83
- format,
84
- defaultMeta,
85
- levels,
86
- level = 'info',
87
- exitOnError = true,
88
- transports,
89
- colors,
90
- emitErrs,
91
- formatters,
92
- padLevels,
93
- rewriters,
94
- stripColors,
95
- exceptionHandlers,
96
- rejectionHandlers
97
- } = {}) {
98
- // Reset transports if we already have them
99
- if (this.transports.length) {
100
- this.clear();
101
- }
102
-
103
- this.silent = silent;
104
- this.format = format || this.format || require('logform/json')();
105
-
106
- this.defaultMeta = defaultMeta || null;
107
- // Hoist other options onto this instance.
108
- this.levels = levels || this.levels || config.npm.levels;
109
- this.level = level;
110
- this.exceptions = new ExceptionHandler(this);
111
- this.rejections = new RejectionHandler(this);
112
- this.profilers = {};
113
- this.exitOnError = exitOnError;
114
-
115
- // Add all transports we have been provided.
116
- if (transports) {
117
- transports = Array.isArray(transports) ? transports : [transports];
118
- transports.forEach(transport => this.add(transport));
119
- }
120
-
121
- if (
122
- colors ||
123
- emitErrs ||
124
- formatters ||
125
- padLevels ||
126
- rewriters ||
127
- stripColors
128
- ) {
129
- throw new Error(
130
- [
131
- '{ colors, emitErrs, formatters, padLevels, rewriters, stripColors } were removed in winston@3.0.0.',
132
- 'Use a custom winston.format(function) instead.',
133
- 'See: https://github.com/winstonjs/winston/tree/master/UPGRADE-3.0.md'
134
- ].join('\n')
135
- );
136
- }
137
-
138
- if (exceptionHandlers) {
139
- this.exceptions.handle(exceptionHandlers);
140
- }
141
- if (rejectionHandlers) {
142
- this.rejections.handle(rejectionHandlers);
143
- }
144
- }
145
-
146
- isLevelEnabled(level) {
147
- const givenLevelValue = getLevelValue(this.levels, level);
148
- if (givenLevelValue === null) {
149
- return false;
150
- }
151
-
152
- const configuredLevelValue = getLevelValue(this.levels, this.level);
153
- if (configuredLevelValue === null) {
154
- return false;
155
- }
156
-
157
- if (!this.transports || this.transports.length === 0) {
158
- return configuredLevelValue >= givenLevelValue;
159
- }
160
-
161
- const index = this.transports.findIndex(transport => {
162
- let transportLevelValue = getLevelValue(this.levels, transport.level);
163
- if (transportLevelValue === null) {
164
- transportLevelValue = configuredLevelValue;
165
- }
166
- return transportLevelValue >= givenLevelValue;
167
- });
168
- return index !== -1;
169
- }
170
-
171
- /* eslint-disable valid-jsdoc */
172
- /**
173
- * Ensure backwards compatibility with a `log` method
174
- * @param {mixed} level - Level the log message is written at.
175
- * @param {mixed} msg - TODO: add param description.
176
- * @param {mixed} meta - TODO: add param description.
177
- * @returns {Logger} - TODO: add return description.
178
- *
179
- * @example
180
- * // Supports the existing API:
181
- * logger.log('info', 'Hello world', { custom: true });
182
- * logger.log('info', new Error('Yo, it\'s on fire'));
183
- *
184
- * // Requires winston.format.splat()
185
- * logger.log('info', '%s %d%%', 'A string', 50, { thisIsMeta: true });
186
- *
187
- * // And the new API with a single JSON literal:
188
- * logger.log({ level: 'info', message: 'Hello world', custom: true });
189
- * logger.log({ level: 'info', message: new Error('Yo, it\'s on fire') });
190
- *
191
- * // Also requires winston.format.splat()
192
- * logger.log({
193
- * level: 'info',
194
- * message: '%s %d%%',
195
- * [SPLAT]: ['A string', 50],
196
- * meta: { thisIsMeta: true }
197
- * });
198
- *
199
- */
200
- /* eslint-enable valid-jsdoc */
201
- log(level, msg, ...splat) {
202
- // eslint-disable-line max-params
203
- // Optimize for the hotpath of logging JSON literals
204
- if (arguments.length === 1) {
205
- // Yo dawg, I heard you like levels ... seriously ...
206
- // In this context the LHS `level` here is actually the `info` so read
207
- // this as: info[LEVEL] = info.level;
208
- level[LEVEL] = level.level;
209
- this._addDefaultMeta(level);
210
- this.write(level);
211
- return this;
212
- }
213
-
214
- // Slightly less hotpath, but worth optimizing for.
215
- if (arguments.length === 2) {
216
- if (msg && typeof msg === 'object') {
217
- msg[LEVEL] = msg.level = level;
218
- this._addDefaultMeta(msg);
219
- this.write(msg);
220
- return this;
221
- }
222
-
223
- this.write({ [LEVEL]: level, level, message: msg });
224
- return this;
225
- }
226
-
227
- const [meta] = splat;
228
- if (typeof meta === 'object' && meta !== null) {
229
- // Extract tokens, if none available default to empty array to
230
- // ensure consistancy in expected results
231
- const tokens = msg && msg.match && msg.match(formatRegExp);
232
-
233
- if (!tokens) {
234
- const info = Object.assign({}, this.defaultMeta, meta, {
235
- [LEVEL]: level,
236
- [SPLAT]: splat,
237
- level,
238
- message: msg
239
- });
240
-
241
- if (meta.message) info.message = `${info.message} ${meta.message}`;
242
- if (meta.stack) info.stack = meta.stack;
243
-
244
- this.write(info);
245
- return this;
246
- }
247
- }
248
-
249
- this.write(Object.assign({}, this.defaultMeta, {
250
- [LEVEL]: level,
251
- [SPLAT]: splat,
252
- level,
253
- message: msg
254
- }));
255
-
256
- return this;
257
- }
258
-
259
- /**
260
- * Pushes data so that it can be picked up by all of our pipe targets.
261
- * @param {mixed} info - TODO: add param description.
262
- * @param {mixed} enc - TODO: add param description.
263
- * @param {mixed} callback - Continues stream processing.
264
- * @returns {undefined}
265
- * @private
266
- */
267
- _transform(info, enc, callback) {
268
- if (this.silent) {
269
- return callback();
270
- }
271
-
272
- // [LEVEL] is only soft guaranteed to be set here since we are a proper
273
- // stream. It is likely that `info` came in through `.log(info)` or
274
- // `.info(info)`. If it is not defined, however, define it.
275
- // This LEVEL symbol is provided by `triple-beam` and also used in:
276
- // - logform
277
- // - winston-transport
278
- // - abstract-winston-transport
279
- if (!info[LEVEL]) {
280
- info[LEVEL] = info.level;
281
- }
282
-
283
- // Remark: really not sure what to do here, but this has been reported as
284
- // very confusing by pre winston@2.0.0 users as quite confusing when using
285
- // custom levels.
286
- if (!this.levels[info[LEVEL]] && this.levels[info[LEVEL]] !== 0) {
287
- // eslint-disable-next-line no-console
288
- console.error('[winston] Unknown logger level: %s', info[LEVEL]);
289
- }
290
-
291
- // Remark: not sure if we should simply error here.
292
- if (!this._readableState.pipes) {
293
- // eslint-disable-next-line no-console
294
- console.error(
295
- '[winston] Attempt to write logs with no transports %j',
296
- info
297
- );
298
- }
299
-
300
- // Here we write to the `format` pipe-chain, which on `readable` above will
301
- // push the formatted `info` Object onto the buffer for this instance. We trap
302
- // (and re-throw) any errors generated by the user-provided format, but also
303
- // guarantee that the streams callback is invoked so that we can continue flowing.
304
- try {
305
- this.push(this.format.transform(info, this.format.options));
306
- } catch (ex) {
307
- throw ex;
308
- } finally {
309
- // eslint-disable-next-line callback-return
310
- callback();
311
- }
312
- }
313
-
314
- /**
315
- * Delays the 'finish' event until all transport pipe targets have
316
- * also emitted 'finish' or are already finished.
317
- * @param {mixed} callback - Continues stream processing.
318
- */
319
- _final(callback) {
320
- const transports = this.transports.slice();
321
- asyncForEach(
322
- transports,
323
- (transport, next) => {
324
- if (!transport || transport.finished) return setImmediate(next);
325
- transport.once('finish', next);
326
- transport.end();
327
- },
328
- callback
329
- );
330
- }
331
-
332
- /**
333
- * Adds the transport to this logger instance by piping to it.
334
- * @param {mixed} transport - TODO: add param description.
335
- * @returns {Logger} - TODO: add return description.
336
- */
337
- add(transport) {
338
- // Support backwards compatibility with all existing `winston < 3.x.x`
339
- // transports which meet one of two criteria:
340
- // 1. They inherit from winston.Transport in < 3.x.x which is NOT a stream.
341
- // 2. They expose a log method which has a length greater than 2 (i.e. more then
342
- // just `log(info, callback)`.
343
- const target =
344
- !isStream(transport) || transport.log.length > 2
345
- ? new LegacyTransportStream({ transport })
346
- : transport;
347
-
348
- if (!target._writableState || !target._writableState.objectMode) {
349
- throw new Error(
350
- 'Transports must WritableStreams in objectMode. Set { objectMode: true }.'
351
- );
352
- }
353
-
354
- // Listen for the `error` event and the `warn` event on the new Transport.
355
- this._onEvent('error', target);
356
- this._onEvent('warn', target);
357
- this.pipe(target);
358
-
359
- if (transport.handleExceptions) {
360
- this.exceptions.handle();
361
- }
362
-
363
- if (transport.handleRejections) {
364
- this.rejections.handle();
365
- }
366
-
367
- return this;
368
- }
369
-
370
- /**
371
- * Removes the transport from this logger instance by unpiping from it.
372
- * @param {mixed} transport - TODO: add param description.
373
- * @returns {Logger} - TODO: add return description.
374
- */
375
- remove(transport) {
376
- if (!transport) return this;
377
- let target = transport;
378
- if (!isStream(transport) || transport.log.length > 2) {
379
- target = this.transports.filter(
380
- match => match.transport === transport
381
- )[0];
382
- }
383
-
384
- if (target) {
385
- this.unpipe(target);
386
- }
387
- return this;
388
- }
389
-
390
- /**
391
- * Removes all transports from this logger instance.
392
- * @returns {Logger} - TODO: add return description.
393
- */
394
- clear() {
395
- this.unpipe();
396
- return this;
397
- }
398
-
399
- /**
400
- * Cleans up resources (streams, event listeners) for all transports
401
- * associated with this instance (if necessary).
402
- * @returns {Logger} - TODO: add return description.
403
- */
404
- close() {
405
- this.clear();
406
- this.emit('close');
407
- return this;
408
- }
409
-
410
- /**
411
- * Sets the `target` levels specified on this instance.
412
- * @param {Object} Target levels to use on this instance.
413
- */
414
- setLevels() {
415
- warn.deprecated('setLevels');
416
- }
417
-
418
- /**
419
- * Queries the all transports for this instance with the specified `options`.
420
- * This will aggregate each transport's results into one object containing
421
- * a property per transport.
422
- * @param {Object} options - Query options for this instance.
423
- * @param {function} callback - Continuation to respond to when complete.
424
- */
425
- query(options, callback) {
426
- if (typeof options === 'function') {
427
- callback = options;
428
- options = {};
429
- }
430
-
431
- options = options || {};
432
- const results = {};
433
- const queryObject = Object.assign({}, options.query || {});
434
-
435
- // Helper function to query a single transport
436
- function queryTransport(transport, next) {
437
- if (options.query && typeof transport.formatQuery === 'function') {
438
- options.query = transport.formatQuery(queryObject);
439
- }
440
-
441
- transport.query(options, (err, res) => {
442
- if (err) {
443
- return next(err);
444
- }
445
-
446
- if (typeof transport.formatResults === 'function') {
447
- res = transport.formatResults(res, options.format);
448
- }
449
-
450
- next(null, res);
451
- });
452
- }
453
-
454
- // Helper function to accumulate the results from `queryTransport` into
455
- // the `results`.
456
- function addResults(transport, next) {
457
- queryTransport(transport, (err, result) => {
458
- // queryTransport could potentially invoke the callback multiple times
459
- // since Transport code can be unpredictable.
460
- if (next) {
461
- result = err || result;
462
- if (result) {
463
- results[transport.name] = result;
464
- }
465
-
466
- // eslint-disable-next-line callback-return
467
- next();
468
- }
469
-
470
- next = null;
471
- });
472
- }
473
-
474
- // Iterate over the transports in parallel setting the appropriate key in
475
- // the `results`.
476
- asyncForEach(
477
- this.transports.filter(transport => !!transport.query),
478
- addResults,
479
- () => callback(null, results)
480
- );
481
- }
482
-
483
- /**
484
- * Returns a log stream for all transports. Options object is optional.
485
- * @param{Object} options={} - Stream options for this instance.
486
- * @returns {Stream} - TODO: add return description.
487
- */
488
- stream(options = {}) {
489
- const out = new Stream();
490
- const streams = [];
491
-
492
- out._streams = streams;
493
- out.destroy = () => {
494
- let i = streams.length;
495
- while (i--) {
496
- streams[i].destroy();
497
- }
498
- };
499
-
500
- // Create a list of all transports for this instance.
501
- this.transports
502
- .filter(transport => !!transport.stream)
503
- .forEach(transport => {
504
- const str = transport.stream(options);
505
- if (!str) {
506
- return;
507
- }
508
-
509
- streams.push(str);
510
-
511
- str.on('log', log => {
512
- log.transport = log.transport || [];
513
- log.transport.push(transport.name);
514
- out.emit('log', log);
515
- });
516
-
517
- str.on('error', err => {
518
- err.transport = err.transport || [];
519
- err.transport.push(transport.name);
520
- out.emit('error', err);
521
- });
522
- });
523
-
524
- return out;
525
- }
526
-
527
- /**
528
- * Returns an object corresponding to a specific timing. When done is called
529
- * the timer will finish and log the duration. e.g.:
530
- * @returns {Profile} - TODO: add return description.
531
- * @example
532
- * const timer = winston.startTimer()
533
- * setTimeout(() => {
534
- * timer.done({
535
- * message: 'Logging message'
536
- * });
537
- * }, 1000);
538
- */
539
- startTimer() {
540
- return new Profiler(this);
541
- }
542
-
543
- /**
544
- * Tracks the time inbetween subsequent calls to this method with the same
545
- * `id` parameter. The second call to this method will log the difference in
546
- * milliseconds along with the message.
547
- * @param {string} id Unique id of the profiler
548
- * @returns {Logger} - TODO: add return description.
549
- */
550
- profile(id, ...args) {
551
- const time = Date.now();
552
- if (this.profilers[id]) {
553
- const timeEnd = this.profilers[id];
554
- delete this.profilers[id];
555
-
556
- // Attempt to be kind to users if they are still using older APIs.
557
- if (typeof args[args.length - 2] === 'function') {
558
- // eslint-disable-next-line no-console
559
- console.warn(
560
- 'Callback function no longer supported as of winston@3.0.0'
561
- );
562
- args.pop();
563
- }
564
-
565
- // Set the duration property of the metadata
566
- const info = typeof args[args.length - 1] === 'object' ? args.pop() : {};
567
- info.level = info.level || 'info';
568
- info.durationMs = time - timeEnd;
569
- info.message = info.message || id;
570
- return this.write(info);
571
- }
572
-
573
- this.profilers[id] = time;
574
- return this;
575
- }
576
-
577
- /**
578
- * Backwards compatibility to `exceptions.handle` in winston < 3.0.0.
579
- * @returns {undefined}
580
- * @deprecated
581
- */
582
- handleExceptions(...args) {
583
- // eslint-disable-next-line no-console
584
- console.warn(
585
- 'Deprecated: .handleExceptions() will be removed in winston@4. Use .exceptions.handle()'
586
- );
587
- this.exceptions.handle(...args);
588
- }
589
-
590
- /**
591
- * Backwards compatibility to `exceptions.handle` in winston < 3.0.0.
592
- * @returns {undefined}
593
- * @deprecated
594
- */
595
- unhandleExceptions(...args) {
596
- // eslint-disable-next-line no-console
597
- console.warn(
598
- 'Deprecated: .unhandleExceptions() will be removed in winston@4. Use .exceptions.unhandle()'
599
- );
600
- this.exceptions.unhandle(...args);
601
- }
602
-
603
- /**
604
- * Throw a more meaningful deprecation notice
605
- * @throws {Error} - TODO: add throws description.
606
- */
607
- cli() {
608
- throw new Error(
609
- [
610
- 'Logger.cli() was removed in winston@3.0.0',
611
- 'Use a custom winston.formats.cli() instead.',
612
- 'See: https://github.com/winstonjs/winston/tree/master/UPGRADE-3.0.md'
613
- ].join('\n')
614
- );
615
- }
616
-
617
- /**
618
- * Bubbles the `event` that occured on the specified `transport` up
619
- * from this instance.
620
- * @param {string} event - The event that occured
621
- * @param {Object} transport - Transport on which the event occured
622
- * @private
623
- */
624
- _onEvent(event, transport) {
625
- function transportEvent(err) {
626
- // https://github.com/winstonjs/winston/issues/1364
627
- if (event === 'error' && !this.transports.includes(transport)) {
628
- this.add(transport);
629
- }
630
- this.emit(event, err, transport);
631
- }
632
-
633
- if (!transport['__winston' + event]) {
634
- transport['__winston' + event] = transportEvent.bind(this);
635
- transport.on(event, transport['__winston' + event]);
636
- }
637
- }
638
-
639
- _addDefaultMeta(msg) {
640
- if (this.defaultMeta) {
641
- Object.assign(msg, this.defaultMeta);
642
- }
643
- }
644
- }
645
-
646
- function getLevelValue(levels, level) {
647
- const value = levels[level];
648
- if (!value && value !== 0) {
649
- return null;
650
- }
651
- return value;
652
- }
653
-
654
- /**
655
- * Represents the current readableState pipe targets for this Logger instance.
656
- * @type {Array|Object}
657
- */
658
- Object.defineProperty(Logger.prototype, 'transports', {
659
- configurable: false,
660
- enumerable: true,
661
- get() {
662
- const { pipes } = this._readableState;
663
- return !Array.isArray(pipes) ? [pipes].filter(Boolean) : pipes;
664
- }
665
- });
666
-
667
- module.exports = Logger;
1
+ /**
2
+ * logger.js: TODO: add file header description.
3
+ *
4
+ * (C) 2010 Charlie Robbins
5
+ * MIT LICENCE
6
+ */
7
+
8
+ 'use strict';
9
+
10
+ const { Stream, Transform } = require('readable-stream');
11
+ const asyncForEach = require('async/forEach');
12
+ const { LEVEL, SPLAT } = require('triple-beam');
13
+ const isStream = require('is-stream');
14
+ const ExceptionHandler = require('./exception-handler');
15
+ const RejectionHandler = require('./rejection-handler');
16
+ const LegacyTransportStream = require('winston-transport/legacy');
17
+ const Profiler = require('./profiler');
18
+ const { warn } = require('./common');
19
+ const config = require('./config');
20
+
21
+ /**
22
+ * Captures the number of format (i.e. %s strings) in a given string.
23
+ * Based on `util.format`, see Node.js source:
24
+ * https://github.com/nodejs/node/blob/b1c8f15c5f169e021f7c46eb7b219de95fe97603/lib/util.js#L201-L230
25
+ * @type {RegExp}
26
+ */
27
+ const formatRegExp = /%[scdjifoO%]/g;
28
+
29
+ /**
30
+ * TODO: add class description.
31
+ * @type {Logger}
32
+ * @extends {Transform}
33
+ */
34
+ class Logger extends Transform {
35
+ /**
36
+ * Constructor function for the Logger object responsible for persisting log
37
+ * messages and metadata to one or more transports.
38
+ * @param {!Object} options - foo
39
+ */
40
+ constructor(options) {
41
+ super({ objectMode: true });
42
+ this.configure(options);
43
+ }
44
+
45
+ child(defaultRequestMetadata) {
46
+ const logger = this;
47
+ return Object.create(logger, {
48
+ write: {
49
+ value: function (info) {
50
+ const infoClone = Object.assign(
51
+ {},
52
+ defaultRequestMetadata,
53
+ info
54
+ );
55
+
56
+ // Object.assign doesn't copy inherited Error
57
+ // properties so we have to do that explicitly
58
+ //
59
+ // Remark (indexzero): we should remove this
60
+ // since the errors format will handle this case.
61
+ //
62
+ if (info instanceof Error) {
63
+ infoClone.stack = info.stack;
64
+ infoClone.message = info.message;
65
+ }
66
+
67
+ logger.write(infoClone);
68
+ }
69
+ }
70
+ });
71
+ }
72
+
73
+ /**
74
+ * This will wholesale reconfigure this instance by:
75
+ * 1. Resetting all transports. Older transports will be removed implicitly.
76
+ * 2. Set all other options including levels, colors, rewriters, filters,
77
+ * exceptionHandlers, etc.
78
+ * @param {!Object} options - TODO: add param description.
79
+ * @returns {undefined}
80
+ */
81
+ configure({
82
+ silent,
83
+ format,
84
+ defaultMeta,
85
+ levels,
86
+ level = 'info',
87
+ exitOnError = true,
88
+ transports,
89
+ colors,
90
+ emitErrs,
91
+ formatters,
92
+ padLevels,
93
+ rewriters,
94
+ stripColors,
95
+ exceptionHandlers,
96
+ rejectionHandlers
97
+ } = {}) {
98
+ // Reset transports if we already have them
99
+ if (this.transports.length) {
100
+ this.clear();
101
+ }
102
+
103
+ this.silent = silent;
104
+ this.format = format || this.format || require('logform/json')();
105
+
106
+ this.defaultMeta = defaultMeta || null;
107
+ // Hoist other options onto this instance.
108
+ this.levels = levels || this.levels || config.npm.levels;
109
+ this.level = level;
110
+ if (this.exceptions) {
111
+ this.exceptions.unhandle();
112
+ }
113
+ if (this.rejections) {
114
+ this.rejections.unhandle();
115
+ }
116
+ this.exceptions = new ExceptionHandler(this);
117
+ this.rejections = new RejectionHandler(this);
118
+ this.profilers = {};
119
+ this.exitOnError = exitOnError;
120
+
121
+ // Add all transports we have been provided.
122
+ if (transports) {
123
+ transports = Array.isArray(transports) ? transports : [transports];
124
+ transports.forEach(transport => this.add(transport));
125
+ }
126
+
127
+ if (
128
+ colors ||
129
+ emitErrs ||
130
+ formatters ||
131
+ padLevels ||
132
+ rewriters ||
133
+ stripColors
134
+ ) {
135
+ throw new Error(
136
+ [
137
+ '{ colors, emitErrs, formatters, padLevels, rewriters, stripColors } were removed in winston@3.0.0.',
138
+ 'Use a custom winston.format(function) instead.',
139
+ 'See: https://github.com/winstonjs/winston/tree/master/UPGRADE-3.0.md'
140
+ ].join('\n')
141
+ );
142
+ }
143
+
144
+ if (exceptionHandlers) {
145
+ this.exceptions.handle(exceptionHandlers);
146
+ }
147
+ if (rejectionHandlers) {
148
+ this.rejections.handle(rejectionHandlers);
149
+ }
150
+ }
151
+
152
+ isLevelEnabled(level) {
153
+ const givenLevelValue = getLevelValue(this.levels, level);
154
+ if (givenLevelValue === null) {
155
+ return false;
156
+ }
157
+
158
+ const configuredLevelValue = getLevelValue(this.levels, this.level);
159
+ if (configuredLevelValue === null) {
160
+ return false;
161
+ }
162
+
163
+ if (!this.transports || this.transports.length === 0) {
164
+ return configuredLevelValue >= givenLevelValue;
165
+ }
166
+
167
+ const index = this.transports.findIndex(transport => {
168
+ let transportLevelValue = getLevelValue(this.levels, transport.level);
169
+ if (transportLevelValue === null) {
170
+ transportLevelValue = configuredLevelValue;
171
+ }
172
+ return transportLevelValue >= givenLevelValue;
173
+ });
174
+ return index !== -1;
175
+ }
176
+
177
+ /* eslint-disable valid-jsdoc */
178
+ /**
179
+ * Ensure backwards compatibility with a `log` method
180
+ * @param {mixed} level - Level the log message is written at.
181
+ * @param {mixed} msg - TODO: add param description.
182
+ * @param {mixed} meta - TODO: add param description.
183
+ * @returns {Logger} - TODO: add return description.
184
+ *
185
+ * @example
186
+ * // Supports the existing API:
187
+ * logger.log('info', 'Hello world', { custom: true });
188
+ * logger.log('info', new Error('Yo, it\'s on fire'));
189
+ *
190
+ * // Requires winston.format.splat()
191
+ * logger.log('info', '%s %d%%', 'A string', 50, { thisIsMeta: true });
192
+ *
193
+ * // And the new API with a single JSON literal:
194
+ * logger.log({ level: 'info', message: 'Hello world', custom: true });
195
+ * logger.log({ level: 'info', message: new Error('Yo, it\'s on fire') });
196
+ *
197
+ * // Also requires winston.format.splat()
198
+ * logger.log({
199
+ * level: 'info',
200
+ * message: '%s %d%%',
201
+ * [SPLAT]: ['A string', 50],
202
+ * meta: { thisIsMeta: true }
203
+ * });
204
+ *
205
+ */
206
+ /* eslint-enable valid-jsdoc */
207
+ log(level, msg, ...splat) {
208
+ // eslint-disable-line max-params
209
+ // Optimize for the hotpath of logging JSON literals
210
+ if (arguments.length === 1) {
211
+ // Yo dawg, I heard you like levels ... seriously ...
212
+ // In this context the LHS `level` here is actually the `info` so read
213
+ // this as: info[LEVEL] = info.level;
214
+ level[LEVEL] = level.level;
215
+ this._addDefaultMeta(level);
216
+ this.write(level);
217
+ return this;
218
+ }
219
+
220
+ // Slightly less hotpath, but worth optimizing for.
221
+ if (arguments.length === 2) {
222
+ if (msg && typeof msg === 'object') {
223
+ msg[LEVEL] = msg.level = level;
224
+ this._addDefaultMeta(msg);
225
+ this.write(msg);
226
+ return this;
227
+ }
228
+
229
+ msg = { [LEVEL]: level, level, message: msg };
230
+ this._addDefaultMeta(msg);
231
+ this.write(msg);
232
+ return this;
233
+ }
234
+
235
+ const [meta] = splat;
236
+ if (typeof meta === 'object' && meta !== null) {
237
+ // Extract tokens, if none available default to empty array to
238
+ // ensure consistancy in expected results
239
+ const tokens = msg && msg.match && msg.match(formatRegExp);
240
+
241
+ if (!tokens) {
242
+ const info = Object.assign({}, this.defaultMeta, meta, {
243
+ [LEVEL]: level,
244
+ [SPLAT]: splat,
245
+ level,
246
+ message: msg
247
+ });
248
+
249
+ if (meta.message) info.message = `${info.message} ${meta.message}`;
250
+ if (meta.stack) info.stack = meta.stack;
251
+
252
+ this.write(info);
253
+ return this;
254
+ }
255
+ }
256
+
257
+ this.write(Object.assign({}, this.defaultMeta, {
258
+ [LEVEL]: level,
259
+ [SPLAT]: splat,
260
+ level,
261
+ message: msg
262
+ }));
263
+
264
+ return this;
265
+ }
266
+
267
+ /**
268
+ * Pushes data so that it can be picked up by all of our pipe targets.
269
+ * @param {mixed} info - TODO: add param description.
270
+ * @param {mixed} enc - TODO: add param description.
271
+ * @param {mixed} callback - Continues stream processing.
272
+ * @returns {undefined}
273
+ * @private
274
+ */
275
+ _transform(info, enc, callback) {
276
+ if (this.silent) {
277
+ return callback();
278
+ }
279
+
280
+ // [LEVEL] is only soft guaranteed to be set here since we are a proper
281
+ // stream. It is likely that `info` came in through `.log(info)` or
282
+ // `.info(info)`. If it is not defined, however, define it.
283
+ // This LEVEL symbol is provided by `triple-beam` and also used in:
284
+ // - logform
285
+ // - winston-transport
286
+ // - abstract-winston-transport
287
+ if (!info[LEVEL]) {
288
+ info[LEVEL] = info.level;
289
+ }
290
+
291
+ // Remark: really not sure what to do here, but this has been reported as
292
+ // very confusing by pre winston@2.0.0 users as quite confusing when using
293
+ // custom levels.
294
+ if (!this.levels[info[LEVEL]] && this.levels[info[LEVEL]] !== 0) {
295
+ // eslint-disable-next-line no-console
296
+ console.error('[winston] Unknown logger level: %s', info[LEVEL]);
297
+ }
298
+
299
+ // Remark: not sure if we should simply error here.
300
+ if (!this._readableState.pipes) {
301
+ // eslint-disable-next-line no-console
302
+ console.error(
303
+ '[winston] Attempt to write logs with no transports %j',
304
+ info
305
+ );
306
+ }
307
+
308
+ // Here we write to the `format` pipe-chain, which on `readable` above will
309
+ // push the formatted `info` Object onto the buffer for this instance. We trap
310
+ // (and re-throw) any errors generated by the user-provided format, but also
311
+ // guarantee that the streams callback is invoked so that we can continue flowing.
312
+ try {
313
+ this.push(this.format.transform(info, this.format.options));
314
+ } finally {
315
+ this._writableState.sync = false;
316
+ // eslint-disable-next-line callback-return
317
+ callback();
318
+ }
319
+ }
320
+
321
+ /**
322
+ * Delays the 'finish' event until all transport pipe targets have
323
+ * also emitted 'finish' or are already finished.
324
+ * @param {mixed} callback - Continues stream processing.
325
+ */
326
+ _final(callback) {
327
+ const transports = this.transports.slice();
328
+ asyncForEach(
329
+ transports,
330
+ (transport, next) => {
331
+ if (!transport || transport.finished) return setImmediate(next);
332
+ transport.once('finish', next);
333
+ transport.end();
334
+ },
335
+ callback
336
+ );
337
+ }
338
+
339
+ /**
340
+ * Adds the transport to this logger instance by piping to it.
341
+ * @param {mixed} transport - TODO: add param description.
342
+ * @returns {Logger} - TODO: add return description.
343
+ */
344
+ add(transport) {
345
+ // Support backwards compatibility with all existing `winston < 3.x.x`
346
+ // transports which meet one of two criteria:
347
+ // 1. They inherit from winston.Transport in < 3.x.x which is NOT a stream.
348
+ // 2. They expose a log method which has a length greater than 2 (i.e. more then
349
+ // just `log(info, callback)`.
350
+ const target =
351
+ !isStream(transport) || transport.log.length > 2
352
+ ? new LegacyTransportStream({ transport })
353
+ : transport;
354
+
355
+ if (!target._writableState || !target._writableState.objectMode) {
356
+ throw new Error(
357
+ 'Transports must WritableStreams in objectMode. Set { objectMode: true }.'
358
+ );
359
+ }
360
+
361
+ // Listen for the `error` event and the `warn` event on the new Transport.
362
+ this._onEvent('error', target);
363
+ this._onEvent('warn', target);
364
+ this.pipe(target);
365
+
366
+ if (transport.handleExceptions) {
367
+ this.exceptions.handle();
368
+ }
369
+
370
+ if (transport.handleRejections) {
371
+ this.rejections.handle();
372
+ }
373
+
374
+ return this;
375
+ }
376
+
377
+ /**
378
+ * Removes the transport from this logger instance by unpiping from it.
379
+ * @param {mixed} transport - TODO: add param description.
380
+ * @returns {Logger} - TODO: add return description.
381
+ */
382
+ remove(transport) {
383
+ if (!transport) return this;
384
+ let target = transport;
385
+ if (!isStream(transport) || transport.log.length > 2) {
386
+ target = this.transports.filter(
387
+ match => match.transport === transport
388
+ )[0];
389
+ }
390
+
391
+ if (target) {
392
+ this.unpipe(target);
393
+ }
394
+ return this;
395
+ }
396
+
397
+ /**
398
+ * Removes all transports from this logger instance.
399
+ * @returns {Logger} - TODO: add return description.
400
+ */
401
+ clear() {
402
+ this.unpipe();
403
+ return this;
404
+ }
405
+
406
+ /**
407
+ * Cleans up resources (streams, event listeners) for all transports
408
+ * associated with this instance (if necessary).
409
+ * @returns {Logger} - TODO: add return description.
410
+ */
411
+ close() {
412
+ this.exceptions.unhandle();
413
+ this.rejections.unhandle();
414
+ this.clear();
415
+ this.emit('close');
416
+ return this;
417
+ }
418
+
419
+ /**
420
+ * Sets the `target` levels specified on this instance.
421
+ * @param {Object} Target levels to use on this instance.
422
+ */
423
+ setLevels() {
424
+ warn.deprecated('setLevels');
425
+ }
426
+
427
+ /**
428
+ * Queries the all transports for this instance with the specified `options`.
429
+ * This will aggregate each transport's results into one object containing
430
+ * a property per transport.
431
+ * @param {Object} options - Query options for this instance.
432
+ * @param {function} callback - Continuation to respond to when complete.
433
+ */
434
+ query(options, callback) {
435
+ if (typeof options === 'function') {
436
+ callback = options;
437
+ options = {};
438
+ }
439
+
440
+ options = options || {};
441
+ const results = {};
442
+ const queryObject = Object.assign({}, options.query || {});
443
+
444
+ // Helper function to query a single transport
445
+ function queryTransport(transport, next) {
446
+ if (options.query && typeof transport.formatQuery === 'function') {
447
+ options.query = transport.formatQuery(queryObject);
448
+ }
449
+
450
+ transport.query(options, (err, res) => {
451
+ if (err) {
452
+ return next(err);
453
+ }
454
+
455
+ if (typeof transport.formatResults === 'function') {
456
+ res = transport.formatResults(res, options.format);
457
+ }
458
+
459
+ next(null, res);
460
+ });
461
+ }
462
+
463
+ // Helper function to accumulate the results from `queryTransport` into
464
+ // the `results`.
465
+ function addResults(transport, next) {
466
+ queryTransport(transport, (err, result) => {
467
+ // queryTransport could potentially invoke the callback multiple times
468
+ // since Transport code can be unpredictable.
469
+ if (next) {
470
+ result = err || result;
471
+ if (result) {
472
+ results[transport.name] = result;
473
+ }
474
+
475
+ // eslint-disable-next-line callback-return
476
+ next();
477
+ }
478
+
479
+ next = null;
480
+ });
481
+ }
482
+
483
+ // Iterate over the transports in parallel setting the appropriate key in
484
+ // the `results`.
485
+ asyncForEach(
486
+ this.transports.filter(transport => !!transport.query),
487
+ addResults,
488
+ () => callback(null, results)
489
+ );
490
+ }
491
+
492
+ /**
493
+ * Returns a log stream for all transports. Options object is optional.
494
+ * @param{Object} options={} - Stream options for this instance.
495
+ * @returns {Stream} - TODO: add return description.
496
+ */
497
+ stream(options = {}) {
498
+ const out = new Stream();
499
+ const streams = [];
500
+
501
+ out._streams = streams;
502
+ out.destroy = () => {
503
+ let i = streams.length;
504
+ while (i--) {
505
+ streams[i].destroy();
506
+ }
507
+ };
508
+
509
+ // Create a list of all transports for this instance.
510
+ this.transports
511
+ .filter(transport => !!transport.stream)
512
+ .forEach(transport => {
513
+ const str = transport.stream(options);
514
+ if (!str) {
515
+ return;
516
+ }
517
+
518
+ streams.push(str);
519
+
520
+ str.on('log', log => {
521
+ log.transport = log.transport || [];
522
+ log.transport.push(transport.name);
523
+ out.emit('log', log);
524
+ });
525
+
526
+ str.on('error', err => {
527
+ err.transport = err.transport || [];
528
+ err.transport.push(transport.name);
529
+ out.emit('error', err);
530
+ });
531
+ });
532
+
533
+ return out;
534
+ }
535
+
536
+ /**
537
+ * Returns an object corresponding to a specific timing. When done is called
538
+ * the timer will finish and log the duration. e.g.:
539
+ * @returns {Profile} - TODO: add return description.
540
+ * @example
541
+ * const timer = winston.startTimer()
542
+ * setTimeout(() => {
543
+ * timer.done({
544
+ * message: 'Logging message'
545
+ * });
546
+ * }, 1000);
547
+ */
548
+ startTimer() {
549
+ return new Profiler(this);
550
+ }
551
+
552
+ /**
553
+ * Tracks the time inbetween subsequent calls to this method with the same
554
+ * `id` parameter. The second call to this method will log the difference in
555
+ * milliseconds along with the message.
556
+ * @param {string} id Unique id of the profiler
557
+ * @returns {Logger} - TODO: add return description.
558
+ */
559
+ profile(id, ...args) {
560
+ const time = Date.now();
561
+ if (this.profilers[id]) {
562
+ const timeEnd = this.profilers[id];
563
+ delete this.profilers[id];
564
+
565
+ // Attempt to be kind to users if they are still using older APIs.
566
+ if (typeof args[args.length - 2] === 'function') {
567
+ // eslint-disable-next-line no-console
568
+ console.warn(
569
+ 'Callback function no longer supported as of winston@3.0.0'
570
+ );
571
+ args.pop();
572
+ }
573
+
574
+ // Set the duration property of the metadata
575
+ const info = typeof args[args.length - 1] === 'object' ? args.pop() : {};
576
+ info.level = info.level || 'info';
577
+ info.durationMs = time - timeEnd;
578
+ info.message = info.message || id;
579
+ return this.write(info);
580
+ }
581
+
582
+ this.profilers[id] = time;
583
+ return this;
584
+ }
585
+
586
+ /**
587
+ * Backwards compatibility to `exceptions.handle` in winston < 3.0.0.
588
+ * @returns {undefined}
589
+ * @deprecated
590
+ */
591
+ handleExceptions(...args) {
592
+ // eslint-disable-next-line no-console
593
+ console.warn(
594
+ 'Deprecated: .handleExceptions() will be removed in winston@4. Use .exceptions.handle()'
595
+ );
596
+ this.exceptions.handle(...args);
597
+ }
598
+
599
+ /**
600
+ * Backwards compatibility to `exceptions.handle` in winston < 3.0.0.
601
+ * @returns {undefined}
602
+ * @deprecated
603
+ */
604
+ unhandleExceptions(...args) {
605
+ // eslint-disable-next-line no-console
606
+ console.warn(
607
+ 'Deprecated: .unhandleExceptions() will be removed in winston@4. Use .exceptions.unhandle()'
608
+ );
609
+ this.exceptions.unhandle(...args);
610
+ }
611
+
612
+ /**
613
+ * Throw a more meaningful deprecation notice
614
+ * @throws {Error} - TODO: add throws description.
615
+ */
616
+ cli() {
617
+ throw new Error(
618
+ [
619
+ 'Logger.cli() was removed in winston@3.0.0',
620
+ 'Use a custom winston.formats.cli() instead.',
621
+ 'See: https://github.com/winstonjs/winston/tree/master/UPGRADE-3.0.md'
622
+ ].join('\n')
623
+ );
624
+ }
625
+
626
+ /**
627
+ * Bubbles the `event` that occured on the specified `transport` up
628
+ * from this instance.
629
+ * @param {string} event - The event that occured
630
+ * @param {Object} transport - Transport on which the event occured
631
+ * @private
632
+ */
633
+ _onEvent(event, transport) {
634
+ function transportEvent(err) {
635
+ // https://github.com/winstonjs/winston/issues/1364
636
+ if (event === 'error' && !this.transports.includes(transport)) {
637
+ this.add(transport);
638
+ }
639
+ this.emit(event, err, transport);
640
+ }
641
+
642
+ if (!transport['__winston' + event]) {
643
+ transport['__winston' + event] = transportEvent.bind(this);
644
+ transport.on(event, transport['__winston' + event]);
645
+ }
646
+ }
647
+
648
+ _addDefaultMeta(msg) {
649
+ if (this.defaultMeta) {
650
+ Object.assign(msg, this.defaultMeta);
651
+ }
652
+ }
653
+ }
654
+
655
+ function getLevelValue(levels, level) {
656
+ const value = levels[level];
657
+ if (!value && value !== 0) {
658
+ return null;
659
+ }
660
+ return value;
661
+ }
662
+
663
+ /**
664
+ * Represents the current readableState pipe targets for this Logger instance.
665
+ * @type {Array|Object}
666
+ */
667
+ Object.defineProperty(Logger.prototype, 'transports', {
668
+ configurable: false,
669
+ enumerable: true,
670
+ get() {
671
+ const { pipes } = this._readableState;
672
+ return !Array.isArray(pipes) ? [pipes].filter(Boolean) : pipes;
673
+ }
674
+ });
675
+
676
+ module.exports = Logger;