xypriss-security
Version:
XyPriss Security is an advanced JavaScript security library designed for enterprise applications. It provides military-grade encryption, secure data structures, quantum-resistant cryptography, and comprehensive security utilities for modern web applicatio
247 lines (244 loc) • 7.25 kB
JavaScript
'use strict';
/**
* XyPrissSecurity - Fortified Function Logger
* Specialized logging system for fortified function operations
*/
exports.LogLevel = void 0;
(function (LogLevel) {
LogLevel[LogLevel["DEBUG"] = 0] = "DEBUG";
LogLevel[LogLevel["INFO"] = 1] = "INFO";
LogLevel[LogLevel["WARN"] = 2] = "WARN";
LogLevel[LogLevel["ERROR"] = 3] = "ERROR";
LogLevel[LogLevel["CRITICAL"] = 4] = "CRITICAL";
})(exports.LogLevel || (exports.LogLevel = {}));
/**
* High-performance logger optimized for fortified function operations
*/
class FortifiedLogger {
constructor() {
this.logBuffer = [];
this.maxBufferSize = 1000;
this.flushInterval = 5000; // 5 seconds
this.currentLogLevel = exports.LogLevel.INFO;
this.metricsBuffer = [];
this.auditBuffer = [];
this.setupAutoFlush();
}
static getInstance() {
if (!FortifiedLogger.instance) {
FortifiedLogger.instance = new FortifiedLogger();
}
return FortifiedLogger.instance;
}
/**
* Set the minimum log level
*/
setLogLevel(level) {
this.currentLogLevel = level;
}
/**
* Log a debug message
*/
debug(category, message, metadata) {
this.log(exports.LogLevel.DEBUG, category, message, metadata);
}
/**
* Log an info message
*/
info(category, message, metadata) {
this.log(exports.LogLevel.INFO, category, message, metadata);
}
/**
* Log a warning message
*/
warn(category, message, metadata) {
this.log(exports.LogLevel.WARN, category, message, metadata);
}
/**
* Log an error message
*/
error(category, message, metadata) {
this.log(exports.LogLevel.ERROR, category, message, metadata);
}
/**
* Log a critical message
*/
critical(category, message, metadata) {
this.log(exports.LogLevel.CRITICAL, category, message, metadata);
}
/**
* Log performance metrics
*/
logMetrics(metrics, executionId) {
this.metricsBuffer.push(metrics);
this.log(exports.LogLevel.DEBUG, "PERFORMANCE", "Metrics recorded", {
executionTime: metrics.executionTime,
memoryUsage: metrics.memoryUsage,
cacheHitRate: metrics.cacheHitRate,
executionId,
});
// Keep buffer size manageable
if (this.metricsBuffer.length > this.maxBufferSize) {
this.metricsBuffer = this.metricsBuffer.slice(-this.maxBufferSize / 2);
}
}
/**
* Log audit entry
*/
logAudit(entry) {
this.auditBuffer.push(entry);
this.log(exports.LogLevel.INFO, "AUDIT", "Execution audited", {
executionId: entry.executionId,
success: entry.success,
executionTime: entry.executionTime,
memoryUsage: entry.memoryUsage,
});
// Keep buffer size manageable
if (this.auditBuffer.length > this.maxBufferSize) {
this.auditBuffer = this.auditBuffer.slice(-this.maxBufferSize / 2);
}
}
/**
* Log optimization suggestion
*/
logOptimization(suggestion) {
this.log(exports.LogLevel.INFO, "OPTIMIZATION", suggestion.description, {
type: suggestion.type,
priority: suggestion.priority,
expectedImprovement: suggestion.expectedImprovement,
implementation: suggestion.implementation,
});
}
/**
* Log execution event
*/
logExecution(executionId, event, duration, metadata) {
this.log(exports.LogLevel.DEBUG, "EXECUTION", event, {
executionId,
duration,
...metadata,
});
}
/**
* Log cache operation
*/
logCache(operation, key, metadata) {
this.log(exports.LogLevel.DEBUG, "CACHE", `Cache ${operation}`, {
key: key.substring(0, 16) + "...",
operation,
...metadata,
});
}
/**
* Core logging method
*/
log(level, category, message, metadata) {
if (level < this.currentLogLevel) {
return;
}
const entry = {
timestamp: performance.now(),
level,
category,
message,
metadata,
};
this.logBuffer.push(entry);
// Immediate console output for errors and critical messages
if (level >= exports.LogLevel.ERROR) {
this.outputToConsole(entry);
}
// Auto-flush if buffer is getting full
if (this.logBuffer.length >= this.maxBufferSize) {
this.flush();
}
}
/**
* Output log entry to console
*/
outputToConsole(entry) {
const levelName = exports.LogLevel[entry.level];
const timestamp = new Date(entry.timestamp).toISOString();
const message = `[${timestamp}] [${levelName}] [${entry.category}] ${entry.message}`;
switch (entry.level) {
case exports.LogLevel.DEBUG:
console.debug(message, entry.metadata);
break;
case exports.LogLevel.INFO:
console.info(message, entry.metadata);
break;
case exports.LogLevel.WARN:
console.warn(message, entry.metadata);
break;
case exports.LogLevel.ERROR:
console.error(message, entry.metadata);
break;
case exports.LogLevel.CRITICAL:
console.error(`🚨 CRITICAL: ${message}`, entry.metadata);
break;
}
}
/**
* Flush log buffer
*/
flush() {
if (this.logBuffer.length === 0) {
return;
}
// Wemight send logs to a service
// For now, we'll just output to console for non-error levels
this.logBuffer
.filter((entry) => entry.level < exports.LogLevel.ERROR)
.forEach((entry) => this.outputToConsole(entry));
this.logBuffer = [];
}
/**
* Setup automatic flushing
*/
setupAutoFlush() {
this.flushTimer = setInterval(() => {
this.flush();
}, this.flushInterval);
}
/**
* Get recent metrics
*/
getRecentMetrics(count = 10) {
return this.metricsBuffer.slice(-count);
}
/**
* Get recent audit entries
*/
getRecentAuditEntries(count = 10) {
return this.auditBuffer.slice(-count);
}
/**
* Get recent log entries
*/
getRecentLogs(count = 50) {
return this.logBuffer.slice(-count);
}
/**
* Clear all buffers
*/
clear() {
this.logBuffer = [];
this.metricsBuffer = [];
this.auditBuffer = [];
}
/**
* Destroy logger and cleanup
*/
destroy() {
if (this.flushTimer) {
clearInterval(this.flushTimer);
}
this.flush();
this.clear();
}
}
// Export singleton instance
const fortifiedLogger = FortifiedLogger.getInstance();
exports.FortifiedLogger = FortifiedLogger;
exports.fortifiedLogger = fortifiedLogger;
//# sourceMappingURL=fortified-logger.js.map