@ithena-one/mcp-governance
Version:
Governance layer (Identity, RBAC, Credentials, Audit, Logging, Tracing) for Model Context Protocol (MCP) servers.
106 lines • 3.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultLogger = exports.ConsoleLogger = void 0;
/**
* A simple logger implementation that writes structured JSON to the console.
*/
class ConsoleLogger {
constructor(baseContext = {}, minLevel = 'info') {
this.levelMap = {
debug: 10,
info: 20,
warn: 30,
error: 40,
};
this.baseContext = baseContext;
this.minLevel = minLevel;
}
async initialize() {
// Optional: Log initialization
this.info("ConsoleLogger initialized");
}
shouldLog(level) {
return this.levelMap[level] >= this.levelMap[this.minLevel];
}
getConsoleMethod(level) {
let method;
switch (level) {
case 'debug':
method = typeof console.debug === 'function' ? console.debug : console.log;
break;
case 'info':
method = typeof console.info === 'function' ? console.info : console.log;
break;
case 'warn':
method = typeof console.warn === 'function' ? console.warn : console.log;
break;
case 'error':
method = typeof console.error === 'function' ? console.error : console.log;
break;
default:
method = console.log;
}
if (typeof method !== 'function') {
method = console.log;
}
return method.bind(console);
}
log(level, message, context, error) {
if (!this.shouldLog(level)) {
return;
}
const logEntry = {
level,
timestamp: new Date().toISOString(),
message,
...this.baseContext,
...context,
};
if (error) {
if (error instanceof Error) {
logEntry.error = {
message: error.message,
name: error.name,
stack: error.stack,
};
}
else {
logEntry.error = error;
}
}
const jsonString = JSON.stringify(logEntry);
const consoleMethod = this.getConsoleMethod(level);
try {
consoleMethod(jsonString);
}
catch (stringifyError) {
// Fallback if stringify fails (e.g., circular reference)
console.error("Failed to stringify log entry, logging raw:", stringifyError, logEntry);
}
}
debug(message, context) {
this.log('debug', message, context);
}
info(message, context) {
this.log('info', message, context);
}
warn(message, context) {
this.log('warn', message, context);
}
error(message, error, context) {
this.log('error', message, context, error);
}
child(bindings) {
// Create a new logger instance with merged context
return new ConsoleLogger({ ...this.baseContext, ...bindings }, this.minLevel);
}
async shutdown() {
// Optional: Log shutdown
this.info("ConsoleLogger shutting down");
// No specific action needed for console
}
}
exports.ConsoleLogger = ConsoleLogger;
/** Default logger instance */
exports.defaultLogger = new ConsoleLogger();
//# sourceMappingURL=logger.js.map