@ai-capabilities-suite/mcp-debugger-core
Version:
Core debugging engine for Node.js and TypeScript applications. Provides Inspector Protocol integration, breakpoint management, variable inspection, execution control, profiling, hang detection, and source map support.
207 lines • 5.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StructuredLogger = exports.LogLevel = void 0;
const crypto_1 = require("crypto");
/**
* Log levels for structured logging
*/
var LogLevel;
(function (LogLevel) {
LogLevel["DEBUG"] = "debug";
LogLevel["INFO"] = "info";
LogLevel["WARN"] = "warn";
LogLevel["ERROR"] = "error";
})(LogLevel || (exports.LogLevel = LogLevel = {}));
/**
* Structured logger for enterprise observability
* Provides JSON-formatted logging with correlation IDs and context
*/
class StructuredLogger {
constructor(config = {}) {
this.logBuffer = [];
this.maxBufferSize = 1000;
this.config = {
minLevel: config.minLevel || LogLevel.INFO,
enableConsole: config.enableConsole !== false,
enableJson: config.enableJson !== false,
includeTimestamp: config.includeTimestamp !== false,
};
}
/**
* Generate a correlation ID for request tracing
*/
generateCorrelationId() {
return (0, crypto_1.randomBytes)(8).toString('hex');
}
/**
* Check if a log level should be logged based on configuration
*/
shouldLog(level) {
const levels = [
LogLevel.DEBUG,
LogLevel.INFO,
LogLevel.WARN,
LogLevel.ERROR,
];
const minLevelIndex = levels.indexOf(this.config.minLevel);
const currentLevelIndex = levels.indexOf(level);
return currentLevelIndex >= minLevelIndex;
}
/**
* Create a log entry
*/
createLogEntry(level, message, context) {
const entry = {
timestamp: this.config.includeTimestamp ? new Date().toISOString() : '',
level,
message,
};
if (context?.correlationId) {
entry.correlationId = context.correlationId;
}
if (context?.sessionId) {
entry.sessionId = context.sessionId;
}
if (context?.operationType) {
entry.operationType = context.operationType;
}
if (context?.additionalContext) {
entry.context = context.additionalContext;
}
if (context?.error) {
entry.error = {
message: context.error.message,
stack: context.error.stack,
code: context.error.code,
};
}
return entry;
}
/**
* Format log entry for output
*/
formatLogEntry(entry) {
if (this.config.enableJson) {
return JSON.stringify(entry);
}
// Human-readable format
const parts = [];
if (entry.timestamp) {
parts.push(`[${entry.timestamp}]`);
}
parts.push(`[${entry.level.toUpperCase()}]`);
if (entry.correlationId) {
parts.push(`[${entry.correlationId}]`);
}
if (entry.sessionId) {
parts.push(`[session:${entry.sessionId}]`);
}
if (entry.operationType) {
parts.push(`[op:${entry.operationType}]`);
}
parts.push(entry.message);
if (entry.context) {
parts.push(JSON.stringify(entry.context));
}
if (entry.error) {
parts.push(`Error: ${entry.error.message}`);
if (entry.error.stack) {
parts.push(`\n${entry.error.stack}`);
}
}
return parts.join(' ');
}
/**
* Write log entry to output
*/
writeLog(entry) {
// Add to buffer
this.logBuffer.push(entry);
if (this.logBuffer.length > this.maxBufferSize) {
this.logBuffer.shift();
}
// Write to console if enabled
if (this.config.enableConsole) {
const formatted = this.formatLogEntry(entry);
switch (entry.level) {
case LogLevel.DEBUG:
console.debug(formatted);
break;
case LogLevel.INFO:
console.info(formatted);
break;
case LogLevel.WARN:
console.warn(formatted);
break;
case LogLevel.ERROR:
console.error(formatted);
break;
}
}
}
/**
* Log a debug message
*/
debug(message, context) {
if (!this.shouldLog(LogLevel.DEBUG))
return;
const entry = this.createLogEntry(LogLevel.DEBUG, message, context);
this.writeLog(entry);
}
/**
* Log an info message
*/
info(message, context) {
if (!this.shouldLog(LogLevel.INFO))
return;
const entry = this.createLogEntry(LogLevel.INFO, message, context);
this.writeLog(entry);
}
/**
* Log a warning message
*/
warn(message, context) {
if (!this.shouldLog(LogLevel.WARN))
return;
const entry = this.createLogEntry(LogLevel.WARN, message, context);
this.writeLog(entry);
}
/**
* Log an error message
*/
error(message, context) {
if (!this.shouldLog(LogLevel.ERROR))
return;
const entry = this.createLogEntry(LogLevel.ERROR, message, context);
this.writeLog(entry);
}
/**
* Get recent log entries
*/
getRecentLogs(count) {
if (count) {
return this.logBuffer.slice(-count);
}
return [...this.logBuffer];
}
/**
* Clear log buffer
*/
clearLogs() {
this.logBuffer = [];
}
/**
* Set minimum log level
*/
setMinLevel(level) {
this.config.minLevel = level;
}
/**
* Get current configuration
*/
getConfig() {
return { ...this.config };
}
}
exports.StructuredLogger = StructuredLogger;
//# sourceMappingURL=structured-logger.js.map