1
0
mirror of https://git.hibas.dev/OpenServer/NodeLogging.git synced 2025-07-01 12:41:11 +00:00

Addic file size limits

This commit is contained in:
hibas123
2018-02-18 16:00:16 +01:00
parent 5aaa4f6961
commit 2ccf45ff86
9 changed files with 315 additions and 52 deletions

View File

@ -29,12 +29,16 @@ const BgMagenta = "\x1b[45m"
const BgCyan = "\x1b[46m"
const BgWhite = "\x1b[47m"
const maxFileSize = 500000000;
export class Logging {
private static logFileLocation: string = "./logs/";
private static stdout: boolean = true;
public static stdout: boolean = true;
private static fileStream: fs.WriteStream;
private static errorStream: fs.WriteStream;
private static fileSize: number = 0;
private static errorSize: number = 0;
private static writing = false;
private static queue = new Array<{ message: string, error: boolean }>();
@ -123,8 +127,8 @@ export class Logging {
}
if (this.logFileLocation) {
if (!this.fileStream || !this.errorStream) {
await Logging.initializeFile();
if ((!this.fileStream || !this.errorStream) && !this.writing) {
Logging.initializeFile();
}
Logging.writeMessageToFile(m, type === LoggingTypes.Error);
}
@ -136,31 +140,55 @@ export class Logging {
Logging.checkQueue();
}
private static checkQueue() {
if (Logging.writing) return;
if (Logging.queue.length <= 0) return;
Logging.writing = true;
var message = Logging.queue[0];
Logging.fileStream.write(message.message + "\n", () => {
if (message.error) {
Logging.errorStream.write(message.message + "\n", () => {
private static async checkQueue() {
try {
if (Logging.writing) return;
if (Logging.queue.length <= 0) return;
Logging.writing = true;
var message = Logging.queue[0];
message.message += "\n";
let data = new Buffer(message.message, "utf8");
if (data.byteLength < maxFileSize && data.byteLength + Logging.fileSize > maxFileSize) {
Logging.fileStream.close();
if (await fsExists(this.logFileLocation + "all.log.old"))
await fsUnlink(this.logFileLocation + "all.log.old");
await fsMove(this.logFileLocation + "all.log", this.logFileLocation + "all.log.old")
Logging.fileStream = fs.createWriteStream(this.logFileLocation + "all.log");
Logging.fileSize = 0;
}
Logging.fileSize += data.byteLength;
Logging.fileStream.write(data, async () => {
if (message.error) {
if (data.byteLength < maxFileSize && data.byteLength + Logging.errorSize > maxFileSize) {
Logging.errorStream.close();
if (await fsExists(this.logFileLocation + "error.log.old"))
await fsUnlink(this.logFileLocation + "error.log.old");
await fsMove(this.logFileLocation + "error.log", this.logFileLocation + "error.log.old")
Logging.errorStream = fs.createWriteStream(this.logFileLocation + "error.log");
Logging.errorSize = 0;
}
Logging.errorSize += data.byteLength;
Logging.errorStream.write(data, () => {
Logging.queue.splice(Logging.queue.indexOf(message), 1);
Logging.writing = false;
Logging.checkQueue();
});
} else {
Logging.queue.splice(Logging.queue.indexOf(message), 1);
Logging.writing = false;
Logging.checkQueue();
});
} else {
Logging.queue.splice(Logging.queue.indexOf(message), 1);
Logging.writing = false;
Logging.checkQueue();
}
});
}
});
} catch (e) {
console.log(e)
}
}
private static async initializeFile() {
if (this.fileStream && this.errorStream) return;
if (!this.logFileLocation) return;
this.writing = true;
try {
var exists = util.promisify(fs.exists);
await new Promise((resolve, reject) => {
fs.exists(this.logFileLocation, (exists) => {
if (!exists) {
@ -175,16 +203,99 @@ export class Logging {
});
});
Logging.fileStream = fs.createWriteStream(this.logFileLocation + "all.log", { flags: "a" });
Logging.fileStream.write("\n");
Logging.errorStream = fs.createWriteStream(this.logFileLocation + "error.log", { flags: "a" });
Logging.errorStream.write("\n");
if (await fsExists(this.logFileLocation + "all.log")) {
if (await fsExists(this.logFileLocation + "all.log.old"))
await fsUnlink(this.logFileLocation + "all.log.old");
await fsMove(this.logFileLocation + "all.log", this.logFileLocation + "all.log.old")
}
if (await fsExists(this.logFileLocation + "error.log")) {
let stats = await fsStat(this.logFileLocation + "error.log")
if (stats.size > maxFileSize) {
if (await fsExists(this.logFileLocation + "error.log.old"))
await fsUnlink(this.logFileLocation + "error.log.old");
await fsMove(this.logFileLocation + "error.log", this.logFileLocation + "error.log.old")
} else {
this.errorSize = stats.size;
}
}
this.fileStream = fs.createWriteStream(this.logFileLocation + "all.log", { flags: "a" });
this.errorStream = fs.createWriteStream(this.logFileLocation + "error.log", { flags: "a" });
this.writing = false;
this.checkQueue();
} catch (e) {
console.log(e);
}
}
}
function fsUnlink(path) {
return new Promise((resolve, reject) => {
fs.unlink(path, (err) => {
if (err) reject(err);
else resolve();
})
})
}
function fsStat(path: string) {
return new Promise<fs.Stats>((resolve, reject) => {
fs.stat(path, (err, stats) => {
if (err) reject(err);
else resolve(stats);
})
})
}
function fsMove(oldPath, newPath) {
return new Promise((resolve, reject) => {
let callback = (err?) => {
if (err) reject(err)
else resolve()
}
fs.rename(oldPath, newPath, function (err) {
if (err) {
if (err.code === 'EXDEV') {
copy();
} else {
callback(err)
}
return;
}
callback()
});
function copy() {
var readStream = fs.createReadStream(oldPath);
var writeStream = fs.createWriteStream(newPath);
readStream.on('error', callback);
writeStream.on('error', callback);
readStream.on('close', function () {
fs.unlink(oldPath, callback);
});
readStream.pipe(writeStream);
}
})
}
function fsExists(path) {
return new Promise<boolean>((resolve, reject) => {
fs.exists(path, resolve);
});
}
function fsMkDir(path) {
return new Promise((resolve, reject) => {
fs.exists(path, resolve);
});
}
function _getCallerFile() {
try {
var err = new Error();

View File

@ -1,7 +1,12 @@
import { Logging } from "./index";
import { randomBytes } from "crypto";
Logging.log("test")
Logging.log("i", "am", { a: "an" }, 1000);
Logging.error(new Error("fehler 001"));
Logging.debug("Some Debug infos");
Logging.errorMessage("i", "am", "an", "error");
Logging.errorMessage("i", "am", "an", "error");
Logging.stdout = false;
for (let i = 0; i < 7000; i++) {
Logging.log(randomBytes(50000).toString("hex"))
}