All files / utils logger.js

18.86% Statements 10/53
3.03% Branches 1/33
8.33% Functions 1/12
20.4% Lines 10/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150          3x 3x 3x 3x       3x 3x 3x               3x                                                                                                                                                                                                                                                         3x   3x
/**
 * Centralized Logging System
 * Replaces all console.log statements with structured logging
 */
 
const chalk = require('chalk');
const fs = require('fs-extra');
const path = require('path');
const os = require('os');
 
class Logger {
  constructor() {
    this.logLevel = process.env.VAULTACE_LOG_LEVEL || 'info';
    this.logFile = path.join(os.homedir(), '.vaultace', 'cli.log');
    this.levels = {
      debug: 0,
      info: 1,
      warn: 2,
      error: 3
    };
 
    // Ensure log directory exists
    fs.ensureDirSync(path.dirname(this.logFile));
  }
 
  shouldLog(level) {
    return this.levels[level] >= this.levels[this.logLevel];
  }
 
  formatMessage(level, message, context = {}) {
    const timestamp = new Date().toISOString();
    const formatted = {
      timestamp,
      level: level.toUpperCase(),
      message,
      context,
      pid: process.pid
    };
    return formatted;
  }
 
  writeToFile(formatted) {
    try {
      const logLine = JSON.stringify(formatted) + '\n';
      fs.appendFileSync(this.logFile, logLine);
    } catch (error) {
      // Fail silently for file logging to avoid infinite loops
    }
  }
 
  debug(message, context = {}) {
    if (!this.shouldLog('debug')) return;
 
    const formatted = this.formatMessage('debug', message, context);
    this.writeToFile(formatted);
 
    if (process.env.VAULTACE_VERBOSE) {
      console.log(chalk.gray(`[DEBUG] ${message}`));
      if (Object.keys(context).length > 0) {
        console.log(chalk.gray(JSON.stringify(context, null, 2)));
      }
    }
  }
 
  info(message, context = {}) {
    if (!this.shouldLog('info')) return;
 
    const formatted = this.formatMessage('info', message, context);
    this.writeToFile(formatted);
 
    console.log(chalk.blue(`[INFO] ${message}`));
    if (Object.keys(context).length > 0 && process.env.VAULTACE_VERBOSE) {
      console.log(chalk.gray(JSON.stringify(context, null, 2)));
    }
  }
 
  warn(message, context = {}) {
    if (!this.shouldLog('warn')) return;
 
    const formatted = this.formatMessage('warn', message, context);
    this.writeToFile(formatted);
 
    console.warn(chalk.yellow(`[WARN] ${message}`));
    if (Object.keys(context).length > 0) {
      console.warn(chalk.gray(JSON.stringify(context, null, 2)));
    }
  }
 
  error(message, context = {}) {
    if (!this.shouldLog('error')) return;
 
    const formatted = this.formatMessage('error', message, context);
    this.writeToFile(formatted);
 
    console.error(chalk.red(`[ERROR] ${message}`));
    if (Object.keys(context).length > 0) {
      console.error(chalk.gray(JSON.stringify(context, null, 2)));
    }
  }
 
  // Security audit logging
  security(event, details = {}) {
    const securityLog = this.formatMessage('security', event, {
      ...details,
      security_event: true,
      user: process.env.USER || 'unknown'
    });
 
    this.writeToFile(securityLog);
 
    if (process.env.VAULTACE_VERBOSE) {
      console.log(chalk.magenta(`[SECURITY] ${event}`));
    }
  }
 
  // Performance monitoring
  perf(operation, duration, details = {}) {
    this.debug(`Performance: ${operation} took ${duration}ms`, {
      operation,
      duration,
      ...details
    });
  }
 
  // API request logging
  apiRequest(method, url, status, duration) {
    this.debug(`API ${method} ${url} -> ${status} (${duration}ms)`, {
      type: 'api_request',
      method,
      url,
      status,
      duration
    });
  }
 
  // Command execution logging
  command(cmd, args, success) {
    this.info(`Command executed: ${cmd}`, {
      type: 'command_execution',
      command: cmd,
      arguments: args,
      success
    });
  }
}
 
// Create singleton instance
const logger = new Logger();
 
module.exports = logger;