timestamp.js 817 B

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