profiler.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * profiler.js: TODO: add file header description.
  3. *
  4. * (C) 2010 Charlie Robbins
  5. * MIT LICENCE
  6. */
  7. 'use strict';
  8. /**
  9. * TODO: add class description.
  10. * @type {Profiler}
  11. * @private
  12. */
  13. module.exports = class Profiler {
  14. /**
  15. * Constructor function for the Profiler instance used by
  16. * `Logger.prototype.startTimer`. When done is called the timer will finish
  17. * and log the duration.
  18. * @param {!Logger} logger - TODO: add param description.
  19. * @private
  20. */
  21. constructor(logger) {
  22. if (!logger) {
  23. throw new Error('Logger is required for profiling.');
  24. }
  25. this.logger = logger;
  26. this.start = Date.now();
  27. }
  28. /**
  29. * Ends the current timer (i.e. Profiler) instance and logs the `msg` along
  30. * with the duration since creation.
  31. * @returns {mixed} - TODO: add return description.
  32. * @private
  33. */
  34. done(...args) {
  35. if (typeof args[args.length - 1] === 'function') {
  36. // eslint-disable-next-line no-console
  37. console.warn('Callback function no longer supported as of winston@3.0.0');
  38. args.pop();
  39. }
  40. const info = typeof args[args.length - 1] === 'object' ? args.pop() : {};
  41. info.level = info.level || 'info';
  42. info.durationMs = (Date.now()) - this.start;
  43. return this.logger.write(info);
  44. }
  45. };