1
0
mirror of https://git.stamm.me/OpenServer/NodeLogging.git synced 2024-11-15 02:21:04 +00:00

Making it logging 2.0 compatible

This commit is contained in:
Fabian 2019-04-04 22:40:34 -04:00
parent 27da76c1b0
commit 0dbd8e9c40
3 changed files with 79 additions and 44 deletions

69
package-lock.json generated
View File

@ -1,26 +1,26 @@
{ {
"name": "@hibas123/nodelogging", "name": "@hibas123/nodelogging",
"version": "1.6.0", "version": "2.0.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
"@hibas123/logging": { "@hibas123/logging": {
"version": "1.1.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/@hibas123/logging/-/logging-1.1.0.tgz", "resolved": "https://registry.npmjs.org/@hibas123/logging/-/logging-2.0.0.tgz",
"integrity": "sha512-EaDyvvWDD+SikLgP6WTvTCR6Sk9Kp2BZkDbUk0Rz4pF2B4dGyO8yRI8qhBEtjbMXGqedeM5mQDGgvY5QUDevVg==", "integrity": "sha512-v4e7xiXGvAKpq6nxLA2TVoEy/DTT7DZkqEKh8eZ1XiAQAJh0PnmF1GP/TdHWvDTPMG/U8kMHArrGllVxM6xtkg==",
"requires": { "requires": {
"@hibas123/utils": "^2.0.2" "@hibas123/utils": "^2.0.5"
} }
}, },
"@hibas123/utils": { "@hibas123/utils": {
"version": "2.0.2", "version": "2.0.5",
"resolved": "https://registry.npmjs.org/@hibas123/utils/-/utils-2.0.2.tgz", "resolved": "https://registry.npmjs.org/@hibas123/utils/-/utils-2.0.5.tgz",
"integrity": "sha512-5yrpuuUv0YlL2BixiTQLta4EmPLMNZ9Waorymu6dbERiSIcBxDs8mcJDUDW7CGiewpHHQUMsqLycLUYPDx0LWw==" "integrity": "sha512-LI1Xw0QArpm0JqCUmuxRG7dgmjvyLPmJehcNRqHrWuCwmDRoPFe0PwmZsp8/saz6pCL2A4u0utVHWG2PG4lSyg=="
}, },
"@types/node": { "@types/node": {
"version": "11.11.7", "version": "11.13.0",
"resolved": "https://registry.npmjs.org/@types/node/-/node-11.11.7.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-11.13.0.tgz",
"integrity": "sha512-bHbRcyD6XpXVLg42QYaQCjvDXaCFkvb3WbCIxSDmhGbJYVroxvYzekk9QGg1beeIawfvSLkdZpP0h7jxE4ihnA==", "integrity": "sha512-rx29MMkRdVmzunmiA4lzBYJNnXsW/PhG4kMBy2ATsYaDjGGR75dCFEVVROKpNwlVdcUX3xxlghKQOeDPBJobng==",
"dev": true "dev": true
}, },
"abbrev": { "abbrev": {
@ -805,7 +805,8 @@
"ansi-regex": { "ansi-regex": {
"version": "2.1.1", "version": "2.1.1",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"aproba": { "aproba": {
"version": "1.2.0", "version": "1.2.0",
@ -826,12 +827,14 @@
"balanced-match": { "balanced-match": {
"version": "1.0.0", "version": "1.0.0",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"brace-expansion": { "brace-expansion": {
"version": "1.1.11", "version": "1.1.11",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"balanced-match": "^1.0.0", "balanced-match": "^1.0.0",
"concat-map": "0.0.1" "concat-map": "0.0.1"
@ -846,17 +849,20 @@
"code-point-at": { "code-point-at": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"concat-map": { "concat-map": {
"version": "0.0.1", "version": "0.0.1",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"console-control-strings": { "console-control-strings": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"core-util-is": { "core-util-is": {
"version": "1.0.2", "version": "1.0.2",
@ -973,7 +979,8 @@
"inherits": { "inherits": {
"version": "2.0.3", "version": "2.0.3",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"ini": { "ini": {
"version": "1.3.5", "version": "1.3.5",
@ -985,6 +992,7 @@
"version": "1.0.0", "version": "1.0.0",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"number-is-nan": "^1.0.0" "number-is-nan": "^1.0.0"
} }
@ -999,6 +1007,7 @@
"version": "3.0.4", "version": "3.0.4",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"brace-expansion": "^1.1.7" "brace-expansion": "^1.1.7"
} }
@ -1006,12 +1015,14 @@
"minimist": { "minimist": {
"version": "0.0.8", "version": "0.0.8",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"minipass": { "minipass": {
"version": "2.3.5", "version": "2.3.5",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"safe-buffer": "^5.1.2", "safe-buffer": "^5.1.2",
"yallist": "^3.0.0" "yallist": "^3.0.0"
@ -1030,6 +1041,7 @@
"version": "0.5.1", "version": "0.5.1",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"minimist": "0.0.8" "minimist": "0.0.8"
} }
@ -1110,7 +1122,8 @@
"number-is-nan": { "number-is-nan": {
"version": "1.0.1", "version": "1.0.1",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"object-assign": { "object-assign": {
"version": "4.1.1", "version": "4.1.1",
@ -1122,6 +1135,7 @@
"version": "1.4.0", "version": "1.4.0",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"wrappy": "1" "wrappy": "1"
} }
@ -1207,7 +1221,8 @@
"safe-buffer": { "safe-buffer": {
"version": "5.1.2", "version": "5.1.2",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"safer-buffer": { "safer-buffer": {
"version": "2.1.2", "version": "2.1.2",
@ -1243,6 +1258,7 @@
"version": "1.0.2", "version": "1.0.2",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"code-point-at": "^1.0.0", "code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0", "is-fullwidth-code-point": "^1.0.0",
@ -1262,6 +1278,7 @@
"version": "3.0.1", "version": "3.0.1",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"ansi-regex": "^2.0.0" "ansi-regex": "^2.0.0"
} }
@ -1305,12 +1322,14 @@
"wrappy": { "wrappy": {
"version": "1.0.2", "version": "1.0.2",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"yallist": { "yallist": {
"version": "3.0.3", "version": "3.0.3",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
} }
} }
}, },
@ -2767,9 +2786,9 @@
"dev": true "dev": true
}, },
"typescript": { "typescript": {
"version": "3.3.4000", "version": "3.4.1",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.3.4000.tgz", "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.4.1.tgz",
"integrity": "sha512-jjOcCZvpkl2+z7JFn0yBOoLQyLoIkNZAs/fYJkUG6VKy6zLPHJGfQJYFHzibB6GJaF/8QrcECtlQ5cpvRHSMEA==", "integrity": "sha512-3NSMb2VzDQm8oBTLH6Nj55VVtUEpe/rgkIzMir0qVoLyjDZlnMBva0U6vDiV3IH+sl/Yu6oP5QwsAQtHPmDd2Q==",
"dev": true "dev": true
}, },
"undefsafe": { "undefsafe": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@hibas123/nodelogging", "name": "@hibas123/nodelogging",
"version": "1.7.0", "version": "2.0.1",
"description": "", "description": "",
"main": "out/index.js", "main": "out/index.js",
"types": "out/index.d.ts", "types": "out/index.d.ts",
@ -20,13 +20,13 @@
"author": "Fabian Stamm", "author": "Fabian Stamm",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@types/node": "^11.11.7", "@types/node": "^11.13.0",
"concurrently": "^4.1.0", "concurrently": "^4.1.0",
"nodemon": "^1.17.4", "nodemon": "^1.17.4",
"typescript": "^3.3.4000" "typescript": "^3.4.1"
}, },
"dependencies": { "dependencies": {
"@hibas123/logging": "^1.1.0", "@hibas123/logging": "^2.0.0",
"@hibas123/utils": "^2.0.2" "@hibas123/utils": "^2.0.5"
} }
} }

View File

@ -4,18 +4,18 @@ import * as path from "path";
import { Adapter, Message, LoggingTypes } from "@hibas123/logging"; import { Adapter, Message, LoggingTypes } from "@hibas123/logging";
const maxFileSize = 500000000; const MAX_FILE_SIZE = 500000000;
export class LoggingFiles implements Adapter { export class LoggingFiles implements Adapter {
file: Files; file: Files;
constructor(filename: string, private error = false) { constructor(filename: string, private error = false, private maxFileSize = MAX_FILE_SIZE) {
this.file = Files.getFile(filename); this.file = Files.getFile(filename);
} }
init(observable: ObservableInterface<Message>) { init(observable: ObservableInterface<Message>) {
observable.subscribe(this.onMessage.bind(this)); observable.subscribe(this.onMessage.bind(this));
return this.file.init(); return this.file.init(this.maxFileSize);
} }
flush(sync: boolean) { flush(sync: boolean) {
@ -26,20 +26,21 @@ export class LoggingFiles implements Adapter {
// Just ignore all non error messages, if this.error is set // Just ignore all non error messages, if this.error is set
if (this.error && message.type !== LoggingTypes.Error) if (this.error && message.type !== LoggingTypes.Error)
return; return;
let txt = message.text.formatted.join("\n") + "\n";
txt = txt.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, "");
let index = txt.indexOf("\x1b"); let txt = message.text.formatted.map(fmt => fmt.map(f => f.text).join("") + "\n").join("");
while (index >= 0) {
txt = txt.substring(0, index) + txt.substring(index + 5, txt.length);
index = txt.indexOf("\x1b");
}
let msg = Buffer.from(txt); let msg = Buffer.from(txt);
this.file.write(msg); this.file.write(msg);
} }
close() {
this.file.close();
}
} }
export class Files { export class Files {
private open = 0;
private static files = new Map<string, Files>(); private static files = new Map<string, Files>();
static getFile(filename: string): Files { static getFile(filename: string): Files {
filename = path.resolve(filename); filename = path.resolve(filename);
@ -48,9 +49,11 @@ export class Files {
file = new Files(filename); file = new Files(filename);
this.files.set(filename, file); this.files.set(filename, file);
} }
file.open++;
return file; return file;
} }
private maxFileSize = MAX_FILE_SIZE;
private size: number = 0; private size: number = 0;
private stream: fs.WriteStream = undefined; private stream: fs.WriteStream = undefined;
private lock = new Lock(); private lock = new Lock();
@ -59,10 +62,11 @@ export class Files {
private constructor(private file: string) { } private constructor(private file: string) { }
public async init() { public async init(maxFileSize: number) {
if (this.initialized) if (this.initialized)
return; return;
let lock = await this.lock.getLock(); let lock = await this.lock.getLock();
this.maxFileSize == maxFileSize;
await this.initializeFile() await this.initializeFile()
this.initialized = true; this.initialized = true;
lock.release(); lock.release();
@ -84,7 +88,7 @@ export class Files {
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 >= this.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")
@ -114,6 +118,14 @@ export class Files {
lock.release(); lock.release();
} }
public async close() {
await this.flush(false);
this.open--;
if (this.open <= 0) {
this.stream.close()
Files.files.delete(this.file);
}
}
public flush(sync: boolean) { public flush(sync: boolean) {
if (sync) { if (sync) {
// if sync flush, the process most likely is in failstate, so checkQueue stopped its work. // if sync flush, the process most likely is in failstate, so checkQueue stopped its work.
@ -132,7 +144,7 @@ export class Files {
private async write_to_file(data: Buffer) { private async write_to_file(data: Buffer) {
try { try {
if (data.byteLength < maxFileSize && this.size + data.byteLength > maxFileSize) { if (data.byteLength < this.maxFileSize && this.size + data.byteLength > this.maxFileSize) {
await this.initializeFile(true) await this.initializeFile(true)
} }
this.size += data.byteLength; this.size += data.byteLength;
@ -149,6 +161,10 @@ export class Files {
this.queue.push(data); this.queue.push(data);
this.checkQueue() this.checkQueue()
} }
public dispose() {
}
} }
function fsUnlink(path) { function fsUnlink(path) {