1
0
mirror of https://git.stamm.me/OpenServer/NodeLogging.git synced 2024-11-25 08:19:23 +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

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"; import { EventEmitter } from "events";
export declare class Logging { export declare class Logging {
private static logFileLocation; private static logFileLocation;
private static stdout; static stdout: boolean;
private static fileStream; private static fileStream;
private static errorStream; private static errorStream;
private static fileSize;
private static errorSize;
private static writing; private static writing;
private static queue; private static queue;
static events: EventEmitter; static events: EventEmitter;

View File

@ -27,6 +27,7 @@ const BgBlue = "\x1b[44m";
const BgMagenta = "\x1b[45m"; const BgMagenta = "\x1b[45m";
const BgCyan = "\x1b[46m"; const BgCyan = "\x1b[46m";
const BgWhite = "\x1b[47m"; const BgWhite = "\x1b[47m";
const maxFileSize = 500000000;
class Logging { class Logging {
static config(logfolder, stdout) { static config(logfolder, stdout) {
this.logFileLocation = logfolder; this.logFileLocation = logfolder;
@ -105,8 +106,8 @@ class Logging {
index = m.indexOf("\x1b"); index = m.indexOf("\x1b");
} }
if (this.logFileLocation) { if (this.logFileLocation) {
if (!this.fileStream || !this.errorStream) { if ((!this.fileStream || !this.errorStream) && !this.writing) {
await Logging.initializeFile(); Logging.initializeFile();
} }
Logging.writeMessageToFile(m, type === LoggingTypes.Error); Logging.writeMessageToFile(m, type === LoggingTypes.Error);
} }
@ -116,16 +117,37 @@ class Logging {
Logging.queue.push({ message: message.replace("\n", " "), error: error }); Logging.queue.push({ message: message.replace("\n", " "), error: error });
Logging.checkQueue(); Logging.checkQueue();
} }
static checkQueue() { static async checkQueue() {
try {
if (Logging.writing) if (Logging.writing)
return; return;
if (Logging.queue.length <= 0) if (Logging.queue.length <= 0)
return; return;
Logging.writing = true; Logging.writing = true;
var message = Logging.queue[0]; var message = Logging.queue[0];
Logging.fileStream.write(message.message + "\n", () => { 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 (message.error) {
Logging.errorStream.write(message.message + "\n", () => { 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.queue.splice(Logging.queue.indexOf(message), 1);
Logging.writing = false; Logging.writing = false;
Logging.checkQueue(); Logging.checkQueue();
@ -138,13 +160,17 @@ class Logging {
} }
}); });
} }
catch (e) {
console.log(e);
}
}
static async initializeFile() { static async initializeFile() {
if (this.fileStream && this.errorStream) if (this.fileStream && this.errorStream)
return; return;
if (!this.logFileLocation) if (!this.logFileLocation)
return; return;
this.writing = true;
try { try {
var exists = util.promisify(fs.exists);
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
fs.exists(this.logFileLocation, (exists) => { fs.exists(this.logFileLocation, (exists) => {
if (!exists) { if (!exists) {
@ -161,10 +187,26 @@ class Logging {
resolve(); resolve();
}); });
}); });
Logging.fileStream = fs.createWriteStream(this.logFileLocation + "all.log", { flags: "a" }); if (await fsExists(this.logFileLocation + "all.log")) {
Logging.fileStream.write("\n"); if (await fsExists(this.logFileLocation + "all.log.old"))
Logging.errorStream = fs.createWriteStream(this.logFileLocation + "error.log", { flags: "a" }); await fsUnlink(this.logFileLocation + "all.log.old");
Logging.errorStream.write("\n"); 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) { catch (e) {
console.log(e); console.log(e);
@ -173,10 +215,74 @@ class Logging {
} }
Logging.logFileLocation = "./logs/"; Logging.logFileLocation = "./logs/";
Logging.stdout = true; Logging.stdout = true;
Logging.fileSize = 0;
Logging.errorSize = 0;
Logging.writing = false; Logging.writing = false;
Logging.queue = new Array(); Logging.queue = new Array();
Logging.events = new events_1.EventEmitter(); Logging.events = new events_1.EventEmitter();
exports.Logging = Logging; 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() { function _getCallerFile() {
try { try {
var err = new Error(); var err = new Error();

File diff suppressed because one or more lines are too long

View File

@ -1,9 +1,14 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("./index"); const index_1 = require("./index");
const crypto_1 = require("crypto");
index_1.Logging.log("test"); index_1.Logging.log("test");
index_1.Logging.log("i", "am", { a: "an" }, 1000); index_1.Logging.log("i", "am", { a: "an" }, 1000);
index_1.Logging.error(new Error("fehler 001")); index_1.Logging.error(new Error("fehler 001"));
index_1.Logging.debug("Some Debug infos"); index_1.Logging.debug("Some Debug infos");
index_1.Logging.errorMessage("i", "am", "an", "error"); 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 //# 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 BgCyan = "\x1b[46m"
const BgWhite = "\x1b[47m" const BgWhite = "\x1b[47m"
const maxFileSize = 500000000;
export class Logging { export class Logging {
private static logFileLocation: string = "./logs/"; private static logFileLocation: string = "./logs/";
private static stdout: boolean = true; public static stdout: boolean = true;
private static fileStream: fs.WriteStream; private static fileStream: fs.WriteStream;
private static errorStream: fs.WriteStream; private static errorStream: fs.WriteStream;
private static fileSize: number = 0;
private static errorSize: number = 0;
private static writing = false; private static writing = false;
private static queue = new Array<{ message: string, error: boolean }>(); private static queue = new Array<{ message: string, error: boolean }>();
@ -123,8 +127,8 @@ export class Logging {
} }
if (this.logFileLocation) { if (this.logFileLocation) {
if (!this.fileStream || !this.errorStream) { if ((!this.fileStream || !this.errorStream) && !this.writing) {
await Logging.initializeFile(); Logging.initializeFile();
} }
Logging.writeMessageToFile(m, type === LoggingTypes.Error); Logging.writeMessageToFile(m, type === LoggingTypes.Error);
} }
@ -136,14 +140,35 @@ export class Logging {
Logging.checkQueue(); Logging.checkQueue();
} }
private static checkQueue() { private static async checkQueue() {
try {
if (Logging.writing) return; if (Logging.writing) return;
if (Logging.queue.length <= 0) return; if (Logging.queue.length <= 0) return;
Logging.writing = true; Logging.writing = true;
var message = Logging.queue[0]; var message = Logging.queue[0];
Logging.fileStream.write(message.message + "\n", () => { 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 (message.error) {
Logging.errorStream.write(message.message + "\n", () => { 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.queue.splice(Logging.queue.indexOf(message), 1);
Logging.writing = false; Logging.writing = false;
Logging.checkQueue(); Logging.checkQueue();
@ -154,13 +179,16 @@ export class Logging {
Logging.checkQueue(); Logging.checkQueue();
} }
}); });
} catch (e) {
console.log(e)
}
} }
private static async initializeFile() { private static async initializeFile() {
if (this.fileStream && this.errorStream) return; if (this.fileStream && this.errorStream) return;
if (!this.logFileLocation) return; if (!this.logFileLocation) return;
this.writing = true;
try { try {
var exists = util.promisify(fs.exists);
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
fs.exists(this.logFileLocation, (exists) => { fs.exists(this.logFileLocation, (exists) => {
if (!exists) { if (!exists) {
@ -175,16 +203,99 @@ export class Logging {
}); });
}); });
Logging.fileStream = fs.createWriteStream(this.logFileLocation + "all.log", { flags: "a" }); if (await fsExists(this.logFileLocation + "all.log")) {
Logging.fileStream.write("\n"); if (await fsExists(this.logFileLocation + "all.log.old"))
Logging.errorStream = fs.createWriteStream(this.logFileLocation + "error.log", { flags: "a" }); await fsUnlink(this.logFileLocation + "all.log.old");
Logging.errorStream.write("\n"); 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) { } catch (e) {
console.log(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() { function _getCallerFile() {
try { try {
var err = new Error(); var err = new Error();

View File

@ -1,7 +1,12 @@
import { Logging } from "./index"; import { Logging } from "./index";
import { randomBytes } from "crypto";
Logging.log("test") Logging.log("test")
Logging.log("i", "am", { a: "an" }, 1000); Logging.log("i", "am", { a: "an" }, 1000);
Logging.error(new Error("fehler 001")); Logging.error(new Error("fehler 001"));
Logging.debug("Some Debug infos"); 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"))
}