timestamp.js 741 B

12345678910111213141516171819202122232425262728
  1. 'use strict';
  2. var fecha = require('fecha');
  3. var 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(function (info, opts) {
  13. if (opts.format) {
  14. info.timestamp = typeof opts.format === 'function' ? opts.format() : fecha.format(new Date(), opts.format);
  15. }
  16. if (!info.timestamp) {
  17. info.timestamp = new Date().toISOString();
  18. }
  19. if (opts.alias) {
  20. info[opts.alias] = info.timestamp;
  21. }
  22. return info;
  23. });