fixing file logger

This commit is contained in:
Martin Karkowski 2021-07-29 16:07:32 +02:00
parent 243ed4ec7b
commit 712785e4a9

View File

@ -2,12 +2,11 @@
* @author Martin Karkowski * @author Martin Karkowski
* @email m.karkowski@zema.de * @email m.karkowski@zema.de
* @create date 2021-05-21 16:44:59 * @create date 2021-05-21 16:44:59
* @modify date 2021-05-21 16:44:59 * @modify date 2021-07-29 16:06:13
* @desc [description] * @desc [description]
*/ */
import { writeFile } from "fs"; import { writeFile } from "fs";
import * as Logger from "js-logger";
import { join } from "path"; import { join } from "path";
import { createFile } from "../helpers/fileMethods"; import { createFile } from "../helpers/fileMethods";
import { replaceAll } from "../helpers/stringMethods"; import { replaceAll } from "../helpers/stringMethods";
@ -46,7 +45,7 @@ export function useLogFile(pathToFile = DEFAULT_FILE, bufferSize = 100): void {
// Define a function, that will write the content of the Buffer to our // Define a function, that will write the content of the Buffer to our
// file. // file.
const writeBufferToFile = function () { const writeBufferToFile = function (cb: () => void = null) {
const textToStore = buffer.join("\n"); const textToStore = buffer.join("\n");
readyToWrite = false; readyToWrite = false;
@ -64,6 +63,10 @@ export function useLogFile(pathToFile = DEFAULT_FILE, bufferSize = 100): void {
console.error(err); console.error(err);
} else { } else {
readyToWrite = true; readyToWrite = true;
if (typeof (cb) == "function") {
cb();
}
} }
} }
); );
@ -111,15 +114,12 @@ export function useLogFile(pathToFile = DEFAULT_FILE, bufferSize = 100): void {
if (bufferSize > 0) { if (bufferSize > 0) {
const clearBufferAtEnd = function () { const clearBufferAtEnd = function () {
// change the default Handler:
logger.setHandler(Logger.createDefaultHandler());
consoleLogger.info("Shutdown detected! Trying to Write the Buffer"); consoleLogger.info("Shutdown detected! Trying to Write the Buffer");
if (readyToWrite) { if (readyToWrite) {
// Now if the Data is ready, lets write the // Now if the Data is ready, lets write the
// buffer to the File. // buffer to the File.
writeBufferToFile(); writeBufferToFile(() => process.exit(0));
} else { } else {
setTimeout(clearBufferAtEnd, 50); setTimeout(clearBufferAtEnd, 50);
} }