diff --git a/package-lock.json b/package-lock.json index b9d6b7f..db66a82 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,13 +1,13 @@ { "name": "@hibas123/nodelogging", - "version": "2.1.5", + "version": "2.3.0", "lockfileVersion": 1, "requires": true, "dependencies": { "@hibas123/logging": { - "version": "2.2.2", - "resolved": "https://registry.npmjs.org/@hibas123/logging/-/logging-2.2.2.tgz", - "integrity": "sha512-45ajce5iFAKIYK74VfNVWtUCkk3nXSV2pCrnY7edszPrME8HWgrrkHMxc4yo3cIBpZNP9WN3O2TnDseasCvcjw==", + "version": "2.3.0", + "resolved": "https://npm.hibas123.de/@hibas123%2flogging/-/logging-2.3.0.tgz", + "integrity": "sha512-VB/aOrup5AFOL4Ck5c7jF1Qy5Jr5tDfjhMHI0Tbe4fzkA2pagpTcIgdm+p0rQ9sa/rO2FKpd0kpy11b3W5NC8g==", "requires": { "@hibas123/utils": "^2.2.3" } diff --git a/package.json b/package.json index d5940cc..6efc2b0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hibas123/nodelogging", - "version": "2.2.2", + "version": "2.3.0", "description": "", "main": "out/index.js", "types": "out/index.d.ts", @@ -32,7 +32,7 @@ "typescript": "^3.8.3" }, "dependencies": { - "@hibas123/logging": "^2.2.2", + "@hibas123/logging": "^2.3.0", "@hibas123/utils": "^2.2.3" } } diff --git a/src/filewriter.ts b/src/filewriter.ts index 4b4fcf6..c4f0b59 100644 --- a/src/filewriter.ts +++ b/src/filewriter.ts @@ -3,19 +3,23 @@ import * as fs from "fs"; import * as path from "path"; import { Adapter, Message, LoggingTypes } from "@hibas123/logging"; - const MAX_FILE_SIZE = 500000000; export class LoggingFiles implements Adapter { file: Files; - constructor(filename: string, private error = false, private maxFileSize = MAX_FILE_SIZE) { - this.file = Files.getFile(filename); - } - + constructor( + private filename: string, + private error = false, + private maxFileSize = MAX_FILE_SIZE, + private noPrefix = false + ) {} init(observable: ObservableInterface) { observable.subscribe(this.onMessage.bind(this)); - return this.file.init(this.maxFileSize); + if (!this.file) { + this.file = Files.getFile(this.filename); + return this.file.init(this.maxFileSize); + } } flush(sync: boolean) { @@ -24,10 +28,13 @@ export class LoggingFiles implements Adapter { onMessage(message: Message) { // Just ignore all non error messages, if this.error is set - if (this.error && message.type !== LoggingTypes.Error) - return; + if (this.error && message.type !== LoggingTypes.Error) return; - let txt = message.text.formatted.map(fmt => fmt.map(f => f.text).join("") + "\n").join(""); + let prefix = ""; + if (message.name) prefix = `[${message.name}]=>`; + let txt = message.text.formatted + .map((fmt) => prefix + fmt.map((f) => f.text).join("") + "\n") + .join(""); let msg = Buffer.from(txt); this.file.write(msg); @@ -35,6 +42,7 @@ export class LoggingFiles implements Adapter { close() { this.file.close(); + this.file = undefined; } } @@ -58,19 +66,22 @@ export class Files { private stream: fs.WriteStream = undefined; private lock = new Lock(); - public initialized = false; + private $initialized = false; - private constructor(private file: string) { } + public get initlialized() { + return this.$initialized; + } + + private constructor(private file: string) {} public async init(maxFileSize: number) { - if (this.initialized) - return; + if (this.$initialized) return; let lock = await this.lock.getLock(); this.maxFileSize == maxFileSize; - await this.initializeFile() - this.initialized = true; + await this.initializeFile(); + this.$initialized = true; lock.release(); - this.checkQueue() + this.checkQueue(); } private async initializeFile(new_file = false) { @@ -80,8 +91,8 @@ export class Files { } const folder = path.dirname(this.file); if (folder) { - if (!await fsExists(folder)) { - await fsMkDir(folder).catch(() => { }); //Could happen, if two seperate instances want to create the same folder so ignoring + if (!(await fsExists(folder))) { + await fsMkDir(folder).catch(() => {}); //Could happen, if two seperate instances want to create the same folder so ignoring } } @@ -91,13 +102,13 @@ export class Files { if (new_file || stats.size >= this.maxFileSize) { if (await fsExists(this.file + ".old")) await fsUnlink(this.file + ".old"); - await fsMove(this.file, this.file + ".old") + await fsMove(this.file, this.file + ".old"); } else { size = stats.size; } } - this.stream = fs.createWriteStream(this.file, { flags: "a" }) + this.stream = fs.createWriteStream(this.file, { flags: "a" }); this.size = size; } catch (e) { console.log(e); @@ -112,7 +123,7 @@ export class Files { if (this.lock.locked) return; let lock = await this.lock.getLock(); let msg: Buffer; - while (msg = this.queue.shift()) { + while ((msg = this.queue.shift())) { await this.write_to_file(msg); } lock.release(); @@ -122,7 +133,7 @@ export class Files { await this.flush(false); this.open--; if (this.open <= 0) { - this.stream.close() + this.stream.close(); Files.files.delete(this.file); } } @@ -130,7 +141,7 @@ export class Files { if (sync) { // if sync flush, the process most likely is in failstate, so checkQueue stopped its work. let msg: Buffer; - while (msg = this.queue.shift()) { + while ((msg = this.queue.shift())) { this.stream.write(msg); } } else { @@ -138,14 +149,17 @@ export class Files { const lock = await this.lock.getLock(); lock.release(); await this.checkQueue(); - }) + }); } } private async write_to_file(data: Buffer) { try { - if (data.byteLength < this.maxFileSize && this.size + data.byteLength > this.maxFileSize) { - await this.initializeFile(true) + if ( + data.byteLength < this.maxFileSize && + this.size + data.byteLength > this.maxFileSize + ) { + await this.initializeFile(true); } this.size += data.byteLength; this.stream.write(data); @@ -159,12 +173,10 @@ export class Files { public write(data: Buffer) { this.queue.push(data); - this.checkQueue() + this.checkQueue(); } - public dispose() { - - } + public dispose() {} } function fsUnlink(path) { @@ -172,8 +184,8 @@ function fsUnlink(path) { fs.unlink(path, (err) => { if (err) reject(err); else resolve(); - }) - }) + }); + }); } function fsStat(path: string) { @@ -181,36 +193,36 @@ function fsStat(path: string) { fs.stat(path, (err, stats) => { if (err) reject(err); else resolve(stats); - }) - }) + }); + }); } function fsMove(oldPath: string, newPath: string) { return new Promise((resolve, reject) => { let callback = (err?) => { - if (err) reject(err) - else resolve() - } + if (err) reject(err); + else resolve(); + }; fs.rename(oldPath, newPath, function (err) { if (err) { - if (err.code === 'EXDEV') { + if (err.code === "EXDEV") { copy(); } else { - callback(err) + callback(err); } return; } - callback() + callback(); }); function copy() { fs.copyFile(oldPath, newPath, (err) => { - if (err) callback(err) + if (err) callback(err); else fs.unlink(oldPath, callback); - }) + }); } - }) + }); } function fsExists(path: string) { @@ -221,6 +233,6 @@ function fsExists(path: string) { function fsMkDir(path: string) { return new Promise((resolve, reject) => { - fs.mkdir(path, (err) => err ? reject(err) : resolve()); + fs.mkdir(path, (err) => (err ? reject(err) : resolve())); }); -} \ No newline at end of file +}