Files
svn/tech/client/交接文档/谭健交接文档/Script/LogServer/handler/LogSave/LogSave.js
2025-08-04 10:46:00 +08:00

104 lines
3.8 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const FILE_CLOSE_COUNTDOWN = 60 * 10;
const FILE_CLOSE_CHECK_DELAY = 1;
const LOG_LEVEL_TITLE = [
null, 'LOG', 'WARN', 'ERROR'
];
class LogSave {
constructor(config) {
this.fileDir = null;
this.timer = null;
this.fileCache = null;
this.fileCache = {};
if (config.path)
this.fileDir = config.path;
else
this.fileDir = path_1.default.join(__dirname, '..', 'log');
}
mkdir(dir) {
if (!fs_1.default.existsSync(dir)) {
let parentDir = path_1.default.dirname(dir);
this.mkdir(parentDir);
fs_1.default.mkdirSync(dir);
}
}
onServerStart() {
this.timer = setInterval(() => this.checkFileCache(), FILE_CLOSE_CHECK_DELAY * 1000);
}
onServerStop() {
this.timer && clearInterval(this.timer);
this.timer = null;
this.checkFileCache(true);
}
onClientConnected(id) {
}
onClientLogMessage(id, logs) {
const fileStream = this.openFile(id, "log" /* Log */);
logs.forEach(log => {
const date = new Date(log.time);
const timeString = `${date.getHours()}:${('0' + date.getMinutes()).slice(-2)}:${('0' + date.getSeconds()).slice(-2)}`;
fileStream.write(`[${timeString}][${LOG_LEVEL_TITLE[log.level]}] ${log.text}\n`);
});
}
onClientErrorMessage(id, error) {
const fileStream = this.openFile(id, "error" /* Error */);
const date = new Date(error.time);
const timeString = `${date.getHours()}:${('0' + date.getMinutes()).slice(-2)}:${('0' + date.getSeconds()).slice(-2)}`;
let errorString = `time: ${timeString}\n`;
for (const key in error) {
key !== 'time' && (errorString += `${key}: ${error[key]}\n`);
}
errorString += '\n';
fileStream.write(errorString);
}
onClientDisconnect(id) {
this.closeFile(id, "log" /* Log */);
this.closeFile(id, "error" /* Error */);
}
getFilePath(id, fileType) {
const date = new Date();
const dateString = `${date.getFullYear()}-${('0' + (date.getMonth() + 1)).slice(-2)}-${('0' + date.getDate()).slice(-2)}`;
const dirPath = path_1.default.join(this.fileDir, dateString, fileType);
this.mkdir(dirPath);
return path_1.default.join(dirPath, `${id}.txt`);
}
openFile(id, fileType) {
const fileName = this.getFilePath(id, fileType);
let fileData = this.fileCache[fileName];
if (!fileData) {
const stream = fs_1.default.createWriteStream(fileName, { flags: 'a' });
fileData = this.fileCache[fileName] = { countdown: FILE_CLOSE_COUNTDOWN, stream };
}
fileData.countdown = FILE_CLOSE_COUNTDOWN;
return fileData.stream;
}
closeFile(id, fileType) {
const fileName = this.getFilePath(id, fileType);
const fileData = this.fileCache[fileName];
if (!fileData)
return;
fileData.stream.close();
this.fileCache[fileName] = null;
delete this.fileCache[fileName];
}
checkFileCache(focusClose = false) {
for (const fileName in this.fileCache) {
const fileData = this.fileCache[fileName];
if (!fileData)
continue;
if (!focusClose && fileData.countdown-- > 0)
continue;
fileData.stream.close();
this.fileCache[fileName] = null;
delete this.fileCache[fileName];
}
}
}
exports.default = LogSave;