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

2
out/index.d.ts vendored
View File

@ -52,7 +52,7 @@ export declare class LoggingBase {
private fileSize;
private errorSize;
private queue;
constructor(options?: Partial<LoggingBaseOptions>);
constructor(options?: Partial<LoggingBaseOptions> | string);
console_out: boolean;
waitForSetup(): Promise<void>;
private setup;

View File

@ -40,14 +40,21 @@ class LoggingBase {
this.errorSize = 0;
this.queue = new Array();
this.events = new events_1.EventEmitter();
let opt;
if (!options)
options = {};
if (options.name) {
if (options.logfile === undefined) {
options.logfile = `./logs/all.${options.name}.log`;
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({
@ -55,7 +62,7 @@ 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] = this[key].bind(this);
@ -170,7 +177,7 @@ class LoggingBase {
this.events.emit("message", { type: type, message: mb });
}
writeMessageToFile(message, error) {
if (!this.writeLock.locked && !this.fileStream && !(error || this.errorStream))
if (this.setted_up && (!this.fileStream || (error && !this.errorStream)))
return;
this.queue.push({ message: message.replace("\n", " "), error: error });
this.checkQueue();
@ -218,24 +225,12 @@ class LoggingBase {
}
async initializeFile(file, new_file = true) {
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)) {
let stats = await fsStat(file);
@ -321,7 +316,7 @@ function fsExists(path) {
}
function fsMkDir(path) {
return new Promise((resolve, reject) => {
fs.exists(path, resolve);
fs.mkdir(path, (err) => err ? reject(err) : resolve());
});
}
function getStack() {

File diff suppressed because one or more lines are too long

View File

@ -1,7 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("./index");
const crypto_1 = require("crypto");
index_1.Logging.log("test");
index_1.Logging.log("i", "am", { a: "an" }, 1000);
index_1.Logging.error(new Error("fehler 001"));
@ -11,12 +10,14 @@ index_1.Logging.log("\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m TEST \x1b[
let err = new Error();
if (typeof err.stack !== "string")
console.log("Stacktrace invalid", err.stack);
index_1.Logging.console_out = false;
index_1.Logging.waitForSetup().then(() => {
for (let i = 0; i < 7000; i++) {
index_1.Logging.log(crypto_1.randomBytes(50000).toString("hex"));
}
});
let cus = new index_1.LoggingBase({ name: "test" });
cus.log("Hello from custom Logger");
let cus2 = new index_1.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"))
// }
// });
//# sourceMappingURL=test.js.map

View File

@ -1 +1 @@
{"version":3,"file":"test.js","sourceRoot":"","sources":["../src/test.ts"],"names":[],"mappings":";;AAAA,mCAA+C;AAC/C,mCAAqC;AAErC,eAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;AACnB,eAAO,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC;AAC1C,eAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC;AACvC,eAAO,CAAC,KAAK,CA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