mirror of
https://git.stamm.me/OpenServer/NodeLogging.git
synced 2024-12-22 19:09:24 +00:00
Exporting file handling to seperate class
This commit is contained in:
parent
912a4b3ad8
commit
59c571e860
21
out/index.d.ts
vendored
21
out/index.d.ts
vendored
@ -45,17 +45,11 @@ export interface LoggingBaseOptions {
|
||||
}
|
||||
export declare class LoggingBase {
|
||||
private config;
|
||||
private writeLock;
|
||||
private setted_up;
|
||||
private fileStream;
|
||||
private errorStream;
|
||||
private fileSize;
|
||||
private errorSize;
|
||||
private queue;
|
||||
private logFile;
|
||||
private errorFile;
|
||||
constructor(options?: Partial<LoggingBaseOptions> | string);
|
||||
console_out: boolean;
|
||||
waitForSetup(): Promise<void>;
|
||||
private setup;
|
||||
waitForSetup(): Promise<any[]>;
|
||||
events: EventEmitter;
|
||||
debug(...message: any[]): void;
|
||||
log(...message: any[]): void;
|
||||
@ -63,12 +57,7 @@ export declare class LoggingBase {
|
||||
logWithCustomColors(type: LoggingTypes, colors: string, ...message: any[]): void;
|
||||
error(error: Error | string): void;
|
||||
errorMessage(...message: any[]): void;
|
||||
private message;
|
||||
private writeMessageToFile;
|
||||
private checkQueue;
|
||||
private writeToLogFile;
|
||||
private writeToErrorFile;
|
||||
private initializeFile;
|
||||
private message(type, message, customColors?, caller?);
|
||||
}
|
||||
export declare let Logging: LoggingBase;
|
||||
export default Logging;
|
||||
@ -76,5 +65,5 @@ export declare enum LoggingTypes {
|
||||
Log = 0,
|
||||
Warning = 1,
|
||||
Error = 2,
|
||||
Debug = 3
|
||||
Debug = 3,
|
||||
}
|
||||
|
205
out/index.js
205
out/index.js
@ -32,13 +32,87 @@ exports.Colors = {
|
||||
};
|
||||
const maxFileSize = 500000000;
|
||||
const OriginalErrorStackFunction = Error.prototype.prepareStackTrace;
|
||||
class LoggingFiles {
|
||||
constructor(file) {
|
||||
this.size = 0;
|
||||
this.stream = undefined;
|
||||
this.lock = new lock_1.default();
|
||||
this.queue = [];
|
||||
this.file = path.resolve(file);
|
||||
this.init();
|
||||
}
|
||||
static getFile(filename) {
|
||||
filename = path.resolve(filename);
|
||||
let file = this.files.find(e => e.file === filename);
|
||||
if (!file) {
|
||||
file = new LoggingFiles(filename);
|
||||
this.files.push(file);
|
||||
}
|
||||
return file;
|
||||
}
|
||||
async awaitinit() {
|
||||
(await this.lock.getLock()).release();
|
||||
}
|
||||
async init() {
|
||||
let lock = await this.lock.getLock();
|
||||
await this.initializeFile();
|
||||
lock.release();
|
||||
this.checkQueue();
|
||||
}
|
||||
async initializeFile(new_file = false) {
|
||||
try {
|
||||
const folder = path.dirname(this.file);
|
||||
if (folder) {
|
||||
if (!await fsExists(folder)) {
|
||||
await fsMkDir(folder).catch(() => { }); //Could happen, if two seperate instances want to create the same folder so ignoring
|
||||
}
|
||||
}
|
||||
let size = 0;
|
||||
if (await fsExists(this.file)) {
|
||||
let stats = await fsStat(this.file);
|
||||
if (new_file || stats.size > maxFileSize) {
|
||||
if (await fsExists(this.file + ".old"))
|
||||
await fsUnlink(this.file + ".old");
|
||||
await fsMove(this.file, this.file + ".old");
|
||||
}
|
||||
else {
|
||||
size = stats.size;
|
||||
}
|
||||
}
|
||||
this.stream = fs.createWriteStream(this.file, { flags: "a" });
|
||||
this.size = size;
|
||||
}
|
||||
catch (e) {
|
||||
console.log(e);
|
||||
//ToDo is this the right behavior?
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
async checkQueue() {
|
||||
if (this.lock.locked)
|
||||
return;
|
||||
let lock = await this.lock.getLock();
|
||||
let msg;
|
||||
while (msg = this.queue.shift()) {
|
||||
await this.write_to_file(msg);
|
||||
}
|
||||
lock.release();
|
||||
}
|
||||
async write_to_file(data) {
|
||||
if (data.byteLength < maxFileSize && this.size + data.byteLength > maxFileSize) {
|
||||
let f = await this.initializeFile(true);
|
||||
}
|
||||
this.size += data.byteLength;
|
||||
this.stream.write(data);
|
||||
}
|
||||
write(data) {
|
||||
this.queue.push(data);
|
||||
this.checkQueue();
|
||||
}
|
||||
}
|
||||
LoggingFiles.files = [];
|
||||
class LoggingBase {
|
||||
constructor(options) {
|
||||
this.writeLock = new lock_1.default();
|
||||
this.setted_up = false;
|
||||
this.fileSize = 0;
|
||||
this.errorSize = 0;
|
||||
this.queue = new Array();
|
||||
this.events = new events_1.EventEmitter();
|
||||
let opt;
|
||||
if (!options)
|
||||
@ -67,6 +141,12 @@ class LoggingBase {
|
||||
if (typeof this[key] === "function")
|
||||
this[key] = this[key].bind(this);
|
||||
}
|
||||
if (this.config.logfile) {
|
||||
this.logFile = LoggingFiles.getFile(this.config.logfile);
|
||||
}
|
||||
if (this.config.errorfile) {
|
||||
this.errorFile = LoggingFiles.getFile(this.config.errorfile);
|
||||
}
|
||||
}
|
||||
get console_out() {
|
||||
return this.config.console_out;
|
||||
@ -74,24 +154,13 @@ class LoggingBase {
|
||||
set console_out(value) {
|
||||
this.config.console_out = value;
|
||||
}
|
||||
async waitForSetup() {
|
||||
(await this.writeLock.getLock()).release();
|
||||
}
|
||||
async setup() {
|
||||
this.setted_up = true;
|
||||
let lock = await this.writeLock.getLock();
|
||||
if (this.config.logfile) {
|
||||
let f = await this.initializeFile(this.config.logfile, true);
|
||||
this.fileStream = f.stream;
|
||||
this.fileSize = f.size;
|
||||
}
|
||||
if (this.config.errorfile) {
|
||||
let f = await this.initializeFile(this.config.errorfile, false);
|
||||
this.errorStream = f.stream;
|
||||
this.errorSize = f.size;
|
||||
}
|
||||
lock.release();
|
||||
this.checkQueue();
|
||||
waitForSetup() {
|
||||
let w = [];
|
||||
if (this.logFile)
|
||||
w.push(this.logFile.awaitinit());
|
||||
if (this.errorFile)
|
||||
w.push(this.errorFile.awaitinit());
|
||||
return Promise.all(w);
|
||||
}
|
||||
debug(...message) {
|
||||
this.message(LoggingTypes.Debug, message);
|
||||
@ -119,7 +188,7 @@ class LoggingBase {
|
||||
errorMessage(...message) {
|
||||
this.message(LoggingTypes.Error, message);
|
||||
}
|
||||
async message(type, message, customColors, caller) {
|
||||
message(type, message, customColors, caller) {
|
||||
var consoleLogFormat = exports.Colors.Reset;
|
||||
if (!customColors) {
|
||||
switch (type) {
|
||||
@ -145,10 +214,10 @@ class LoggingBase {
|
||||
mb = message;
|
||||
}
|
||||
else {
|
||||
message.forEach(e => {
|
||||
message.forEach((e, i) => {
|
||||
if (typeof e !== "string")
|
||||
e = util.inspect(e, false, null);
|
||||
if (e.endsWith("\n")) {
|
||||
if (e.endsWith("\n") || i === message.length - 1) {
|
||||
mb += e;
|
||||
}
|
||||
else {
|
||||
@ -167,89 +236,21 @@ class LoggingBase {
|
||||
message_lines.forEach(line => console.log(consoleLogFormat + prefix + line + exports.Colors.Reset));
|
||||
}
|
||||
let m = message_lines.join("\n");
|
||||
m = m.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, "");
|
||||
let index = m.indexOf("\x1b");
|
||||
while (index >= 0) {
|
||||
m = m.substring(0, index) + m.substring(index + 5, m.length);
|
||||
index = m.indexOf("\x1b");
|
||||
}
|
||||
m = m.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, "");
|
||||
this.writeMessageToFile(m, type === LoggingTypes.Error);
|
||||
let data = Buffer.from(m + "\n");
|
||||
if (type === LoggingTypes.Error && this.errorFile) {
|
||||
this.errorFile.write(data);
|
||||
}
|
||||
if (this.logFile) {
|
||||
this.logFile.write(data);
|
||||
}
|
||||
this.events.emit("message", { type: type, message: mb });
|
||||
}
|
||||
writeMessageToFile(message, error) {
|
||||
if (this.setted_up && (!this.fileStream || (error && !this.errorStream)))
|
||||
return;
|
||||
this.queue.push({ message: message.replace("\n", " "), error: error });
|
||||
this.checkQueue();
|
||||
}
|
||||
async checkQueue() {
|
||||
try {
|
||||
if (this.writeLock.locked)
|
||||
return;
|
||||
if (this.queue.length <= 0)
|
||||
return;
|
||||
if (!this.setted_up)
|
||||
return this.setup();
|
||||
let lock = await this.writeLock.getLock();
|
||||
var message = this.queue.shift();
|
||||
message.message += "\n";
|
||||
let data = new Buffer(message.message, "utf8");
|
||||
await this.writeToLogFile(data);
|
||||
if (message.error)
|
||||
await this.writeToErrorFile(data);
|
||||
lock.release();
|
||||
if (this.queue.length > 0)
|
||||
this.checkQueue();
|
||||
}
|
||||
catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
async writeToLogFile(data) {
|
||||
if (data.byteLength < maxFileSize && this.fileSize + data.byteLength > maxFileSize) {
|
||||
let f = await this.initializeFile(this.config.logfile, true);
|
||||
this.fileStream = f.stream;
|
||||
this.fileSize = f.size;
|
||||
}
|
||||
this.fileSize += data.byteLength;
|
||||
this.fileStream.write(data);
|
||||
}
|
||||
async writeToErrorFile(data) {
|
||||
if (data.byteLength < maxFileSize && this.errorSize + data.byteLength > maxFileSize) {
|
||||
let f = await this.initializeFile(this.config.errorfile, true);
|
||||
this.errorStream = f.stream;
|
||||
this.errorSize = f.size;
|
||||
}
|
||||
this.errorSize += data.byteLength;
|
||||
this.errorStream.write(data);
|
||||
}
|
||||
async initializeFile(file, new_file = true) {
|
||||
try {
|
||||
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);
|
||||
if (new_file || stats.size > maxFileSize) {
|
||||
if (await fsExists(file + ".old"))
|
||||
await fsUnlink(file + ".old");
|
||||
await fsMove(file, file + ".old");
|
||||
}
|
||||
else {
|
||||
size = stats.size;
|
||||
}
|
||||
}
|
||||
return { stream: fs.createWriteStream(file, { flags: "a" }), size: size };
|
||||
}
|
||||
catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
return { size: 0, stream: undefined };
|
||||
}
|
||||
}
|
||||
exports.LoggingBase = LoggingBase;
|
||||
exports.Logging = undefined;
|
||||
|
File diff suppressed because one or more lines are too long
4
out/lock.d.ts
vendored
4
out/lock.d.ts
vendored
@ -7,6 +7,6 @@ export default class Lock {
|
||||
private toCome;
|
||||
constructor();
|
||||
getLock(): Promise<Release>;
|
||||
private lock;
|
||||
private release;
|
||||
private lock();
|
||||
private release();
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
{"version":3,"file":"lock.js","sourceRoot":"","sources":["../src/lock.ts"],"names":[],"mappings":";;AACA,MAAqB,IAAI;IAOtB;QANQ,YAAO,GAAY,KAAK,CAAC;QAIzB,WAAM,GAAmB,EAAE,CAAC;QAGjC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC1C,CAAC;IAPD,IAAI,MAAM;QACP,OAAO,IAAI,CAAC,OAAO,CAAC;IACvB,CAAC;IAOD,KAAK,CAAC,OAAO;QACV,IAAI,CAAC,IAAI,CAAC,OAAO;YAAE,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,CAAC;aAC9C;YACF,OAAO,IAAI,OAAO,CAAU,CAAC,OAAO,EAAE,EAAE;gBACrC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE;oBACnB,OAAO,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;gBACrC,CAAC,CAAC,CAAA;YACL,CAAC,CAAC,CAAA;SACJ;IACJ,CAAC;IAEO,IAAI;QACT,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,OAAO,IAAI,CAAC,OAAO,CAAC;IACvB,CAAC;IAEO,KAAK,CAAC,OAAO;QAClB,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;YACzB,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC;SACxB;aAAM;YACJ,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;SACvB;IACJ,CAAC;CACH;AAlCD,uBAkCC"}
|
||||
{"version":3,"file":"lock.js","sourceRoot":"","sources":["../src/lock.ts"],"names":[],"mappings":";;AACA;IAOG;QANQ,YAAO,GAAY,KAAK,CAAC;QAIzB,WAAM,GAAmB,EAAE,CAAC;QAGjC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC1C,CAAC;IAPD,IAAI,MAAM;QACP,OAAO,IAAI,CAAC,OAAO,CAAC;IACvB,CAAC;IAOD,KAAK,CAAC,OAAO;QACV,IAAI,CAAC,IAAI,CAAC,OAAO;YAAE,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,CAAC;aAC9C;YACF,OAAO,IAAI,OAAO,CAAU,CAAC,OAAO,EAAE,EAAE;gBACrC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE;oBACnB,OAAO,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;gBACrC,CAAC,CAAC,CAAA;YACL,CAAC,CAAC,CAAA;SACJ;IACJ,CAAC;IAEO,IAAI;QACT,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,OAAO,IAAI,CAAC,OAAO,CAAC;IACvB,CAAC;IAEO,KAAK,CAAC,OAAO;QAClB,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;YACzB,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC;SACxB;aAAM;YACJ,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;SACvB;IACJ,CAAC;CACH;AAlCD,uBAkCC"}
|
13
out/test.js
13
out/test.js
@ -14,7 +14,18 @@ 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;
|
||||
let cus22 = new index_1.LoggingBase("test2");
|
||||
cus22.log("Hello from custom Logger 22");
|
||||
cus2.log("Hello from custom Logger 2");
|
||||
cus22.log("Hello from custom Logger 22");
|
||||
cus2.log("Hello from custom Logger 2");
|
||||
cus22.log("Hello from custom Logger 22");
|
||||
cus2.log("Hello from custom Logger 2");
|
||||
cus22.log("Hello from custom Logger 22");
|
||||
cus2.log("Hello from custom Logger 2");
|
||||
cus22.log("Hello from custom Logger 22");
|
||||
cus2.log("Hello from custom Logger 2");
|
||||
index_1.Logging.console_out = false;
|
||||
// Logging.waitForSetup().then(() => {
|
||||
// for (let i = 0; i < 7000; i++) {
|
||||
// Logging.log(randomBytes(50000).toString("hex"))
|
||||
|
@ -1 +1 @@
|
||||
{"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"}
|
||||
{"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,IAAI,KAAK,GAAG,IAAI,mBAAW,CAAC,OAAO,CAAC,CAAC;AACrC,KAAK,CAAC,GAAG,CAAC,6BAA6B,CAAC,CAAA;AACxC,IAAI,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAA;AACtC,KAAK,CAAC,GAAG,CAAC,6BAA6B,CAAC,CAAA;AACxC,IAAI,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAA;AACtC,KAAK,CAAC,GAAG,CAAC,6BAA6B,CAAC,CAAA;AACxC,IAAI,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAA;AACtC,KAAK,CAAC,GAAG,CAAC,6BAA6B,CAAC,CAAA;AACxC,IAAI,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAA;AACtC,KAAK,CAAC,GAAG,CAAC,6BAA6B,CAAC,CAAA;AACxC,IAAI,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAA;AAEtC,eAAO,CAAC,WAAW,GAAG,KAAK,CAAC;AAC5B,sCAAsC;AACtC,sCAAsC;AACtC,wDAAwD;AACxD,OAAO;AACP,MAAM"}
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hibas123/nodelogging",
|
||||
"version": "1.3.15",
|
||||
"version": "1.3.16",
|
||||
"description": "",
|
||||
"main": "out/index.js",
|
||||
"types": "out/index.d.ts",
|
||||
|
228
src/index.ts
228
src/index.ts
@ -55,18 +55,99 @@ export interface LoggingBaseOptions {
|
||||
console_out: boolean;
|
||||
}
|
||||
|
||||
class LoggingFiles {
|
||||
private static files: LoggingFiles[] = [];
|
||||
static getFile(filename: string): LoggingFiles {
|
||||
filename = path.resolve(filename);
|
||||
let file = this.files.find(e => e.file === filename);
|
||||
if (!file) {
|
||||
file = new LoggingFiles(filename);
|
||||
this.files.push(file);
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
private file: string;
|
||||
private size: number = 0;
|
||||
private stream: fs.WriteStream = undefined;
|
||||
private lock = new Lock();
|
||||
private constructor(file: string) {
|
||||
this.file = path.resolve(file);
|
||||
this.init();
|
||||
}
|
||||
|
||||
public async awaitinit() {
|
||||
(await this.lock.getLock()).release();
|
||||
}
|
||||
|
||||
private async init() {
|
||||
let lock = await this.lock.getLock();
|
||||
await this.initializeFile()
|
||||
lock.release();
|
||||
this.checkQueue()
|
||||
}
|
||||
|
||||
private async initializeFile(new_file = false) {
|
||||
try {
|
||||
const folder = path.dirname(this.file);
|
||||
if (folder) {
|
||||
if (!await fsExists(folder)) {
|
||||
await fsMkDir(folder).catch(() => { }); //Could happen, if two seperate instances want to create the same folder so ignoring
|
||||
}
|
||||
}
|
||||
|
||||
let size = 0;
|
||||
if (await fsExists(this.file)) {
|
||||
let stats = await fsStat(this.file);
|
||||
if (new_file || stats.size > maxFileSize) {
|
||||
if (await fsExists(this.file + ".old"))
|
||||
await fsUnlink(this.file + ".old");
|
||||
await fsMove(this.file, this.file + ".old")
|
||||
} else {
|
||||
size = stats.size;
|
||||
}
|
||||
}
|
||||
|
||||
this.stream = fs.createWriteStream(this.file, { flags: "a" })
|
||||
this.size = size;
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
//ToDo is this the right behavior?
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
private queue: Buffer[] = [];
|
||||
|
||||
async checkQueue() {
|
||||
if (this.lock.locked) return;
|
||||
let lock = await this.lock.getLock();
|
||||
let msg: Buffer;
|
||||
while (msg = this.queue.shift()) {
|
||||
await this.write_to_file(msg);
|
||||
}
|
||||
lock.release();
|
||||
}
|
||||
|
||||
private async write_to_file(data: Buffer) {
|
||||
if (data.byteLength < maxFileSize && this.size + data.byteLength > maxFileSize) {
|
||||
let f = await this.initializeFile(true);
|
||||
}
|
||||
this.size += data.byteLength;
|
||||
this.stream.write(data);
|
||||
}
|
||||
|
||||
public write(data: Buffer) {
|
||||
this.queue.push(data);
|
||||
this.checkQueue()
|
||||
}
|
||||
}
|
||||
|
||||
export class LoggingBase {
|
||||
private config: LoggingBaseOptions;
|
||||
private writeLock = new Lock();
|
||||
|
||||
private setted_up = false;
|
||||
|
||||
private fileStream: fs.WriteStream;
|
||||
private errorStream: fs.WriteStream;
|
||||
private fileSize: number = 0;
|
||||
private errorSize: number = 0;
|
||||
|
||||
private queue = new Array<{ message: string, error: boolean }>();
|
||||
private logFile: LoggingFiles;
|
||||
private errorFile: LoggingFiles;
|
||||
|
||||
constructor(options?: Partial<LoggingBaseOptions> | string) {
|
||||
let opt: Partial<LoggingBaseOptions>;
|
||||
@ -95,6 +176,14 @@ export class LoggingBase {
|
||||
for (let key in this) {
|
||||
if (typeof this[key] === "function") this[key] = (<any>this[key]).bind(this);
|
||||
}
|
||||
|
||||
if (this.config.logfile) {
|
||||
this.logFile = LoggingFiles.getFile(this.config.logfile);
|
||||
}
|
||||
|
||||
if (this.config.errorfile) {
|
||||
this.errorFile = LoggingFiles.getFile(this.config.errorfile);
|
||||
}
|
||||
}
|
||||
|
||||
get console_out() {
|
||||
@ -105,26 +194,11 @@ export class LoggingBase {
|
||||
this.config.console_out = value;
|
||||
}
|
||||
|
||||
public async waitForSetup() {
|
||||
(await this.writeLock.getLock()).release();
|
||||
}
|
||||
|
||||
private async setup() {
|
||||
this.setted_up = true;
|
||||
let lock = await this.writeLock.getLock();
|
||||
if (this.config.logfile) {
|
||||
let f = await this.initializeFile(this.config.logfile, true);
|
||||
this.fileStream = f.stream;
|
||||
this.fileSize = f.size;
|
||||
}
|
||||
|
||||
if (this.config.errorfile) {
|
||||
let f = await this.initializeFile(this.config.errorfile, false);
|
||||
this.errorStream = f.stream;
|
||||
this.errorSize = f.size;
|
||||
}
|
||||
lock.release();
|
||||
this.checkQueue();
|
||||
public waitForSetup() {
|
||||
let w = [];
|
||||
if (this.logFile) w.push(this.logFile.awaitinit());
|
||||
if (this.errorFile) w.push(this.errorFile.awaitinit());
|
||||
return Promise.all(w)
|
||||
}
|
||||
|
||||
public events: EventEmitter = new EventEmitter();
|
||||
@ -159,7 +233,7 @@ export class LoggingBase {
|
||||
this.message(LoggingTypes.Error, message);
|
||||
}
|
||||
|
||||
private async message(type: LoggingTypes, message: any[] | string, customColors?: string, caller?: { file: string, line: number }) {
|
||||
private message(type: LoggingTypes, message: any[] | string, customColors?: string, caller?: { file: string, line: number }) {
|
||||
var consoleLogFormat = Colors.Reset;
|
||||
if (!customColors) {
|
||||
switch (type) {
|
||||
@ -183,9 +257,9 @@ export class LoggingBase {
|
||||
if (typeof message === "string") {
|
||||
mb = message;
|
||||
} else {
|
||||
message.forEach(e => {
|
||||
message.forEach((e, i) => {
|
||||
if (typeof e !== "string") e = util.inspect(e, false, null);
|
||||
if (e.endsWith("\n")) {
|
||||
if (e.endsWith("\n") || i === message.length - 1) {
|
||||
mb += e;
|
||||
} else {
|
||||
mb += e + " ";
|
||||
@ -204,88 +278,24 @@ export class LoggingBase {
|
||||
}
|
||||
|
||||
let m = message_lines.join("\n");
|
||||
|
||||
m = m.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, "");
|
||||
|
||||
let index = m.indexOf("\x1b");
|
||||
while (index >= 0) {
|
||||
m = m.substring(0, index) + m.substring(index + 5, m.length);
|
||||
index = m.indexOf("\x1b");
|
||||
}
|
||||
|
||||
m = m.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, "");
|
||||
this.writeMessageToFile(m, type === LoggingTypes.Error);
|
||||
let data = Buffer.from(m + "\n")
|
||||
if (type === LoggingTypes.Error && this.errorFile) {
|
||||
this.errorFile.write(data);
|
||||
}
|
||||
if (this.logFile) {
|
||||
this.logFile.write(data);
|
||||
}
|
||||
this.events.emit("message", { type: type, message: mb });
|
||||
}
|
||||
|
||||
private writeMessageToFile(message: string, error?: boolean) {
|
||||
if (this.setted_up && (!this.fileStream || (error && !this.errorStream))) return;
|
||||
this.queue.push({ message: message.replace("\n", " "), error: error });
|
||||
this.checkQueue();
|
||||
}
|
||||
|
||||
private async checkQueue() {
|
||||
try {
|
||||
if (this.writeLock.locked) return;
|
||||
if (this.queue.length <= 0) return;
|
||||
if (!this.setted_up) return this.setup();
|
||||
let lock = await this.writeLock.getLock();
|
||||
var message = this.queue.shift();
|
||||
message.message += "\n";
|
||||
let data = new Buffer(message.message, "utf8");
|
||||
await this.writeToLogFile(data);
|
||||
if (message.error) await this.writeToErrorFile(data);
|
||||
lock.release();
|
||||
if (this.queue.length > 0) this.checkQueue();
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
private async writeToLogFile(data: Buffer) {
|
||||
if (data.byteLength < maxFileSize && this.fileSize + data.byteLength > maxFileSize) {
|
||||
let f = await this.initializeFile(this.config.logfile, true);
|
||||
this.fileStream = f.stream;
|
||||
this.fileSize = f.size;
|
||||
}
|
||||
this.fileSize += data.byteLength;
|
||||
this.fileStream.write(data);
|
||||
}
|
||||
|
||||
private async writeToErrorFile(data: Buffer) {
|
||||
if (data.byteLength < maxFileSize && this.errorSize + data.byteLength > maxFileSize) {
|
||||
let f = await this.initializeFile(this.config.errorfile, true);
|
||||
this.errorStream = f.stream;
|
||||
this.errorSize = f.size;
|
||||
}
|
||||
this.errorSize += data.byteLength;
|
||||
this.errorStream.write(data);
|
||||
}
|
||||
|
||||
private async initializeFile(file: string, new_file = true): Promise<{ stream: fs.WriteStream, size: number }> {
|
||||
try {
|
||||
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);
|
||||
if (new_file || stats.size > maxFileSize) {
|
||||
if (await fsExists(file + ".old"))
|
||||
await fsUnlink(file + ".old");
|
||||
await fsMove(file, file + ".old")
|
||||
} else {
|
||||
size = stats.size;
|
||||
}
|
||||
}
|
||||
|
||||
return { stream: fs.createWriteStream(file, { flags: "a" }), size: size };
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
return { size: 0, stream: undefined };
|
||||
}
|
||||
}
|
||||
|
||||
export let Logging: LoggingBase = undefined;
|
||||
@ -312,7 +322,7 @@ function fsStat(path: string) {
|
||||
})
|
||||
}
|
||||
|
||||
function fsMove(oldPath, newPath) {
|
||||
function fsMove(oldPath: string, newPath: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
let callback = (err?) => {
|
||||
@ -348,13 +358,13 @@ function fsMove(oldPath, newPath) {
|
||||
})
|
||||
}
|
||||
|
||||
function fsExists(path) {
|
||||
function fsExists(path: string) {
|
||||
return new Promise<boolean>((resolve, reject) => {
|
||||
fs.exists(path, resolve);
|
||||
});
|
||||
}
|
||||
|
||||
function fsMkDir(path) {
|
||||
function fsMkDir(path: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.mkdir(path, (err) => err ? reject(err) : resolve());
|
||||
});
|
||||
|
14
src/test.ts
14
src/test.ts
@ -18,7 +18,19 @@ cus.log("Hello from custom Logger")
|
||||
let cus2 = new LoggingBase("test2");
|
||||
cus2.log("Hello from custom Logger 2")
|
||||
|
||||
// Logging.console_out = false;
|
||||
let cus22 = new LoggingBase("test2");
|
||||
cus22.log("Hello from custom Logger 22")
|
||||
cus2.log("Hello from custom Logger 2")
|
||||
cus22.log("Hello from custom Logger 22")
|
||||
cus2.log("Hello from custom Logger 2")
|
||||
cus22.log("Hello from custom Logger 22")
|
||||
cus2.log("Hello from custom Logger 2")
|
||||
cus22.log("Hello from custom Logger 22")
|
||||
cus2.log("Hello from custom Logger 2")
|
||||
cus22.log("Hello from custom Logger 22")
|
||||
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