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

18
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,18 @@
{
// Verwendet IntelliSense zum Ermitteln möglicher Attribute.
// Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen.
// Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Programm starten",
"program": "${workspaceFolder}\\out\\test.js",
"preLaunchTask": "build",
"outFiles": [
"${workspaceFolder}/out/**/*.js"
]
}
]
}

16
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,16 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"identifier": "builld",
"label": "build",
"problemMatcher": [
"$tsc"
]
}
]
}

4
out/index.d.ts vendored
View File

@ -2,9 +2,11 @@
import { EventEmitter } from "events";
export declare class Logging {
private static logFileLocation;
private static stdout;
static stdout: boolean;
private static fileStream;
private static errorStream;
private static fileSize;
private static errorSize;
private static writing;
private static queue;
static events: EventEmitter;

View File

@ -27,6 +27,7 @@ const BgBlue = "\x1b[44m";
const BgMagenta = "\x1b[45m";
const BgCyan = "\x1b[46m";
const BgWhite = "\x1b[47m";
const maxFileSize = 500000000;
class Logging {
static config(logfolder, stdout) {
this.logFileLocation = logfolder;
@ -105,8 +106,8 @@ class Logging {
index = m.indexOf("\x1b");
}
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);
}
@ -116,35 +117,60 @@ class Logging {
Logging.queue.push({ message: message.replace("\n", " "), error: error });
Logging.checkQueue();
}
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", () => {
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);
}
}
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) {
@ -161,10 +187,26 @@ class Logging {
resolve();
});
});
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);
@ -173,10 +215,74 @@ class Logging {
}
Logging.logFileLocation = "./logs/";
Logging.stdout = true;
Logging.fileSize = 0;
Logging.errorSize = 0;
Logging.writing = false;
Logging.queue = new Array();
Logging.events = new events_1.EventEmitter();
exports.Logging = Logging;
function fsUnlink(path) {
return new Promise((resolve, reject) => {
fs.unlink(path, (err) => {
if (err)
reject(err);
else
resolve();
});
});
}
function fsStat(path) {
return new Promise((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((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();

File diff suppressed because one or more lines are too long

View File

@ -1,9 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("./index");
const crypto_1 = require("crypto");
index_1.Logging.log("test");
index_1.Logging.log("i", "am", { a: "an" }, 1000);
index_1.Logging.error(new Error("fehler 001"));
index_1.Logging.debug("Some Debug infos");
index_1.Logging.errorMessage("i", "am", "an", "error");
index_1.Logging.stdout = false;
for (let i = 0; i < 7000; i++) {
index_1.Logging.log(crypto_1.randomBytes(50000).toString("hex"));
}
//# sourceMappingURL=test.js.map

View File

@ -1 +1 @@
{"version":3,"file":"test.js","sourceRoot":"","sources":["../src/test.ts"],"names":[],"mappings":";;AAAA,mCAAkC;AAElC,eAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;AACnB,eAAO,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC;AAC1C,eAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC;AACvC,eAAO,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAC;AAClC,eAAO,CAAC,YAAY,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC"}
{"version":3,"file":"test.js","sourceRoot":"","sources":["../src/test.ts"],"names":[],"mappings":";;AAAA,mCAAkC;AAClC,mCAAqC;AAErC,eAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;AACnB,eAAO,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC;AAC1C,eAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC;AACvC,eAAO,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAC;AAClC,eAAO,CAAC,YAAY,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;AAC/C,eAAO,CAAC,MAAM,GAAG,KAAK,CAAC;AACvB,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC;IAC7B,eAAO,CAAC,GAAG,CAAC,oBAAW,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAA;AAClD,CAAC"}

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.stdout = false;
for (let i = 0; i < 7000; i++) {
Logging.log(randomBytes(50000).toString("hex"))
}