feat: automatically compresses log to gzip on boot
This commit is contained in:
parent
74ac2e6051
commit
630c863b36
1 changed files with 43 additions and 7 deletions
|
@ -1,14 +1,19 @@
|
|||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const { createGzip } = require("zlib");
|
||||
|
||||
function consolefilelogger(bot, options, message) {
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const currentDate = new Date();
|
||||
const timestamp = `${currentDate.getFullYear()}-${(currentDate.getMonth() + 1)
|
||||
.toString()
|
||||
.padStart(2, "0")}-${currentDate.getDate().toString().padStart(2, "0")}`;
|
||||
const logFolder = path.join(__dirname, "../logs");
|
||||
const logFileName = `${timestamp}.log`;
|
||||
const logFileName = "latest.log";
|
||||
const logFilePath = path.join(logFolder, logFileName);
|
||||
const logStream = fs.createWriteStream(logFilePath, { flags: "a" });
|
||||
|
||||
if (!bot.Console.filelogging) return;
|
||||
|
||||
try {
|
||||
if (!fs.existsSync(logFolder)) {
|
||||
fs.mkdirSync(logFolder);
|
||||
|
@ -17,15 +22,46 @@ function consolefilelogger(bot, options, message) {
|
|||
console.warn(`Unable to create log folder: ${e}`);
|
||||
}
|
||||
|
||||
const logFilePath = path.join(logFolder, logFileName);
|
||||
const logStream = fs.createWriteStream(logFilePath, { flags: "a" });
|
||||
function compressFile(input, output) {
|
||||
const plainOutput = output.slice(0, -3);
|
||||
|
||||
console.log(bot.Console.filelogging);
|
||||
fs.renameSync(input, plainOutput);
|
||||
const gzip = createGzip();
|
||||
fs.createReadStream(plainOutput)
|
||||
.pipe(gzip)
|
||||
.pipe(fs.createWriteStream(output + ".tmp"))
|
||||
.once("finish", () => {
|
||||
fs.unlinkSync(plainOutput);
|
||||
fs.renameSync(output + ".tmp", output);
|
||||
});
|
||||
}
|
||||
|
||||
if (fs.existsSync(logFilePath)) {
|
||||
const plainName = fs
|
||||
.statSync(logFilePath)
|
||||
.ctime.toISOString()
|
||||
.split("T")[0];
|
||||
let name = plainName;
|
||||
let counter = 1;
|
||||
let newFileName = path.join(logFolder, `${name}.log.gz`);
|
||||
while (fs.existsSync(newFileName)) {
|
||||
name = `${plainName}-${counter}`;
|
||||
newFileName = path.join(logFolder, `${name}.log.gz`);
|
||||
counter++;
|
||||
}
|
||||
// if (plainName !== timestamp) {
|
||||
compressFile(logFilePath, newFileName);
|
||||
// }
|
||||
}
|
||||
|
||||
console.log(
|
||||
`File logging: ${bot.Console.filelogging ? "enabled" : "disabled"}`
|
||||
);
|
||||
if (!bot.Console.filelogging) return; // instead of using bot why not just use options cause you already defined it
|
||||
|
||||
// if (toFile) logStream.write(toWrite + '\n');
|
||||
|
||||
bot.console.filelogger = function (message) {
|
||||
bot.Console.filelogger = function (message) {
|
||||
logStream.write(message + "\n"); // toFile is not defined
|
||||
};
|
||||
//if (toConsole) console.log(toWrite);
|
||||
|
|
Loading…
Reference in a new issue