ApiLogger.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import DomUtils from './DomUtils'
  15. type LogType = 'REQUEST' | 'RESPONSE'
  16. type LogOptions = {
  17. url?: string | null,
  18. method: string,
  19. type: LogType,
  20. description?: string,
  21. requestStatus?: number | 'canceled',
  22. requestError?: any,
  23. windowPath?: string,
  24. stack?: string,
  25. }
  26. type RequestLog = LogOptions & {
  27. date: Date,
  28. }
  29. type Log = {
  30. requests: RequestLog[],
  31. userAgent: string,
  32. platform: string,
  33. }
  34. const MAX_LOGS = 1000
  35. const validateLog = (logs: RequestLog[]): boolean => {
  36. if (logs.length && !logs[0].windowPath) {
  37. return false
  38. }
  39. return true
  40. }
  41. class Storage {
  42. static NAME = 'apiLog'
  43. static EMPTY = '[]'
  44. static getLogRaw(): string {
  45. return localStorage.getItem(this.NAME) || this.EMPTY
  46. }
  47. static saveLog(options: LogOptions) {
  48. let logs: RequestLog[] = JSON.parse(localStorage.getItem(this.NAME) || this.EMPTY)
  49. if (!validateLog(logs)) {
  50. localStorage.setItem(this.NAME, this.EMPTY)
  51. logs = JSON.parse(this.EMPTY)
  52. }
  53. const newRequest: RequestLog = {
  54. date: new Date(),
  55. windowPath: window.location.href.replace(`${window.location.protocol}//${window.location.host}`, ''),
  56. ...options,
  57. }
  58. if (options.type === 'REQUEST') {
  59. const err = new Error()
  60. newRequest.stack = err.stack
  61. }
  62. if (logs.length > MAX_LOGS) {
  63. logs.splice(0, logs.length - MAX_LOGS)
  64. }
  65. logs.push(newRequest)
  66. localStorage.setItem(this.NAME, JSON.stringify(logs))
  67. }
  68. }
  69. class ApiLogger {
  70. log(options: LogOptions) {
  71. if (options.type === 'REQUEST') {
  72. console.log(`%cSending ${options.method} Request to ${options.url}`, 'color: #F5A623')
  73. } else if (options.requestError) {
  74. console.log(`%cError Response: ${options.url}`, 'color: #D0021B', options.requestError)
  75. } else if (options.requestStatus === 'canceled') {
  76. console.log(`%cRequest Canceled: ${options.url}`, 'color: #0044CA')
  77. } else if (options.requestStatus === 500) {
  78. console.log(`%cError Something happened in setting up the request: ${options.url}`, 'color: #D0021B')
  79. }
  80. if (options.requestError && options.requestError.response
  81. && options.requestError.response.data) {
  82. // eslint-disable-next-line no-param-reassign
  83. options.requestError = options.requestError.response.data.error
  84. }
  85. Storage.saveLog(options)
  86. }
  87. download() {
  88. const requests: RequestLog[] = JSON.parse(Storage.getLogRaw())
  89. const log: Log = {
  90. requests,
  91. userAgent: window.navigator.userAgent,
  92. platform: window.navigator.platform,
  93. }
  94. DomUtils.download(JSON.stringify(log), 'coriolis-log.json')
  95. }
  96. }
  97. export default new ApiLogger()