@trap_stevo/star-vault
Version:
Unleash the future of data management with the ultimate platform for secure, scalable, and dynamic data operations. Power the next generation of applications by combining advanced encryption, revolutionary real-time querying, and seamless synchronization
134 lines (133 loc) • 5.54 kB
JavaScript
;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var EngineUtilityManager = require("../HUDManagers/EngineUtilityManager");
var path = require("path");
var fs = require("fs");
var StarLogger = /*#__PURE__*/function () {
function StarLogger(logPath) {
var maxLogSizeBytes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5 * 1024 * 1024;
var retention = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "30d";
var dirMode = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 448;
var fileMode = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 384;
_classCallCheck(this, StarLogger);
this.logPath = logPath;
this.maxLogSizeBytes = maxLogSizeBytes;
this.retentionMs = EngineUtilityManager.convertDuration(retention);
this.fileMode = fileMode;
this.dirMode = dirMode;
if (!fs.existsSync(logPath)) {
fs.mkdirSync(logPath, {
recursive: true,
mode: this.dirMode
});
}
}
return _createClass(StarLogger, [{
key: "logWrite",
value: function logWrite(collection, record) {
var clientInfo = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var action = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : "WRITE";
var logFile = path.join(this.logPath, "".concat(collection, ".log"));
var dir = path.dirname(logFile);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, {
recursive: true,
mode: this.dirMode
});
}
this.checkLogRotation(logFile);
var logEntry = {
timestamp: new Date().toISOString(),
client: clientInfo,
data: record,
action: action
};
fs.appendFileSync(logFile, JSON.stringify(logEntry) + "\n", {
mode: this.fileMode
});
}
}, {
key: "recoverWrites",
value: function recoverWrites(collection) {
var logFile = path.join(this.logPath, "".concat(collection, ".log"));
if (!fs.existsSync(logFile)) {
return [];
}
var logs = fs.readFileSync(logFile, "utf-8").split("\n").filter(Boolean);
return logs.map(function (log) {
return JSON.parse(log);
});
}
}, {
key: "logSystemAction",
value: function logSystemAction(action, collection, details) {
var clientInfo = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
var logFile = path.join(this.logPath, "system.log");
var dir = path.dirname(logFile);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, {
recursive: true,
mode: this.dirMode
});
}
var logEntry = {
timestamp: new Date().toISOString(),
client: clientInfo,
action: action,
collection: collection,
details: details
};
fs.appendFileSync(logFile, JSON.stringify(logEntry) + "\n", {
mode: this.fileMode
});
}
}, {
key: "clearLog",
value: function clearLog(collection) {
var logFile = path.join(this.logPath, "".concat(collection, ".log"));
if (fs.existsSync(logFile)) {
fs.unlinkSync(logFile);
}
}
}, {
key: "checkLogRotation",
value: function checkLogRotation(logFile) {
if (fs.existsSync(logFile)) {
var stats = fs.statSync(logFile);
if (stats.size > this.maxLogSizeBytes) {
this.rotateLog(logFile);
}
}
}
}, {
key: "rotateLog",
value: function rotateLog(logFile) {
var timestamp = new Date().toISOString().replace(/[:.]/g, "-");
var archiveFile = "".concat(logFile, ".").concat(timestamp, ".archived");
fs.renameSync(logFile, archiveFile);
fs.chmodSync(archiveFile, this.fileMode);
this.cleanupOldLogs();
}
}, {
key: "cleanupOldLogs",
value: function cleanupOldLogs() {
var _this = this;
var files = fs.readdirSync(this.logPath);
var now = Date.now();
files.forEach(function (file) {
var filePath = path.join(_this.logPath, file);
var stats = fs.statSync(filePath);
if (now - stats.mtimeMs > _this.retentionMs) {
fs.unlinkSync(filePath);
}
});
}
}]);
}();
;
module.exports = StarLogger;