timestamp.js 752 B

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. const fecha = require('fecha');
  3. const format = require('./format');
  4. /*
  5. * function timestamp (info)
  6. * Returns a new instance of the timestamp Format which adds a timestamp
  7. * to the info. It was previously available in winston < 3.0.0 as:
  8. *
  9. * - { timestamp: true } // `new Date.toISOString()`
  10. * - { timestamp: function:String } // Value returned by `timestamp()`
  11. */
  12. module.exports = format((info, opts) => {
  13. if (opts.format) {
  14. info.timestamp = typeof opts.format === 'function'
  15. ? opts.format()
  16. : fecha.format(new Date(), opts.format);
  17. }
  18. if (!info.timestamp) {
  19. info.timestamp = new Date().toISOString();
  20. }
  21. if (opts.alias) {
  22. info[opts.alias] = info.timestamp;
  23. }
  24. return info;
  25. });