mirror of
https://git.stamm.me/OpenServer/NodeLogging.git
synced 2024-11-25 08:59:23 +00:00
Adding config option to set console output name for additional Logging objects.
This commit is contained in:
parent
cf4938d0db
commit
4918fe9b17
4
out/index.d.ts
vendored
4
out/index.d.ts
vendored
@ -26,6 +26,10 @@ export declare const Colors: {
|
||||
BgWhite: string;
|
||||
};
|
||||
export interface LoggingBaseOptions {
|
||||
/**
|
||||
* Name will be prefixed on Console output
|
||||
*/
|
||||
name: string;
|
||||
logfile: string;
|
||||
errorfile: string;
|
||||
console_out: boolean;
|
||||
|
23
out/index.js
23
out/index.js
@ -42,6 +42,7 @@ class LoggingBase {
|
||||
if (!options)
|
||||
options = {};
|
||||
this.config = Object.assign({
|
||||
name: undefined,
|
||||
console_out: true,
|
||||
logfile: "./logs/all.log",
|
||||
errorfile: "./logs/error.log"
|
||||
@ -137,8 +138,12 @@ class LoggingBase {
|
||||
let date = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
|
||||
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 + exports.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 + exports.Colors.Reset));
|
||||
}
|
||||
let m = message_lines.join("\n");
|
||||
let index = m.indexOf("\x1b");
|
||||
while (index >= 0) {
|
||||
@ -227,20 +232,6 @@ 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);
|
||||
|
File diff suppressed because one or more lines are too long
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hibas123/nodelogging",
|
||||
"version": "1.3.3",
|
||||
"version": "1.3.4",
|
||||
"description": "",
|
||||
"main": "out/index.js",
|
||||
"types": "out/index.d.ts",
|
||||
|
27
src/index.ts
27
src/index.ts
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user