c-console.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { v4 as uuid } from 'uuid'
  2. const isDev = process.env.NODE_ENV === 'development'
  3. class CConsole {
  4. constructor (effective) {
  5. this.effective = effective
  6. this.requestHistory = {}
  7. }
  8. open (info) {
  9. if (this.effective) {
  10. const uid = uuid()
  11. const data = { uid, info: { method: info, startTime: Date.now() } }
  12. this.requestHistory[uid] = data
  13. return uid
  14. }
  15. }
  16. append (uid, type, info) {
  17. if (this.effective) {
  18. const currentReq = this.requestHistory[uid]
  19. if (currentReq) {
  20. currentReq.info[type] = info
  21. }
  22. }
  23. }
  24. end (uid) {
  25. if (this.effective) {
  26. const endTime = Date.now()
  27. const times = endTime - this.requestHistory[uid].info.startTime
  28. Reflect.deleteProperty(this.requestHistory[uid].info, 'startTime')
  29. this.requestHistory[uid].info.times = times
  30. console.log('%c【cConsole】', 'color: #67c23a;', this.requestHistory[uid].info)
  31. this.requestHistory[uid] = null
  32. }
  33. }
  34. }
  35. export const cConsole = new CConsole(isDev)