Adding config option to set console output name for additional Logging objects.

This commit is contained in:
Fabian Stamm
2018-09-01 16:45:22 +02:00
parent cf4938d0db
commit 4918fe9b17
5 changed files with 23 additions and 35 deletions

View File

@ -37,6 +37,10 @@ const maxFileSize = 500000000;
const OriginalErrorStackFunction = (<any>Error.prototype).prepareStackTrace
export interface LoggingBaseOptions {
/**
* Name will be prefixed on Console output
*/
name: string,
logfile: string;
errorfile: string;
console_out: boolean;
@ -56,6 +60,7 @@ export class LoggingBase {
constructor(options?: Partial<LoggingBaseOptions>) {
if (!options) options = {};
this.config = Object.assign(<LoggingBaseOptions>{
name: undefined,
console_out: true,
logfile: "./logs/all.log",
errorfile: "./logs/error.log"
@ -161,7 +166,11 @@ export class LoggingBase {
let prefix = `[${LoggingTypes[type]}][${file.file}:${file.line}][${date}]: `;
let message_lines = mb.split("\n").map(line => prefix + line);
if (this.config.console_out) message_lines.forEach(line => console.log(consoleLogFormat + line + Colors.Reset));
if (this.config.console_out) {
let prefix = "";
if (this.config.name) prefix = `[${this.config.name}]`;
message_lines.forEach(line => console.log(consoleLogFormat + prefix + line + Colors.Reset));
}
let m = message_lines.join("\n");
let index = m.indexOf("\x1b");
@ -249,22 +258,6 @@ export class LoggingBase {
}
return { stream: fs.createWriteStream(file, { flags: "a" }), size: size };
// if (await fsExists(this.logFileLocation + "error.log")) {
// let stats = await fsStat(this.logFileLocation + "error.log")
// if (stats.size > maxFileSize) {
// if (await fsExists(this.logFileLocation + "error.log.old"))
// await fsUnlink(this.logFileLocation + "error.log.old");
// await fsMove(this.logFileLocation + "error.log", this.logFileLocation + "error.log.old")
// } else {
// this.errorSize = stats.size;
// }
// }
// this.fileStream = fs.createWriteStream(this.logFileLocation + "all.log", { flags: "a" });
// this.errorStream = fs.createWriteStream(this.logFileLocation + "error.log", { flags: "a" });
// this.writing = false;
// this.checkQueue();
} catch (e) {
console.log(e);
}