mirror of
https://git.stamm.me/OpenServer/NodeLogging.git
synced 2024-11-22 13:29: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 fileSize;
|
||||||
private errorSize;
|
private errorSize;
|
||||||
private queue;
|
private queue;
|
||||||
constructor(options?: Partial<LoggingBaseOptions>);
|
constructor(options?: Partial<LoggingBaseOptions> | string);
|
||||||
console_out: boolean;
|
console_out: boolean;
|
||||||
waitForSetup(): Promise<void>;
|
waitForSetup(): Promise<void>;
|
||||||
private setup;
|
private setup;
|
||||||
|
49
out/index.js
49
out/index.js
@ -40,14 +40,21 @@ class LoggingBase {
|
|||||||
this.errorSize = 0;
|
this.errorSize = 0;
|
||||||
this.queue = new Array();
|
this.queue = new Array();
|
||||||
this.events = new events_1.EventEmitter();
|
this.events = new events_1.EventEmitter();
|
||||||
|
let opt;
|
||||||
if (!options)
|
if (!options)
|
||||||
options = {};
|
opt = {};
|
||||||
if (options.name) {
|
else if (typeof options === "string") {
|
||||||
if (options.logfile === undefined) {
|
opt = { name: options };
|
||||||
options.logfile = `./logs/all.${options.name}.log`;
|
}
|
||||||
|
else {
|
||||||
|
opt = options;
|
||||||
|
}
|
||||||
|
if (opt.name) {
|
||||||
|
if (opt.logfile === undefined) {
|
||||||
|
opt.logfile = `./logs/all.${opt.name}.log`;
|
||||||
}
|
}
|
||||||
if (options.errorfile === undefined) {
|
if (opt.errorfile === undefined) {
|
||||||
options.errorfile = `./logs/error.${options.name}.log`;
|
opt.errorfile = `./logs/error.${opt.name}.log`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.config = Object.assign({
|
this.config = Object.assign({
|
||||||
@ -55,7 +62,7 @@ class LoggingBase {
|
|||||||
console_out: true,
|
console_out: true,
|
||||||
logfile: "./logs/all.log",
|
logfile: "./logs/all.log",
|
||||||
errorfile: "./logs/error.log"
|
errorfile: "./logs/error.log"
|
||||||
}, options);
|
}, opt);
|
||||||
for (let key in this) {
|
for (let key in this) {
|
||||||
if (typeof this[key] === "function")
|
if (typeof this[key] === "function")
|
||||||
this[key] = this[key].bind(this);
|
this[key] = this[key].bind(this);
|
||||||
@ -170,7 +177,7 @@ class LoggingBase {
|
|||||||
this.events.emit("message", { type: type, message: mb });
|
this.events.emit("message", { type: type, message: mb });
|
||||||
}
|
}
|
||||||
writeMessageToFile(message, error) {
|
writeMessageToFile(message, error) {
|
||||||
if (!this.writeLock.locked && !this.fileStream && !(error || this.errorStream))
|
if (this.setted_up && (!this.fileStream || (error && !this.errorStream)))
|
||||||
return;
|
return;
|
||||||
this.queue.push({ message: message.replace("\n", " "), error: error });
|
this.queue.push({ message: message.replace("\n", " "), error: error });
|
||||||
this.checkQueue();
|
this.checkQueue();
|
||||||
@ -218,24 +225,12 @@ class LoggingBase {
|
|||||||
}
|
}
|
||||||
async initializeFile(file, new_file = true) {
|
async initializeFile(file, new_file = true) {
|
||||||
try {
|
try {
|
||||||
await new Promise((resolve, reject) => {
|
const folder = path.dirname(file);
|
||||||
const folder = path.dirname(file);
|
if (folder) {
|
||||||
if (folder)
|
if (!await fsExists(folder)) {
|
||||||
fs.exists(folder, (exists) => {
|
await fsMkDir(folder).catch(() => { }); //Could happen, if two seperate logger want to create folder so ignoring
|
||||||
if (!exists) {
|
}
|
||||||
fs.mkdir(folder, (err) => {
|
}
|
||||||
if (err) {
|
|
||||||
reject(err);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
resolve();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
let size = 0;
|
let size = 0;
|
||||||
if (await fsExists(file)) {
|
if (await fsExists(file)) {
|
||||||
let stats = await fsStat(file);
|
let stats = await fsStat(file);
|
||||||
@ -321,7 +316,7 @@ function fsExists(path) {
|
|||||||
}
|
}
|
||||||
function fsMkDir(path) {
|
function fsMkDir(path) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
fs.exists(path, resolve);
|
fs.mkdir(path, (err) => err ? reject(err) : resolve());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
function getStack() {
|
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";
|
"use strict";
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
const index_1 = require("./index");
|
const index_1 = require("./index");
|
||||||
const crypto_1 = require("crypto");
|
|
||||||
index_1.Logging.log("test");
|
index_1.Logging.log("test");
|
||||||
index_1.Logging.log("i", "am", { a: "an" }, 1000);
|
index_1.Logging.log("i", "am", { a: "an" }, 1000);
|
||||||
index_1.Logging.error(new Error("fehler 001"));
|
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();
|
let err = new Error();
|
||||||
if (typeof err.stack !== "string")
|
if (typeof err.stack !== "string")
|
||||||
console.log("Stacktrace invalid", err.stack);
|
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" });
|
let cus = new index_1.LoggingBase({ name: "test" });
|
||||||
cus.log("Hello from custom Logger");
|
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
|
//# 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,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,EAAE;QAC5B,eAAO,CAAC,GAAG,CAAC,oBAAW,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAA;KACjD;AACJ,CAAC,CAAC,CAAC;AACH,IAAI,GAAG,GAAG,IAAI,mBAAW,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;AAC5C,GAAG,CAAC,GAAG,CAAC,0BAA0B,CAAC,CAAA"}
|
{"version":3,"file":"test.js","sourceRoot":"","sources":["../src/test.ts"],"names":[],"mappings":";;AAAA,mCAA+C;AAG/C,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;AAE/E,IAAI,GAAG,GAAG,IAAI,mBAAW,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;AAC5C,GAAG,CAAC,GAAG,CAAC,0BAA0B,CAAC,CAAA;AAEnC,IAAI,IAAI,GAAG,IAAI,mBAAW,CAAC,OAAO,CAAC,CAAC;AACpC,IAAI,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAA;AAEtC,+BAA+B;AAC/B,sCAAsC;AACtC,sCAAsC;AACtC,wDAAwD;AACxD,OAAO;AACP,MAAM"}
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hibas123/nodelogging",
|
"name": "@hibas123/nodelogging",
|
||||||
"version": "1.3.14",
|
"version": "1.3.15",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "out/index.js",
|
"main": "out/index.js",
|
||||||
"types": "out/index.d.ts",
|
"types": "out/index.d.ts",
|
||||||
|
52
src/index.ts
52
src/index.ts
@ -68,15 +68,21 @@ export class LoggingBase {
|
|||||||
|
|
||||||
private queue = new Array<{ message: string, error: boolean }>();
|
private queue = new Array<{ message: string, error: boolean }>();
|
||||||
|
|
||||||
constructor(options?: Partial<LoggingBaseOptions>) {
|
constructor(options?: Partial<LoggingBaseOptions> | string) {
|
||||||
if (!options) options = {};
|
let opt: Partial<LoggingBaseOptions>;
|
||||||
if (options.name) {
|
if (!options) opt = {}
|
||||||
if (options.logfile === undefined) {
|
else if (typeof options === "string") {
|
||||||
options.logfile = `./logs/all.${options.name}.log`
|
opt = { name: options };
|
||||||
|
} else {
|
||||||
|
opt = options;
|
||||||
|
}
|
||||||
|
if (opt.name) {
|
||||||
|
if (opt.logfile === undefined) {
|
||||||
|
opt.logfile = `./logs/all.${opt.name}.log`
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.errorfile === undefined) {
|
if (opt.errorfile === undefined) {
|
||||||
options.errorfile = `./logs/error.${options.name}.log`
|
opt.errorfile = `./logs/error.${opt.name}.log`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.config = Object.assign(<LoggingBaseOptions>{
|
this.config = Object.assign(<LoggingBaseOptions>{
|
||||||
@ -84,19 +90,18 @@ export class LoggingBase {
|
|||||||
console_out: true,
|
console_out: true,
|
||||||
logfile: "./logs/all.log",
|
logfile: "./logs/all.log",
|
||||||
errorfile: "./logs/error.log"
|
errorfile: "./logs/error.log"
|
||||||
}, options);
|
}, opt);
|
||||||
|
|
||||||
for (let key in this) {
|
for (let key in this) {
|
||||||
if (typeof this[key] === "function") this[key] = (<any>this[key]).bind(this);
|
if (typeof this[key] === "function") this[key] = (<any>this[key]).bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get console_out() {
|
get console_out() {
|
||||||
return this.config.console_out;
|
return this.config.console_out;
|
||||||
}
|
}
|
||||||
|
|
||||||
set console_out(value) {
|
set console_out(value: boolean) {
|
||||||
this.config.console_out = value;
|
this.config.console_out = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,7 +127,7 @@ export class LoggingBase {
|
|||||||
this.checkQueue();
|
this.checkQueue();
|
||||||
}
|
}
|
||||||
|
|
||||||
events: EventEmitter = new EventEmitter();
|
public events: EventEmitter = new EventEmitter();
|
||||||
|
|
||||||
debug(...message: any[]) {
|
debug(...message: any[]) {
|
||||||
this.message(LoggingTypes.Debug, message);
|
this.message(LoggingTypes.Debug, message);
|
||||||
@ -211,7 +216,7 @@ export class LoggingBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private writeMessageToFile(message: string, error?: boolean) {
|
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.queue.push({ message: message.replace("\n", " "), error: error });
|
||||||
this.checkQueue();
|
this.checkQueue();
|
||||||
}
|
}
|
||||||
@ -256,21 +261,12 @@ export class LoggingBase {
|
|||||||
|
|
||||||
private async initializeFile(file: string, new_file = true): Promise<{ stream: fs.WriteStream, size: number }> {
|
private async initializeFile(file: string, new_file = true): Promise<{ stream: fs.WriteStream, size: number }> {
|
||||||
try {
|
try {
|
||||||
await new Promise((resolve, reject) => {
|
const folder = path.dirname(file);
|
||||||
const folder = path.dirname(file);
|
if (folder) {
|
||||||
if (folder)
|
if (!await fsExists(folder)) {
|
||||||
fs.exists(folder, (exists) => {
|
await fsMkDir(folder).catch(() => { }); //Could happen, if two seperate logger want to create folder so ignoring
|
||||||
if (!exists) {
|
}
|
||||||
fs.mkdir(folder, (err) => {
|
}
|
||||||
if (err) {
|
|
||||||
reject(err);
|
|
||||||
} else {
|
|
||||||
resolve();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
let size = 0;
|
let size = 0;
|
||||||
if (await fsExists(file)) {
|
if (await fsExists(file)) {
|
||||||
@ -360,7 +356,7 @@ function fsExists(path) {
|
|||||||
|
|
||||||
function fsMkDir(path) {
|
function fsMkDir(path) {
|
||||||
return new Promise((resolve, reject) => {
|
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()
|
let err = new Error()
|
||||||
if (typeof err.stack !== "string") console.log("Stacktrace invalid", err.stack)
|
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" });
|
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"))
|
||||||
|
// }
|
||||||
|
// });
|
Loading…
Reference in New Issue
Block a user