z-mmap-logger
Version:
High-performance logging for NestJS using memory-mapped files
119 lines (118 loc) • 4.54 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createZMmapLogger = exports.ZMmapLogger = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
class ZMmapLogger {
constructor(options) {
var _a, _b;
this.offset = 0;
this.maxSize = 10 * 1024 * 1024; // 10MB
const maxSize = (_a = options === null || options === void 0 ? void 0 : options.maxSize) !== null && _a !== void 0 ? _a : this.maxSize;
this.maxSize = maxSize;
const logDir = (_b = options === null || options === void 0 ? void 0 : options.logDir) !== null && _b !== void 0 ? _b : path.join(process.cwd(), '.z-logs');
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir, { recursive: true });
}
this.logFile = path.join(logDir, 'logs.mmap');
// Create or open the file
this.fd = fs.openSync(this.logFile, 'a+');
const stats = fs.fstatSync(this.fd);
if (stats.size < this.maxSize) {
// Pre-allocate buffer space for better performance
fs.closeSync(this.fd);
fs.truncateSync(this.logFile, this.maxSize);
this.fd = fs.openSync(this.logFile, 'r+');
}
// Create a buffer that maps to the file
this.buffer = Buffer.alloc(this.maxSize);
// Read the file into buffer
fs.readSync(this.fd, this.buffer, 0, 4, 0);
// Initialize offset if file is new
if (this.buffer.readUInt32LE(0) === 0) {
this.buffer.writeUInt32LE(4, 0); // Offset starts after the header (4 bytes)
this.offset = 4;
}
else {
this.offset = this.buffer.readUInt32LE(0);
}
}
log(message, context) {
this.writeLog({ level: 'log', message, context });
}
error(message, trace, context) {
this.writeLog({
level: 'error',
message,
trace,
context
});
}
warn(message, context) {
this.writeLog({ level: 'warn', message, context });
}
debug(message, context) {
this.writeLog({ level: 'debug', message, context });
}
verbose(message, context) {
this.writeLog({ level: 'verbose', message, context });
}
/**
* Write a log entry to the memory-mapped file
*/
writeLog(log) {
// Create log entry with timestamp
const logEntry = {
timestamp: new Date().toISOString(),
...log,
};
const logString = JSON.stringify(logEntry) + '\n';
const logBuffer = Buffer.from(logString);
// Reset offset if we're at the end of the buffer
if (this.offset + logBuffer.length > this.maxSize) {
this.offset = 4; // Reset to start (after header)
}
// Write the log to our buffer
logBuffer.copy(this.buffer, this.offset);
this.offset += logBuffer.length;
// Update the offset in the header
this.buffer.writeUInt32LE(this.offset, 0);
// Sync the buffer to the file
fs.writeSync(this.fd, this.buffer, 0, this.offset, 0);
}
/**
* Close the file descriptor when the logger is no longer needed
*/
close() {
fs.closeSync(this.fd);
}
}
exports.ZMmapLogger = ZMmapLogger;
// Export a factory function for easy creation
function createZMmapLogger(options) {
return new ZMmapLogger(options);
}
exports.createZMmapLogger = createZMmapLogger;