mirror of
https://git.stamm.me/OpenServer/NodeLogging.git
synced 2024-11-25 04:29:24 +00:00
Fixing bug with not closed file while moving to new destination
This commit is contained in:
parent
59c571e860
commit
687748a784
32
out/index.js
32
out/index.js
@ -61,6 +61,9 @@ class LoggingFiles {
|
|||||||
}
|
}
|
||||||
async initializeFile(new_file = false) {
|
async initializeFile(new_file = false) {
|
||||||
try {
|
try {
|
||||||
|
if (this.stream) {
|
||||||
|
this.stream.close();
|
||||||
|
}
|
||||||
const folder = path.dirname(this.file);
|
const folder = path.dirname(this.file);
|
||||||
if (folder) {
|
if (folder) {
|
||||||
if (!await fsExists(folder)) {
|
if (!await fsExists(folder)) {
|
||||||
@ -70,7 +73,7 @@ class LoggingFiles {
|
|||||||
let size = 0;
|
let size = 0;
|
||||||
if (await fsExists(this.file)) {
|
if (await fsExists(this.file)) {
|
||||||
let stats = await fsStat(this.file);
|
let stats = await fsStat(this.file);
|
||||||
if (new_file || stats.size > maxFileSize) {
|
if (new_file || stats.size >= maxFileSize) {
|
||||||
if (await fsExists(this.file + ".old"))
|
if (await fsExists(this.file + ".old"))
|
||||||
await fsUnlink(this.file + ".old");
|
await fsUnlink(this.file + ".old");
|
||||||
await fsMove(this.file, this.file + ".old");
|
await fsMove(this.file, this.file + ".old");
|
||||||
@ -99,12 +102,19 @@ class LoggingFiles {
|
|||||||
lock.release();
|
lock.release();
|
||||||
}
|
}
|
||||||
async write_to_file(data) {
|
async write_to_file(data) {
|
||||||
|
try {
|
||||||
if (data.byteLength < maxFileSize && this.size + data.byteLength > maxFileSize) {
|
if (data.byteLength < maxFileSize && this.size + data.byteLength > maxFileSize) {
|
||||||
let f = await this.initializeFile(true);
|
await this.initializeFile(true);
|
||||||
}
|
}
|
||||||
this.size += data.byteLength;
|
this.size += data.byteLength;
|
||||||
this.stream.write(data);
|
this.stream.write(data);
|
||||||
}
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
this.initializeFile(false);
|
||||||
|
this.write_to_file(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
write(data) {
|
write(data) {
|
||||||
this.queue.push(data);
|
this.queue.push(data);
|
||||||
this.checkQueue();
|
this.checkQueue();
|
||||||
@ -299,14 +309,20 @@ function fsMove(oldPath, newPath) {
|
|||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
function copy() {
|
function copy() {
|
||||||
var readStream = fs.createReadStream(oldPath);
|
fs.copyFile(oldPath, newPath, (err) => {
|
||||||
var writeStream = fs.createWriteStream(newPath);
|
if (err)
|
||||||
readStream.on('error', callback);
|
callback(err);
|
||||||
writeStream.on('error', callback);
|
else
|
||||||
readStream.on('close', function () {
|
|
||||||
fs.unlink(oldPath, callback);
|
fs.unlink(oldPath, callback);
|
||||||
});
|
});
|
||||||
readStream.pipe(writeStream);
|
// 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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
File diff suppressed because one or more lines are too long
26
out/test.js
26
out/test.js
@ -1,6 +1,7 @@
|
|||||||
"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"));
|
||||||
@ -26,9 +27,24 @@ cus2.log("Hello from custom Logger 2");
|
|||||||
cus22.log("Hello from custom Logger 22");
|
cus22.log("Hello from custom Logger 22");
|
||||||
cus2.log("Hello from custom Logger 2");
|
cus2.log("Hello from custom Logger 2");
|
||||||
index_1.Logging.console_out = false;
|
index_1.Logging.console_out = false;
|
||||||
// Logging.waitForSetup().then(() => {
|
async function benchmark(count, message_size) {
|
||||||
// for (let i = 0; i < 7000; i++) {
|
await index_1.Logging.waitForSetup();
|
||||||
// Logging.log(randomBytes(50000).toString("hex"))
|
const randData = crypto_1.randomBytes(message_size).toString("hex");
|
||||||
// }
|
const t = process.hrtime();
|
||||||
// });
|
for (let i = 0; i < count; i++) {
|
||||||
|
index_1.Logging.log(randData);
|
||||||
|
}
|
||||||
|
const diff = process.hrtime(t);
|
||||||
|
const NS_PER_SEC = 1e9;
|
||||||
|
await index_1.Logging.waitForSetup();
|
||||||
|
const ns = diff[0] * NS_PER_SEC + diff[1];
|
||||||
|
console.log(`Benchmark took ${ns / 1000000}ms for ${count} messages with a size of ${message_size} characters`);
|
||||||
|
console.log(`This is equal to ${(ns / 1000000) / count} ms per message`);
|
||||||
|
}
|
||||||
|
index_1.Logging.waitForSetup().then(async () => {
|
||||||
|
console.log("Large data benchmark:");
|
||||||
|
await benchmark(7000, 50000);
|
||||||
|
console.log("Realdata data benchmark:");
|
||||||
|
await benchmark(100000, 100);
|
||||||
|
});
|
||||||
//# sourceMappingURL=test.js.map
|
//# sourceMappingURL=test.js.map
|
@ -1 +1 @@
|
|||||||
{"version":3,"file":"test.js","sourceRoot":"","sources":["../src/test.ts"],"names":[],"mappings":";;AAAA,mCAA+C;AAG/C,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,CAA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,GAAG,CAAC,uBAAuB,CAAC,CAAA;IACpC,MAAM,SAAS,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAE7B,OAAO,CAAC,GAAG,CAAC,0BAA0B,CAAC,CAAA;IACvC,MAAM,SAAS,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;AAC/B,CAAC,CAAC,CAAC"}
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hibas123/nodelogging",
|
"name": "@hibas123/nodelogging",
|
||||||
"version": "1.3.16",
|
"version": "1.3.17",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "out/index.js",
|
"main": "out/index.js",
|
||||||
"types": "out/index.d.ts",
|
"types": "out/index.d.ts",
|
||||||
|
34
src/index.ts
34
src/index.ts
@ -89,6 +89,9 @@ class LoggingFiles {
|
|||||||
|
|
||||||
private async initializeFile(new_file = false) {
|
private async initializeFile(new_file = false) {
|
||||||
try {
|
try {
|
||||||
|
if (this.stream) {
|
||||||
|
this.stream.close();
|
||||||
|
}
|
||||||
const folder = path.dirname(this.file);
|
const folder = path.dirname(this.file);
|
||||||
if (folder) {
|
if (folder) {
|
||||||
if (!await fsExists(folder)) {
|
if (!await fsExists(folder)) {
|
||||||
@ -99,7 +102,7 @@ class LoggingFiles {
|
|||||||
let size = 0;
|
let size = 0;
|
||||||
if (await fsExists(this.file)) {
|
if (await fsExists(this.file)) {
|
||||||
let stats = await fsStat(this.file);
|
let stats = await fsStat(this.file);
|
||||||
if (new_file || stats.size > maxFileSize) {
|
if (new_file || stats.size >= maxFileSize) {
|
||||||
if (await fsExists(this.file + ".old"))
|
if (await fsExists(this.file + ".old"))
|
||||||
await fsUnlink(this.file + ".old");
|
await fsUnlink(this.file + ".old");
|
||||||
await fsMove(this.file, this.file + ".old")
|
await fsMove(this.file, this.file + ".old")
|
||||||
@ -130,11 +133,17 @@ class LoggingFiles {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async write_to_file(data: Buffer) {
|
private async write_to_file(data: Buffer) {
|
||||||
|
try {
|
||||||
if (data.byteLength < maxFileSize && this.size + data.byteLength > maxFileSize) {
|
if (data.byteLength < maxFileSize && this.size + data.byteLength > maxFileSize) {
|
||||||
let f = await this.initializeFile(true);
|
await this.initializeFile(true)
|
||||||
}
|
}
|
||||||
this.size += data.byteLength;
|
this.size += data.byteLength;
|
||||||
this.stream.write(data);
|
this.stream.write(data);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
this.initializeFile(false);
|
||||||
|
this.write_to_file(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public write(data: Buffer) {
|
public write(data: Buffer) {
|
||||||
@ -324,7 +333,6 @@ function fsStat(path: string) {
|
|||||||
|
|
||||||
function fsMove(oldPath: string, newPath: string) {
|
function fsMove(oldPath: string, newPath: string) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
|
||||||
let callback = (err?) => {
|
let callback = (err?) => {
|
||||||
if (err) reject(err)
|
if (err) reject(err)
|
||||||
else resolve()
|
else resolve()
|
||||||
@ -343,17 +351,21 @@ function fsMove(oldPath: string, newPath: string) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
function copy() {
|
function copy() {
|
||||||
var readStream = fs.createReadStream(oldPath);
|
fs.copyFile(oldPath, newPath, (err) => {
|
||||||
var writeStream = fs.createWriteStream(newPath);
|
if (err) callback(err)
|
||||||
|
else fs.unlink(oldPath, callback);
|
||||||
|
})
|
||||||
|
// var readStream = fs.createReadStream(oldPath);
|
||||||
|
// var writeStream = fs.createWriteStream(newPath);
|
||||||
|
|
||||||
readStream.on('error', callback);
|
// readStream.on('error', callback);
|
||||||
writeStream.on('error', callback);
|
// writeStream.on('error', callback);
|
||||||
|
|
||||||
readStream.on('close', function () {
|
// readStream.on('close', function () {
|
||||||
fs.unlink(oldPath, callback);
|
// fs.unlink(oldPath, callback);
|
||||||
});
|
// });
|
||||||
|
|
||||||
readStream.pipe(writeStream);
|
// readStream.pipe(writeStream);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
27
src/test.ts
27
src/test.ts
@ -31,8 +31,25 @@ cus22.log("Hello from custom Logger 22")
|
|||||||
cus2.log("Hello from custom Logger 2")
|
cus2.log("Hello from custom Logger 2")
|
||||||
|
|
||||||
Logging.console_out = false;
|
Logging.console_out = false;
|
||||||
// Logging.waitForSetup().then(() => {
|
async function benchmark(count: number, message_size: number) {
|
||||||
// for (let i = 0; i < 7000; i++) {
|
await Logging.waitForSetup();
|
||||||
// Logging.log(randomBytes(50000).toString("hex"))
|
const randData = randomBytes(message_size).toString("hex")
|
||||||
// }
|
const t = process.hrtime();
|
||||||
// });
|
for (let i = 0; i < count; i++) {
|
||||||
|
Logging.log(randData)
|
||||||
|
}
|
||||||
|
const diff = process.hrtime(t);
|
||||||
|
const NS_PER_SEC = 1e9;
|
||||||
|
await Logging.waitForSetup();
|
||||||
|
const ns = diff[0] * NS_PER_SEC + diff[1];
|
||||||
|
console.log(`Benchmark took ${ns / 1000000}ms for ${count} messages with a size of ${message_size} characters`);
|
||||||
|
console.log(`This is equal to ${(ns / 1000000) / count} ms per message`)
|
||||||
|
}
|
||||||
|
|
||||||
|
Logging.waitForSetup().then(async () => {
|
||||||
|
console.log("Large data benchmark:")
|
||||||
|
await benchmark(7000, 50000);
|
||||||
|
|
||||||
|
console.log("Realdata data benchmark:")
|
||||||
|
await benchmark(100000, 100)
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user