Compare commits

..

No commits in common. "0dbd8e9c40131a3a45172c18e74e1b3d08a9e801" and "2aaee1be8963bf05994de675ab71f21eadd2988c" have entirely different histories.

4 changed files with 56 additions and 96 deletions

69
package-lock.json generated
View File

@ -1,26 +1,26 @@
{ {
"name": "@hibas123/nodelogging", "name": "@hibas123/nodelogging",
"version": "2.0.0", "version": "1.6.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
"@hibas123/logging": { "@hibas123/logging": {
"version": "2.0.0", "version": "1.1.0",
"resolved": "https://registry.npmjs.org/@hibas123/logging/-/logging-2.0.0.tgz", "resolved": "https://registry.npmjs.org/@hibas123/logging/-/logging-1.1.0.tgz",
"integrity": "sha512-v4e7xiXGvAKpq6nxLA2TVoEy/DTT7DZkqEKh8eZ1XiAQAJh0PnmF1GP/TdHWvDTPMG/U8kMHArrGllVxM6xtkg==", "integrity": "sha512-EaDyvvWDD+SikLgP6WTvTCR6Sk9Kp2BZkDbUk0Rz4pF2B4dGyO8yRI8qhBEtjbMXGqedeM5mQDGgvY5QUDevVg==",
"requires": { "requires": {
"@hibas123/utils": "^2.0.5" "@hibas123/utils": "^2.0.2"
} }
}, },
"@hibas123/utils": { "@hibas123/utils": {
"version": "2.0.5", "version": "2.0.2",
"resolved": "https://registry.npmjs.org/@hibas123/utils/-/utils-2.0.5.tgz", "resolved": "https://registry.npmjs.org/@hibas123/utils/-/utils-2.0.2.tgz",
"integrity": "sha512-LI1Xw0QArpm0JqCUmuxRG7dgmjvyLPmJehcNRqHrWuCwmDRoPFe0PwmZsp8/saz6pCL2A4u0utVHWG2PG4lSyg==" "integrity": "sha512-5yrpuuUv0YlL2BixiTQLta4EmPLMNZ9Waorymu6dbERiSIcBxDs8mcJDUDW7CGiewpHHQUMsqLycLUYPDx0LWw=="
}, },
"@types/node": { "@types/node": {
"version": "11.13.0", "version": "11.11.7",
"resolved": "https://registry.npmjs.org/@types/node/-/node-11.13.0.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-11.11.7.tgz",
"integrity": "sha512-rx29MMkRdVmzunmiA4lzBYJNnXsW/PhG4kMBy2ATsYaDjGGR75dCFEVVROKpNwlVdcUX3xxlghKQOeDPBJobng==", "integrity": "sha512-bHbRcyD6XpXVLg42QYaQCjvDXaCFkvb3WbCIxSDmhGbJYVroxvYzekk9QGg1beeIawfvSLkdZpP0h7jxE4ihnA==",
"dev": true "dev": true
}, },
"abbrev": { "abbrev": {
@ -805,8 +805,7 @@
"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",
@ -827,14 +826,12 @@
"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"
@ -849,20 +846,17 @@
"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",
@ -979,8 +973,7 @@
"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",
@ -992,7 +985,6 @@
"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"
} }
@ -1007,7 +999,6 @@
"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"
} }
@ -1015,14 +1006,12 @@
"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"
@ -1041,7 +1030,6 @@
"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"
} }
@ -1122,8 +1110,7 @@
"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",
@ -1135,7 +1122,6 @@
"version": "1.4.0", "version": "1.4.0",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"wrappy": "1" "wrappy": "1"
} }
@ -1221,8 +1207,7 @@
"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",
@ -1258,7 +1243,6 @@
"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",
@ -1278,7 +1262,6 @@
"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"
} }
@ -1322,14 +1305,12 @@
"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
} }
} }
}, },
@ -2786,9 +2767,9 @@
"dev": true "dev": true
}, },
"typescript": { "typescript": {
"version": "3.4.1", "version": "3.3.4000",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.4.1.tgz", "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.3.4000.tgz",
"integrity": "sha512-3NSMb2VzDQm8oBTLH6Nj55VVtUEpe/rgkIzMir0qVoLyjDZlnMBva0U6vDiV3IH+sl/Yu6oP5QwsAQtHPmDd2Q==", "integrity": "sha512-jjOcCZvpkl2+z7JFn0yBOoLQyLoIkNZAs/fYJkUG6VKy6zLPHJGfQJYFHzibB6GJaF/8QrcECtlQ5cpvRHSMEA==",
"dev": true "dev": true
}, },
"undefsafe": { "undefsafe": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@hibas123/nodelogging", "name": "@hibas123/nodelogging",
"version": "2.0.1", "version": "1.7.0",
"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.13.0", "@types/node": "^11.11.7",
"concurrently": "^4.1.0", "concurrently": "^4.1.0",
"nodemon": "^1.17.4", "nodemon": "^1.17.4",
"typescript": "^3.4.1" "typescript": "^3.3.4000"
}, },
"dependencies": { "dependencies": {
"@hibas123/logging": "^2.0.0", "@hibas123/logging": "^1.1.0",
"@hibas123/utils": "^2.0.5" "@hibas123/utils": "^2.0.2"
} }
} }

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 MAX_FILE_SIZE = 500000000; const maxFileSize = 500000000;
export class LoggingFiles implements Adapter { export class LoggingFiles implements Adapter {
file: Files; file: Files;
constructor(filename: string, private error = false, private maxFileSize = MAX_FILE_SIZE) { constructor(filename: string, private error = false) {
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(this.maxFileSize); return this.file.init();
} }
flush(sync: boolean) { flush(sync: boolean) {
@ -26,21 +26,20 @@ 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 txt = message.text.formatted.map(fmt => fmt.map(f => f.text).join("") + "\n").join(""); let index = txt.indexOf("\x1b");
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);
@ -49,11 +48,9 @@ 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();
@ -62,11 +59,10 @@ export class Files {
private constructor(private file: string) { } private constructor(private file: string) { }
public async init(maxFileSize: number) { public async init() {
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();
@ -88,7 +84,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 >= this.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")
@ -118,14 +114,6 @@ 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.
@ -144,7 +132,7 @@ export class Files {
private async write_to_file(data: Buffer) { private async write_to_file(data: Buffer) {
try { try {
if (data.byteLength < this.maxFileSize && this.size + data.byteLength > this.maxFileSize) { if (data.byteLength < maxFileSize && this.size + data.byteLength > maxFileSize) {
await this.initializeFile(true) await this.initializeFile(true)
} }
this.size += data.byteLength; this.size += data.byteLength;
@ -161,10 +149,6 @@ export class Files {
this.queue.push(data); this.queue.push(data);
this.checkQueue() this.checkQueue()
} }
public dispose() {
}
} }
function fsUnlink(path) { function fsUnlink(path) {

View File

@ -6,17 +6,13 @@ import { LoggingBase as LoggingBaseOriginal, LoggingBaseOptions } from "@hibas12
export interface LoggingOptions extends LoggingBaseOptions { export interface LoggingOptions extends LoggingBaseOptions {
files: boolean | { files: boolean | {
/** /**
* Filename/path of the logfile. Skip if generated with name. * Filename/path of the logfile. Skip if generated with name
*
* If not wanted pass null
*/ */
logfile?: string | null; logfile: string;
/** /**
* Filename/path of the logfile. Skip if generated with name. * Filename/path of the logfile. Skip if generated with name
*
* If not wanted pass null
*/ */
errorfile?: string | null; errorfile: string;
} }
} }
@ -30,22 +26,21 @@ export class LoggingBase extends LoggingBaseOriginal {
if (typeof config !== "string" && typeof config.files === "object") { if (typeof config !== "string" && typeof config.files === "object") {
logfile = config.files.logfile; logfile = config.files.logfile;
errorfile = config.files.errorfile; errorfile = config.files.errorfile;
} else {
let name = this.name ? "." + this.name : "";
logfile = `./logs/all${name}.log`;
errorfile = `./logs/error${name}.log`;
} }
let name = this.name ? "." + this.name : ""; if (logfile) {
if (!logfile && logfile !== null)
logfile = `./logs/all${name}.log`;
if (!errorfile && errorfile !== null)
errorfile = `./logs/error${name}.log`;
if (logfile)
this.addAdapter(new LoggingFiles(logfile)); this.addAdapter(new LoggingFiles(logfile));
}
if (errorfile) {
if (errorfile)
this.addAdapter(new LoggingFiles(errorfile, true)); this.addAdapter(new LoggingFiles(errorfile, true));
} }
} }
}
} }
export let Logging: LoggingBase = undefined; export let Logging: LoggingBase = undefined;