logger.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * Production-safe logging utility for GrabZilla renderer process
  3. *
  4. * Note: This is the renderer-side logger. It uses window.ENV to detect production mode.
  5. * Most logs are disabled in production to protect user privacy and reduce console noise.
  6. */
  7. // Determine if running in production (renderer process)
  8. const isProduction = window?.ENV?.NODE_ENV === 'production' || window?.ENV?.ELECTRON_IS_PACKAGED === 'true'
  9. // Log levels
  10. const LogLevel = {
  11. ERROR: 0,
  12. WARN: 1,
  13. INFO: 2,
  14. DEBUG: 3
  15. }
  16. // Current log level (DEBUG disabled in production)
  17. const currentLevel = isProduction ? LogLevel.INFO : LogLevel.DEBUG
  18. /**
  19. * Sanitize file path to show only filename
  20. */
  21. function sanitizeFilePath(filePath) {
  22. if (!filePath || typeof filePath !== 'string') return '[invalid-path]'
  23. try {
  24. const parts = filePath.split(/[\/\\]/)
  25. return parts[parts.length - 1]
  26. } catch {
  27. return '[path-error]'
  28. }
  29. }
  30. /**
  31. * Sanitize URL to remove query parameters
  32. */
  33. function sanitizeUrl(url) {
  34. if (!url || typeof url !== 'string') return '[invalid-url]'
  35. try {
  36. const parsed = new URL(url)
  37. return `${parsed.protocol}//${parsed.hostname}${parsed.pathname}`
  38. } catch {
  39. return '[sanitized]'
  40. }
  41. }
  42. /**
  43. * Sanitize object by removing/redacting sensitive fields
  44. */
  45. function sanitizeObject(obj) {
  46. if (!obj || typeof obj !== 'object') return obj
  47. const sanitized = Array.isArray(obj) ? [] : {}
  48. for (const [key, value] of Object.entries(obj)) {
  49. const lowerKey = key.toLowerCase()
  50. // Redact sensitive fields
  51. if (lowerKey.includes('cookie') || lowerKey.includes('auth') || lowerKey.includes('token') || lowerKey.includes('key')) {
  52. sanitized[key] = '[REDACTED]'
  53. }
  54. // Sanitize file paths
  55. else if (lowerKey.includes('path') && typeof value === 'string') {
  56. sanitized[key] = sanitizeFilePath(value)
  57. }
  58. // Sanitize URLs
  59. else if (lowerKey.includes('url') && typeof value === 'string') {
  60. sanitized[key] = sanitizeUrl(value)
  61. }
  62. // Recursively sanitize nested objects
  63. else if (value && typeof value === 'object') {
  64. sanitized[key] = sanitizeObject(value)
  65. }
  66. else {
  67. sanitized[key] = value
  68. }
  69. }
  70. return sanitized
  71. }
  72. /**
  73. * Format log message with timestamp and level
  74. */
  75. function formatMessage(level, ...args) {
  76. const timestamp = new Date().toISOString()
  77. const levelStr = ['ERROR', 'WARN', 'INFO', 'DEBUG'][level]
  78. const prefix = `[${timestamp}] [${levelStr}]`
  79. // Sanitize arguments
  80. const sanitizedArgs = args.map(arg => {
  81. if (typeof arg === 'string') {
  82. // Check if string looks like a URL
  83. if (arg.startsWith('http://') || arg.startsWith('https://')) {
  84. return sanitizeUrl(arg)
  85. }
  86. // Check if string looks like a file path
  87. if (arg.includes('/') || arg.includes('\\')) {
  88. return sanitizeFilePath(arg)
  89. }
  90. return arg
  91. }
  92. if (typeof arg === 'object') {
  93. return sanitizeObject(arg)
  94. }
  95. return arg
  96. })
  97. return [prefix, ...sanitizedArgs]
  98. }
  99. /**
  100. * Log error message (always shown)
  101. */
  102. function error(...args) {
  103. if (currentLevel >= LogLevel.ERROR) {
  104. console.error(...formatMessage(LogLevel.ERROR, ...args))
  105. }
  106. }
  107. /**
  108. * Log warning message (shown in production and development)
  109. */
  110. function warn(...args) {
  111. if (currentLevel >= LogLevel.WARN) {
  112. console.warn(...formatMessage(LogLevel.WARN, ...args))
  113. }
  114. }
  115. /**
  116. * Log info message (shown in production and development)
  117. */
  118. function info(...args) {
  119. if (currentLevel >= LogLevel.INFO) {
  120. console.log(...formatMessage(LogLevel.INFO, ...args))
  121. }
  122. }
  123. /**
  124. * Log debug message (development only)
  125. */
  126. function debug(...args) {
  127. if (currentLevel >= LogLevel.DEBUG) {
  128. console.log(...formatMessage(LogLevel.DEBUG, ...args))
  129. }
  130. }
  131. /**
  132. * Legacy console.log replacement - maps to debug level
  133. */
  134. function log(...args) {
  135. debug('[LEGACY]', ...args)
  136. }
  137. export {
  138. error,
  139. warn,
  140. info,
  141. debug,
  142. log,
  143. sanitizeFilePath,
  144. sanitizeUrl,
  145. sanitizeObject,
  146. isProduction,
  147. currentLevel,
  148. LogLevel
  149. }