Adding simplified custom logger initialisation

This commit is contained in:
Fabian Stamm
2018-09-28 12:37:13 +02:00
parent 51c519ce75
commit 912a4b3ad8
8 changed files with 70 additions and 73 deletions

View File

@ -68,15 +68,21 @@ export class LoggingBase {
private queue = new Array<{ message: string, error: boolean }>();
constructor(options?: Partial<LoggingBaseOptions>) {
if (!options) options = {};
if (options.name) {
if (options.logfile === undefined) {
options.logfile = `./logs/all.${options.name}.log`
constructor(options?: Partial<LoggingBaseOptions> | string) {
let opt: Partial<LoggingBaseOptions>;
if (!options) opt = {}
else if (typeof options === "string") {
opt = { name: options };
} else {
opt = options;
}
if (opt.name) {
if (opt.logfile === undefined) {
opt.logfile = `./logs/all.${opt.name}.log`
}
if (options.errorfile === undefined) {
options.errorfile = `./logs/error.${options.name}.log`
if (opt.errorfile === undefined) {
opt.errorfile = `./logs/error.${opt.name}.log`
}
}
this.config = Object.assign(<LoggingBaseOptions>{
@ -84,19 +90,18 @@ export class LoggingBase {
console_out: true,
logfile: "./logs/all.log",
errorfile: "./logs/error.log"
}, options);
}, opt);
for (let key in this) {
if (typeof this[key] === "function") this[key] = (<any>this[key]).bind(this);
}
}
get console_out() {
return this.config.console_out;
}
set console_out(value) {
set console_out(value: boolean) {
this.config.console_out = value;
}
@ -122,7 +127,7 @@ export class LoggingBase {
this.checkQueue();
}
events: EventEmitter = new EventEmitter();
public events: EventEmitter = new EventEmitter();
debug(...message: any[]) {
this.message(LoggingTypes.Debug, message);
@ -211,7 +216,7 @@ export class LoggingBase {
}
private writeMessageToFile(message: string, error?: boolean) {
if (!this.writeLock.locked && !this.fileStream && !(error || this.errorStream)) return;
if (this.setted_up && (!this.fileStream || (error && !this.errorStream))) return;
this.queue.push({ message: message.replace("\n", " "), error: error });
this.checkQueue();
}
@ -256,21 +261,12 @@ export class LoggingBase {
private async initializeFile(file: string, new_file = true): Promise<{ stream: fs.WriteStream, size: number }> {
try {
await new Promise((resolve, reject) => {
const folder = path.dirname(file);
if (folder)
fs.exists(folder, (exists) => {
if (!exists) {
fs.mkdir(folder, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
} else resolve();
});
});
const folder = path.dirname(file);
if (folder) {
if (!await fsExists(folder)) {
await fsMkDir(folder).catch(() => { }); //Could happen, if two seperate logger want to create folder so ignoring
}
}
let size = 0;
if (await fsExists(file)) {
@ -360,7 +356,7 @@ function fsExists(path) {
function fsMkDir(path) {
return new Promise((resolve, reject) => {
fs.exists(path, resolve);
fs.mkdir(path, (err) => err ? reject(err) : resolve());
});
}

View File

@ -11,11 +11,16 @@ Logging.log("\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m TEST \x1b[31m\x1b[
let err = new Error()
if (typeof err.stack !== "string") console.log("Stacktrace invalid", err.stack)
Logging.console_out = false;
Logging.waitForSetup().then(() => {
for (let i = 0; i < 7000; i++) {
Logging.log(randomBytes(50000).toString("hex"))
}
});
let cus = new LoggingBase({ name: "test" });
cus.log("Hello from custom Logger")
cus.log("Hello from custom Logger")
let cus2 = new LoggingBase("test2");
cus2.log("Hello from custom Logger 2")
// Logging.console_out = false;
// Logging.waitForSetup().then(() => {
// for (let i = 0; i < 7000; i++) {
// Logging.log(randomBytes(50000).toString("hex"))
// }
// });