metadata.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const { format } = require('../');
  2. const { combine, json, metadata, timestamp } = format;
  3. // Default Functionality (no options passed)
  4. const defaultFormatter = combine(
  5. timestamp(),
  6. metadata(),
  7. json()
  8. );
  9. const defaultMessage = defaultFormatter.transform({
  10. level: 'info',
  11. message: 'This should be a message.',
  12. application: 'Microsoft Office',
  13. store: 'Big Box Store',
  14. purchaseAmount: '9.99'
  15. });
  16. console.dir(defaultMessage);
  17. // Fill all keys into metadata except those provided
  18. const formattedLogger = combine(
  19. timestamp(),
  20. metadata({ fillExcept: ['message', 'level', 'timestamp'] }),
  21. json()
  22. );
  23. const fillExceptMessage = formattedLogger.transform({
  24. level: 'info',
  25. message: 'This should have attached metadata',
  26. category: 'movies',
  27. subCategory: 'action'
  28. });
  29. console.dir(fillExceptMessage);
  30. // Fill only the keys provided into the object, and also give it a different key
  31. const customMetadataLogger = combine(
  32. timestamp(),
  33. metadata({ fillWith: ['publisher', 'author', 'book'], key: 'bookInfo' }),
  34. json()
  35. );
  36. const fillWithMessage = customMetadataLogger.transform({
  37. level: 'debug',
  38. message: 'This message should be outside of the bookInfo object',
  39. publisher: 'Lorem Press',
  40. author: 'Albert Einstein',
  41. book: '4D Chess for Dummies',
  42. label: 'myCustomLabel'
  43. });
  44. console.dir(fillWithMessage);
  45. // Demonstrates Metadata 'chaining' to combine multiple datapoints.
  46. const chainedMetadata = combine(
  47. timestamp(),
  48. metadata({ fillWith: ['publisher', 'author', 'book'], key: 'bookInfo' }),
  49. metadata({ fillWith: ['purchasePrice', 'purchaseDate', 'transactionId'], key: 'transactionInfo' }),
  50. metadata({ fillExcept: ['level', 'message', 'label', 'timestamp'] }),
  51. json()
  52. );
  53. const chainedMessage = chainedMetadata.transform({
  54. level: 'debug',
  55. message: 'This message should be outside of the bookInfo object',
  56. publisher: 'Lorem Press',
  57. author: 'Albert Einstein',
  58. book: '4D Chess for Dummies',
  59. label: 'myCustomLabel',
  60. purchasePrice: '9.99',
  61. purchaseDate: '2.10.2018',
  62. transactionId: '123ABC'
  63. });
  64. console.dir(chainedMessage);