mirror of
https://git.hibas.dev/OpenServer/NodeLogging.git
synced 2025-07-01 20:51:07 +00:00
Exporting file handling to seperate class
This commit is contained in:
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"))
|
||||
|
Reference in New Issue
Block a user