logger.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const chalk = require('chalk')
  2. const escapeStringRegexp = require('escape-string-regexp')
  3. const util = require('util')
  4. const { ProviderApiError, ProviderAuthError } = require('./provider/error')
  5. const valuesToMask = []
  6. /**
  7. * Adds a list of strings that should be masked by the logger.
  8. * This function can only be called once through out the life of the server.
  9. *
  10. * @param {Array} maskables a list of strings to be masked
  11. */
  12. exports.setMaskables = (maskables) => {
  13. maskables.forEach((i) => {
  14. valuesToMask.push(escapeStringRegexp(i))
  15. })
  16. Object.freeze(valuesToMask)
  17. }
  18. /**
  19. * Mask the secret content of a message
  20. *
  21. * @param {string} msg the message whose content should be masked
  22. * @returns {string}
  23. */
  24. function maskMessage (msg) {
  25. let out = msg
  26. for (const toBeMasked of valuesToMask) {
  27. const toBeReplaced = new RegExp(toBeMasked, 'gi')
  28. out = out.replace(toBeReplaced, '******')
  29. }
  30. return out
  31. }
  32. /**
  33. * message log
  34. *
  35. * @param {string | Error} arg the message or error to log
  36. * @param {string} tag a unique tag to easily search for this message
  37. * @param {string} level error | info | debug
  38. * @param {string} [id] a unique id to easily trace logs tied to a request
  39. * @param {Function} [color] function to display the log in appropriate color
  40. */
  41. const log = (arg, tag = '', level, id = '', color = (message) => message) => {
  42. const time = new Date().toISOString()
  43. const whitespace = tag && id ? ' ' : ''
  44. function msgToString () {
  45. // We don't need to log stack trace on special errors that we ourselves have produced
  46. // (to reduce log noise)
  47. if ((arg instanceof ProviderApiError || arg instanceof ProviderAuthError) && typeof arg.message === 'string') {
  48. return arg.message
  49. }
  50. if (typeof arg === 'string') return arg
  51. return util.inspect(arg)
  52. }
  53. const msgString = msgToString()
  54. const masked = maskMessage(msgString)
  55. // eslint-disable-next-line no-console
  56. console.log(color(`companion: ${time} [${level}] ${id}${whitespace}${tag}`), color(masked))
  57. }
  58. /**
  59. * INFO level log
  60. *
  61. * @param {string} msg the message to log
  62. * @param {string} [tag] a unique tag to easily search for this message
  63. * @param {string} [traceId] a unique id to easily trace logs tied to a request
  64. */
  65. exports.info = (msg, tag, traceId) => {
  66. log(msg, tag, 'info', traceId)
  67. }
  68. /**
  69. * WARN level log
  70. *
  71. * @param {string} msg the message to log
  72. * @param {string} [tag] a unique tag to easily search for this message
  73. * @param {string} [traceId] a unique id to easily trace logs tied to a request
  74. */
  75. exports.warn = (msg, tag, traceId) => {
  76. // @ts-ignore
  77. log(msg, tag, 'warn', traceId, chalk.bold.yellow)
  78. }
  79. /**
  80. * ERROR level log
  81. *
  82. * @param {string | Error} msg the message to log
  83. * @param {string} [tag] a unique tag to easily search for this message
  84. * @param {string} [traceId] a unique id to easily trace logs tied to a request
  85. */
  86. exports.error = (msg, tag, traceId) => {
  87. // @ts-ignore
  88. log(msg, tag, 'error', traceId, chalk.bold.red)
  89. }
  90. /**
  91. * DEBUG level log
  92. *
  93. * @param {string} msg the message to log
  94. * @param {string} [tag] a unique tag to easily search for this message
  95. * @param {string} [traceId] a unique id to easily trace logs tied to a request
  96. */
  97. exports.debug = (msg, tag, traceId) => {
  98. if (process.env.NODE_ENV !== 'production') {
  99. // @ts-ignore
  100. log(msg, tag, 'debug', traceId, chalk.bold.blue)
  101. }
  102. }