log.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. const chalk = require('chalk')
  2. const VERBOSE_KEY = Symbol('verbose_key')
  3. var lvl = 0
  4. var wasRaw = false
  5. module.exports.setVerbose = function (value) {
  6. global[VERBOSE_KEY] = value
  7. }
  8. function isVerbose (i) {
  9. return global[VERBOSE_KEY] >= i
  10. }
  11. function indent () {
  12. var indent = ''
  13. for (var i = 0; i < lvl; i++) {
  14. indent += ' '
  15. }
  16. return indent
  17. }
  18. /**
  19. * Print an error to the screen.
  20. * These will only be output if log level >= 0
  21. * Args is a description of the error.
  22. * @param {*} args - string description
  23. */
  24. module.exports.error = function (...args) {
  25. if (!isVerbose(0)) { return }
  26. if (wasRaw) {
  27. console.log('')
  28. wasRaw = false
  29. }
  30. console.log(indent() + chalk.red('ERROR!'), ...args)
  31. }
  32. /**
  33. * Print to the screen that an action was taken
  34. * These will only be output if log level >= 1
  35. * @param {string} action - Action that was taken
  36. * @param {*} args - string description
  37. */
  38. module.exports.action = function (action, ...args) {
  39. if (!isVerbose(1)) { return }
  40. if (wasRaw) {
  41. console.log('')
  42. wasRaw = false
  43. }
  44. console.log(indent() + chalk.green(action), ...args)
  45. }
  46. /**
  47. * Print to screen that a group of actions happened
  48. * These will only be output if log level >= 1
  49. * @param {string} action - action
  50. * @param {*} args - string description
  51. */
  52. module.exports.begin = function (action, ...args) {
  53. if (!isVerbose(1)) { return }
  54. if (wasRaw) {
  55. console.log('')
  56. wasRaw = false
  57. }
  58. console.log(indent() + chalk.green(action), ...args)
  59. lvl += 1
  60. }
  61. /**
  62. * Exit indent group
  63. * These will only be output if log level >= 1
  64. */
  65. module.exports.end = function () {
  66. if (!isVerbose(1)) { return }
  67. if (wasRaw) {
  68. console.log('')
  69. wasRaw = false
  70. }
  71. lvl -= 1
  72. }
  73. /**
  74. * Print a warning
  75. * * These will only be output if log level >= 0
  76. * @param {*} args - String description of the warning
  77. */
  78. module.exports.warning = function (...args) {
  79. if (!isVerbose(0)) { return }
  80. if (wasRaw) {
  81. console.log('')
  82. wasRaw = false
  83. }
  84. console.log(indent() + chalk.yellow('WARNING!'), ...args)
  85. }
  86. /**
  87. * Verbose logging drop-in for console.log
  88. * These will only be output if log level >= 2
  89. * @param {*} args - print output
  90. */
  91. module.exports.verbose = function (...args) {
  92. if (!isVerbose(2)) { return }
  93. if (wasRaw) {
  94. console.log('')
  95. wasRaw = false
  96. }
  97. console.log(indent() + chalk.blue('verbose'), ...args)
  98. }
  99. /**
  100. * Raw logging drop-in for console.log
  101. * These lines will NOT be formatted.
  102. * These will only be output if log level >= 0
  103. * @param {*} args - print output
  104. */
  105. module.exports.raw = function (...args) {
  106. if (!isVerbose(0)) { return }
  107. if (!wasRaw) {
  108. console.log('')
  109. wasRaw = true
  110. }
  111. console.log(...args)
  112. }