Team Ai
Apppublic

sanket3280/code-execution

sourceHugging Faceupdated 11mo agoView on Hugging Face
0likes
logger.js141 linesDownload Raw Back to utils
1/**2 * Centralized Logging Service3 * Provides consistent logging across the application4 */5 6const { config } = require('../config/env.config');7 8// ANSI color codes for terminal output9const colors = {10  reset: '\x1b[0m',11  bright: '\x1b[1m',12  dim: '\x1b[2m',13  red: '\x1b[31m',14  green: '\x1b[32m',15  yellow: '\x1b[33m',16  blue: '\x1b[34m',17  magenta: '\x1b[35m',18  cyan: '\x1b[36m',19  white: '\x1b[37m',20};21 22class Logger {23  constructor() {24    this.isDevelopment = config.isDevelopment;25    this.logLevel = config.logging.level;26  }27 28  /**29   * Format timestamp30   */31  getTimestamp() {32    return new Date().toISOString();33  }34 35  /**36   * Format log message37   */38  formatMessage(level, message, meta = {}) {39    const timestamp = this.getTimestamp();40    const metaStr = Object.keys(meta).length > 0 ? JSON.stringify(meta) : '';41    return `[${timestamp}] [${level}] ${message} ${metaStr}`.trim();42  }43 44  /**45   * Log with color (development only)46   */47  logWithColor(color, level, message, meta) {48    if (this.isDevelopment) {49      console.log(`${color}${this.formatMessage(level, message, meta)}${colors.reset}`);50    } else {51      console.log(this.formatMessage(level, message, meta));52    }53  }54 55  /**56   * Info level logging57   */58  info(message, meta = {}) {59    this.logWithColor(colors.blue, 'INFO', message, meta);60  }61 62  /**63   * Success level logging64   */65  success(message, meta = {}) {66    this.logWithColor(colors.green, 'SUCCESS', message, meta);67  }68 69  /**70   * Warning level logging71   */72  warn(message, meta = {}) {73    this.logWithColor(colors.yellow, 'WARN', message, meta);74  }75 76  /**77   * Error level logging78   */79  error(message, error = null, meta = {}) {80    const errorMeta = error ? {81      ...meta,82      error: error.message,83      stack: this.isDevelopment ? error.stack : undefined,84    } : meta;85    86    this.logWithColor(colors.red, 'ERROR', message, errorMeta);87  }88 89  /**90   * Debug level logging (only in development)91   */92  debug(message, meta = {}) {93    if (this.isDevelopment) {94      this.logWithColor(colors.dim, 'DEBUG', message, meta);95    }96  }97 98  /**99   * HTTP request logging100   */101  http(method, path, statusCode, duration) {102    const color = statusCode >= 500 ? colors.red :103                  statusCode >= 400 ? colors.yellow :104                  statusCode >= 300 ? colors.cyan :105                  colors.green;106    107    this.logWithColor(108      color,109      'HTTP',110      `${method} ${path} ${statusCode}`,111      { duration: `${duration}ms` }112    );113  }114 115  /**116   * Database operation logging117   */118  db(operation, collection, meta = {}) {119    this.logWithColor(120      colors.magenta,121      'DB',122      `${operation} ${collection}`,123      meta124    );125  }126 127  /**128   * Authentication logging129   */130  auth(action, userId, meta = {}) {131    this.logWithColor(132      colors.cyan,133      'AUTH',134      `${action} - User: ${userId}`,135      meta136    );137  }138}139 140// Export singleton instance141module.exports = new Logger();