132 lines
3.3 KiB
TypeScript
132 lines
3.3 KiB
TypeScript
/**
|
|
* @author Martin Karkowski
|
|
* @email m.karkowski@zema.de
|
|
* @create date 2021-05-21 16:44:59
|
|
* @modify date 2021-05-21 16:44:59
|
|
* @desc [description]
|
|
*/
|
|
|
|
import { writeFile } from "fs";
|
|
import * as Logger from "js-logger";
|
|
import { join } from "path";
|
|
import { createFile } from "../helpers/fileMethods";
|
|
import { replaceAll } from "../helpers/stringMethods";
|
|
import { getCentralNopeLogger, getNopeLogger } from "./getLogger";
|
|
|
|
export const CURRENT_DATE = _parsableISOString();
|
|
export const DEFAULT_LOG_LOCATION = join(process.cwd(), "logs");
|
|
const DEFAULT_FILE = join(DEFAULT_LOG_LOCATION, CURRENT_DATE + ".log");
|
|
|
|
function _parsableISOString(date = new Date()) {
|
|
let isoString = date.toISOString();
|
|
isoString = replaceAll(isoString, ":", "-");
|
|
isoString = replaceAll(isoString, ".", "-");
|
|
return isoString;
|
|
}
|
|
|
|
/**
|
|
* Generates a Log-File Path based on the given name with the following format:
|
|
* /logs/{name}_{date}.log
|
|
*
|
|
* @export
|
|
* @param {string} name Name of the File.
|
|
* @return {*} {string}
|
|
*/
|
|
export function generateLogfilePath(name: string): string {
|
|
return join(DEFAULT_LOG_LOCATION, name + "_" + _parsableISOString() + ".log");
|
|
}
|
|
|
|
/**
|
|
* Function to use a log file instead of the console log.
|
|
*
|
|
* @export
|
|
*/
|
|
export function useLogFile(pathToFile = DEFAULT_FILE, bufferSize = 100): void {
|
|
const logger = getCentralNopeLogger();
|
|
|
|
// Define a function, that will write the content of the Buffer to our
|
|
// file.
|
|
const writeBufferToFile = function () {
|
|
const textToStore = buffer.join("\n");
|
|
|
|
readyToWrite = false;
|
|
|
|
// Now lets start to write the Fil.e
|
|
writeFile(
|
|
pathToFile,
|
|
textToStore,
|
|
{
|
|
encoding: "utf-8",
|
|
flag: "a"
|
|
},
|
|
(err) => {
|
|
if (err) {
|
|
console.error(err);
|
|
} else {
|
|
readyToWrite = true;
|
|
}
|
|
}
|
|
);
|
|
|
|
buffer = [];
|
|
};
|
|
|
|
let buffer: string[] = [];
|
|
let readyToWrite = false;
|
|
|
|
// Now every thing, is defined, so we are able to
|
|
// define our file.
|
|
|
|
const consoleLogger = getNopeLogger("file-logger");
|
|
|
|
createFile(pathToFile, "", { encoding: "utf-8" }).then(
|
|
// If our file has been created,
|
|
(_) => {
|
|
readyToWrite = true;
|
|
|
|
// Now make shure, that our inital buffer has been written
|
|
if (buffer.length > 0) {
|
|
writeBufferToFile();
|
|
}
|
|
}
|
|
);
|
|
|
|
logger.setHandler((msg, context) => {
|
|
if (bufferSize < buffer.length && readyToWrite) {
|
|
// Now if the Data is ready, lets write the
|
|
// buffer to the File.
|
|
writeBufferToFile();
|
|
} else {
|
|
// Else we extend our buffer:
|
|
const logAsString = [
|
|
context.filterLevel.name,
|
|
"-",
|
|
context.name,
|
|
":",
|
|
Object.values(msg).join(" ")
|
|
].join(" ");
|
|
buffer.push(logAsString);
|
|
}
|
|
});
|
|
|
|
if (bufferSize > 0) {
|
|
const clearBufferAtEnd = function () {
|
|
// change the default Handler:
|
|
logger.setHandler(Logger.createDefaultHandler());
|
|
|
|
consoleLogger.info("Shutdown detected! Trying to Write the Buffer");
|
|
|
|
if (readyToWrite) {
|
|
// Now if the Data is ready, lets write the
|
|
// buffer to the File.
|
|
writeBufferToFile();
|
|
} else {
|
|
setTimeout(clearBufferAtEnd, 50);
|
|
}
|
|
};
|
|
|
|
process.on("SIGINT", clearBufferAtEnd);
|
|
process.on("SIGTERM", clearBufferAtEnd);
|
|
}
|
|
}
|