mirror of
https://git.stamm.me/OpenServer/NodeLogging.git
synced 2024-11-25 09:59:23 +00:00
Adding simplified custom logger initialisation
This commit is contained in:
parent
51c519ce75
commit
912a4b3ad8
2
out/index.d.ts
vendored
2
out/index.d.ts
vendored
@ -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;
|
||||
|
43
out/index.js
43
out/index.js
@ -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 };
|
||||
}
|
||||
if (options.errorfile === undefined) {
|
||||
options.errorfile = `./logs/error.${options.name}.log`;
|
||||
else {
|
||||
opt = options;
|
||||
}
|
||||
if (opt.name) {
|
||||
if (opt.logfile === undefined) {
|
||||
opt.logfile = `./logs/all.${opt.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);
|
||||
if (folder) {
|
||||
if (!await fsExists(folder)) {
|
||||
await fsMkDir(folder).catch(() => { }); //Could happen, if two seperate logger want to create folder so ignoring
|
||||
}
|
||||
else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
else
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
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
15
out/test.js
15
out/test.js
@ -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
|
@ -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,CAAC,kBAAkB,CAAC,CAAC;AAClC,eAAO,CAAC,YAAY,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;AAE/C,eAAO,CAAC,GAAG,CAAC,gFAAgF,CAAC,CAAA;AAE7F,IAAI,GAAG,GAAG,IAAI,KAAK,EAAE,CAAA;AACrB,IAAI,OAAO,GAAG,CAAC,KAAK,KAAK,QAAQ;IAAE,OAAO,CAAC,GAAG,CAAC,oBAAoB,EAAE,GAAG,CAAC,KAAK,CAAC,CAAA;AAC/E,eAAO,CAAC,WAAW,GAAG,KAAK,CAAC;AAC5B,eAAO,CAAC,YAAY,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE;IAC9B,KAAK,IAAI,CAAC,GAAG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CAAC,CAAA;AAEtC,+BAA+B;AAC/B,sCAAsC;AACtC,sCAAsC;AACtC,wDAAwD;AACxD,OAAO;AACP,MAAM"}
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hibas123/nodelogging",
|
||||
"version": "1.3.14",
|
||||
"version": "1.3.15",
|
||||
"description": "",
|
||||
"main": "out/index.js",
|
||||
"types": "out/index.d.ts",
|
||||
|
48
src/index.ts
48
src/index.ts
@ -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();
|
||||
if (folder) {
|
||||
if (!await fsExists(folder)) {
|
||||
await fsMkDir(folder).catch(() => { }); //Could happen, if two seperate logger want to create folder so ignoring
|
||||
}
|
||||
}
|
||||
});
|
||||
} else resolve();
|
||||
});
|
||||
});
|
||||
|
||||
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());
|
||||
});
|
||||
}
|
||||
|
||||
|
17
src/test.ts
17
src/test.ts
@ -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")
|
||||
|
||||
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"))
|
||||
// }
|
||||
// });
|
Loading…
Reference in New Issue
Block a user