util.log.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * 日志(log)工具类
  3. * <pre>
  4. * 作者:hugh zhuang
  5. * 邮箱:3378340995@qq.com
  6. * 日期:2018-07-02-下午3:29:34
  7. * 版权:广州流辰信息技术有限公司
  8. * </pre>
  9. */
  10. /**
  11. * @description 返回这个样式的颜色值
  12. * @param {String} type 样式名称 [ primary | success | warning | danger | text ]
  13. */
  14. function typeColor(type = 'default') {
  15. let color = ''
  16. switch (type) {
  17. case 'default': color = '#35495E'; break
  18. case 'primary': color = '#3488ff'; break
  19. case 'success': color = '#43B883'; break
  20. case 'warning': color = '#e6a23c'; break
  21. case 'danger': color = '#f56c6c'; break
  22. case 'error': color = '#f56c6c'; break
  23. default: break
  24. }
  25. return color
  26. }
  27. const log = {
  28. /**
  29. * @description 控制台打印一个 [ title | text ] 样式的信息
  30. * @param {String} title title text
  31. * @param {String} info info text
  32. * @param {String} type style
  33. */
  34. capsule: function(title, info, type = 'primary') {
  35. console.log(
  36. `%c ${title} %c ${info} %c`,
  37. 'background:#35495E; padding: 1px; border-radius: 3px 0 0 3px; color: #fff;',
  38. `background:${typeColor(type)}; padding: 1px; border-radius: 0 3px 3px 0; color: #fff;`,
  39. 'background:transparent'
  40. )
  41. },
  42. /**
  43. * @description 控制台打印彩色文字
  44. */
  45. colorful: function(textArr) {
  46. console.log(
  47. `%c${textArr.map(t => t.text || '').join('%c')}`,
  48. ...textArr.map(t => `color: ${typeColor(t.type)};`)
  49. )
  50. },
  51. /**
  52. * @description 控制台打印 default 样式的文字
  53. */
  54. default: function(text) {
  55. log.colorful([{ text }])
  56. },
  57. /**
  58. * @description 控制台打印 primary 样式的文字
  59. */
  60. primary: function(text) {
  61. log.colorful([{ text, type: 'primary' }])
  62. },
  63. /**
  64. * @description 控制台打印 success 样式的文字
  65. */
  66. success: function(text) {
  67. log.colorful([{ text, type: 'success' }])
  68. },
  69. /**
  70. * @description 控制台打印 warning 样式的文字
  71. */
  72. warning: function(text) {
  73. log.colorful([{ text, type: 'warning' }])
  74. },
  75. /**
  76. * @description 控制台打印 danger 样式的文字
  77. */
  78. danger: function(text) {
  79. log.colorful([{ text, type: 'danger' }])
  80. },
  81. /**
  82. * @description 控制台打印 error 样式的文字
  83. */
  84. error: function(text) {
  85. log.colorful([{ text, type: 'error' }])
  86. }
  87. }
  88. export default log