logger.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. var util = require('util')
  2. var _ = require('lodash')
  3. var loggerInstance
  4. var defaultProvider = {
  5. log: console.log,
  6. debug: console.log, // use .log(); since console does not have .debug()
  7. info: console.info,
  8. warn: console.warn,
  9. error: console.error
  10. }
  11. // log level 'weight'
  12. var LEVELS = {
  13. debug: 10,
  14. info: 20,
  15. warn: 30,
  16. error: 50,
  17. silent: 80
  18. }
  19. module.exports = {
  20. // singleton
  21. getInstance: function() {
  22. if (!loggerInstance) {
  23. loggerInstance = new Logger()
  24. }
  25. return loggerInstance
  26. },
  27. getArrow: getArrow
  28. }
  29. function Logger() {
  30. var logLevel
  31. var provider
  32. var api = {
  33. log: log,
  34. debug: debug,
  35. info: info,
  36. warn: warn,
  37. error: error,
  38. setLevel: function(v) {
  39. if (isValidLevel(v)) {
  40. logLevel = v
  41. }
  42. },
  43. setProvider: function(fn) {
  44. if (fn && isValidProvider(fn)) {
  45. provider = fn(defaultProvider)
  46. }
  47. }
  48. }
  49. init()
  50. return api
  51. function init() {
  52. api.setLevel('info')
  53. api.setProvider(function() {
  54. return defaultProvider
  55. })
  56. }
  57. // log will log messages, regardless of logLevels
  58. function log() {
  59. provider.log(_interpolate.apply(null, arguments))
  60. }
  61. function debug() {
  62. if (_showLevel('debug')) {
  63. provider.debug(_interpolate.apply(null, arguments))
  64. }
  65. }
  66. function info() {
  67. if (_showLevel('info')) {
  68. provider.info(_interpolate.apply(null, arguments))
  69. }
  70. }
  71. function warn() {
  72. if (_showLevel('warn')) {
  73. provider.warn(_interpolate.apply(null, arguments))
  74. }
  75. }
  76. function error() {
  77. if (_showLevel('error')) {
  78. provider.error(_interpolate.apply(null, arguments))
  79. }
  80. }
  81. /**
  82. * Decide to log or not to log, based on the log levels 'weight'
  83. * @param {String} showLevel [debug, info, warn, error, silent]
  84. * @return {Boolean}
  85. */
  86. function _showLevel(showLevel) {
  87. var result = false
  88. var currentLogLevel = LEVELS[logLevel]
  89. if (currentLogLevel && currentLogLevel <= LEVELS[showLevel]) {
  90. result = true
  91. }
  92. return result
  93. }
  94. // make sure logged messages and its data are return interpolated
  95. // make it possible for additional log data, such date/time or custom prefix.
  96. function _interpolate() {
  97. var fn = _.spread(util.format)
  98. var result = fn(_.slice(arguments))
  99. return result
  100. }
  101. function isValidProvider(fnProvider) {
  102. var result = true
  103. if (fnProvider && !_.isFunction(fnProvider)) {
  104. throw new Error('[HPM] Log provider config error. Expecting a function.')
  105. }
  106. return result
  107. }
  108. function isValidLevel(levelName) {
  109. var validLevels = _.keys(LEVELS)
  110. var isValid = _.includes(validLevels, levelName)
  111. if (!isValid) {
  112. throw new Error('[HPM] Log level error. Invalid logLevel.')
  113. }
  114. return isValid
  115. }
  116. }
  117. /**
  118. * -> normal proxy
  119. * => router
  120. * ~> pathRewrite
  121. * ≈> router + pathRewrite
  122. *
  123. * @param {String} originalPath
  124. * @param {String} newPath
  125. * @param {String} originalTarget
  126. * @param {String} newTarget
  127. * @return {String}
  128. */
  129. function getArrow(originalPath, newPath, originalTarget, newTarget) {
  130. var arrow = ['>']
  131. var isNewTarget = originalTarget !== newTarget // router
  132. var isNewPath = originalPath !== newPath // pathRewrite
  133. if (isNewPath && !isNewTarget) {
  134. arrow.unshift('~')
  135. } else if (!isNewPath && isNewTarget) {
  136. arrow.unshift('=')
  137. } else if (isNewPath && isNewTarget) {
  138. arrow.unshift('≈')
  139. } else {
  140. arrow.unshift('-')
  141. }
  142. return arrow.join('')
  143. }