1
0
mirror of https://git.stamm.me/OpenServer/NodeLogging.git synced 2024-11-15 05:51:05 +00:00
nodelogging/src/index.ts

452 lines
12 KiB
TypeScript
Raw Normal View History

2017-08-23 14:45:03 +00:00
import * as util from "util";
import * as fs from "fs";
import { EventEmitter } from "events";
import * as path from "path";
2018-08-09 17:52:01 +00:00
import Lock from "./lock";
2017-08-23 14:45:03 +00:00
2018-09-01 10:28:15 +00:00
export const Colors = {
Reset: "\x1b[0m",
Bright: "\x1b[1m",
Dim: "\x1b[2m",
Underscore: "\x1b[4m",
Blink: "\x1b[5m",
Reverse: "\x1b[7m",
Hidden: "\x1b[8m",
FgBlack: "\x1b[30m",
FgRed: "\x1b[31m",
FgGreen: "\x1b[32m",
FgYellow: "\x1b[33m",
FgBlue: "\x1b[34m",
FgMagenta: "\x1b[35m",
FgCyan: "\x1b[36m",
FgWhite: "\x1b[37m",
BgBlack: "\x1b[40m",
BgRed: "\x1b[41m",
BgGreen: "\x1b[42m",
BgYellow: "\x1b[43m",
BgBlue: "\x1b[44m",
BgMagenta: "\x1b[45m",
BgCyan: "\x1b[46m",
BgWhite: "\x1b[47m"
}
2017-08-23 14:45:03 +00:00
2018-02-18 15:00:16 +00:00
const maxFileSize = 500000000;
const OriginalErrorStackFunction = (<any>Error.prototype).prepareStackTrace
2018-08-09 17:52:01 +00:00
export interface LoggingBaseOptions {
/**
* Name will be prefixed on Console output and added to logfiles, if not specified here
*/
name: string,
/**
* Filename/path of the logfile. Skip if generated with name
*/
2018-08-09 17:52:01 +00:00
logfile: string;
/**
* Filename/path of the logfile. Skip if generated with name
*/
2018-08-09 17:52:01 +00:00
errorfile: string;
/**
* Prints output to console
*/
2018-08-09 17:52:01 +00:00
console_out: boolean;
}
2017-08-23 14:45:03 +00:00
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;
}
2018-08-09 17:52:01 +00:00
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();
}
2018-09-28 08:05:10 +00:00
public async awaitinit() {
(await this.lock.getLock()).release();
}
2018-08-09 17:52:01 +00:00
private async init() {
let lock = await this.lock.getLock();
await this.initializeFile()
lock.release();
this.checkQueue()
}
private async initializeFile(new_file = false) {
try {
if (this.stream) {
this.stream.close();
}
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) {
try {
if (data.byteLength < maxFileSize && this.size + data.byteLength > maxFileSize) {
await this.initializeFile(true)
}
this.size += data.byteLength;
this.stream.write(data);
} catch (err) {
console.error(err);
this.initializeFile(false);
this.write_to_file(data);
}
}
public write(data: Buffer) {
this.queue.push(data);
this.checkQueue()
}
}
export class LoggingBase {
private config: LoggingBaseOptions;
private logFile: LoggingFiles;
private errorFile: LoggingFiles;
2018-08-09 17:52:01 +00:00
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 (opt.errorfile === undefined) {
opt.errorfile = `./logs/error.${opt.name}.log`
}
}
2018-08-09 17:52:01 +00:00
this.config = Object.assign(<LoggingBaseOptions>{
name: undefined,
2018-08-09 17:52:01 +00:00
console_out: true,
logfile: "./logs/all.log",
errorfile: "./logs/error.log"
}, opt);
2018-09-28 08:05:10 +00:00
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);
}
2018-08-09 17:52:01 +00:00
}
2017-08-23 14:45:03 +00:00
2018-08-09 17:52:01 +00:00
get console_out() {
return this.config.console_out;
}
2017-08-23 14:45:03 +00:00
set console_out(value: boolean) {
2018-08-09 17:52:01 +00:00
this.config.console_out = value;
}
public waitForSetup() {
let w = [];
if (this.logFile) w.push(this.logFile.awaitinit());
if (this.errorFile) w.push(this.errorFile.awaitinit());
return Promise.all(w)
2017-08-23 14:45:03 +00:00
}
public events: EventEmitter = new EventEmitter();
2018-08-09 17:52:01 +00:00
debug(...message: any[]) {
this.message(LoggingTypes.Debug, message);
2017-08-23 14:45:03 +00:00
}
2018-08-09 17:52:01 +00:00
log(...message: any[]) {
this.message(LoggingTypes.Log, message);
2017-08-23 14:45:03 +00:00
}
2018-08-09 17:52:01 +00:00
warning(...message: any[]) {
this.message(LoggingTypes.Warning, message);
2017-09-16 17:21:22 +00:00
}
2018-08-09 17:52:01 +00:00
logWithCustomColors(type: LoggingTypes, colors: string, ...message: any[]) {
this.message(type, message, colors);
}
error(error: Error | string) {
2018-09-17 20:42:22 +00:00
if (!error) error = "Empty ERROR was passed, so no informations available";
2017-08-23 14:45:03 +00:00
if (typeof error === "string") {
let e = new Error()
2018-09-02 10:22:39 +00:00
this.message(LoggingTypes.Error, [error, "\n", e.stack]);
2018-07-01 11:05:34 +00:00
} else {
2018-09-02 10:22:39 +00:00
this.message(LoggingTypes.Error, [error.message, "\n", error.stack], undefined, getCallerFromExisting(error));
2017-08-23 14:45:03 +00:00
}
}
2018-08-09 17:52:01 +00:00
errorMessage(...message: any[]) {
this.message(LoggingTypes.Error, message);
2017-08-23 14:45:03 +00:00
}
private message(type: LoggingTypes, message: any[] | string, customColors?: string, caller?: { file: string, line: number }) {
2018-09-01 10:28:15 +00:00
var consoleLogFormat = Colors.Reset;
2017-09-16 17:21:22 +00:00
if (!customColors) {
switch (type) {
case LoggingTypes.Log:
//m += FgWhite + BgBlack;
break;
case LoggingTypes.Error:
2018-09-01 10:28:15 +00:00
consoleLogFormat += Colors.FgRed;//FgWhite + BgRed + FgWhite;
2017-09-16 17:21:22 +00:00
break;
case LoggingTypes.Debug:
2018-09-01 10:28:15 +00:00
consoleLogFormat += Colors.FgCyan;
2017-09-16 17:21:22 +00:00
break;
case LoggingTypes.Warning:
2018-09-01 10:28:15 +00:00
consoleLogFormat += Colors.FgYellow;
2017-09-16 17:21:22 +00:00
break;
}
} else {
consoleLogFormat += customColors;
2017-08-23 14:45:03 +00:00
}
var mb = "";
if (typeof message === "string") {
mb = message;
} else {
message.forEach((e, i) => {
2017-08-23 14:45:03 +00:00
if (typeof e !== "string") e = util.inspect(e, false, null);
if (e.endsWith("\n") || i === message.length - 1) {
2017-08-23 14:45:03 +00:00
mb += e;
} else {
mb += e + " ";
}
});
}
2018-08-19 14:39:04 +00:00
let file = caller || getCallerFile();
let date = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
2019-03-19 20:49:25 +00:00
let prefix = `[ ${date} ][${LoggingTypes[type].toUpperCase()}][${file.file}:${String(file.line).padEnd(3, " ")}]: `;
2018-08-09 17:52:01 +00:00
let message_lines = mb.split("\n").map(line => prefix + line);
if (this.config.console_out) {
2018-10-29 08:05:30 +00:00
let name = "";
2019-03-19 20:49:25 +00:00
if (this.config.name) name = `[${this.config.name}]=>`;
2018-10-29 08:05:30 +00:00
message_lines.forEach(line => console.log(consoleLogFormat + name + line + Colors.Reset));
}
2018-08-09 17:52:01 +00:00
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) {
2018-04-09 18:02:34 +00:00
m = m.substring(0, index) + m.substring(index + 5, m.length);
index = m.indexOf("\x1b");
}
let data = Buffer.from(m + "\n")
if (type === LoggingTypes.Error && this.errorFile) {
this.errorFile.write(data);
2018-02-18 15:00:16 +00:00
}
if (this.logFile) {
this.logFile.write(data);
2018-08-09 17:52:01 +00:00
}
2018-10-29 08:05:30 +00:00
this.events.emit("message", { type: type, message: m, module: this.config.name });
2017-08-23 14:45:03 +00:00
}
}
export let Logging: LoggingBase = undefined;
if (process.env.LOGGING_NO_DEFAULT !== "true") {
Logging = new LoggingBase();
}
export default Logging;
2018-02-18 15:00:16 +00:00
function fsUnlink(path) {
return new Promise((resolve, reject) => {
fs.unlink(path, (err) => {
if (err) reject(err);
else resolve();
})
})
}
function fsStat(path: string) {
return new Promise<fs.Stats>((resolve, reject) => {
fs.stat(path, (err, stats) => {
if (err) reject(err);
else resolve(stats);
})
})
}
function fsMove(oldPath: string, newPath: string) {
2018-02-18 15:00:16 +00:00
return new Promise((resolve, reject) => {
let callback = (err?) => {
if (err) reject(err)
else resolve()
}
fs.rename(oldPath, newPath, function (err) {
if (err) {
if (err.code === 'EXDEV') {
copy();
} else {
callback(err)
}
return;
}
callback()
});
function copy() {
fs.copyFile(oldPath, newPath, (err) => {
if (err) callback(err)
else fs.unlink(oldPath, callback);
})
// var readStream = fs.createReadStream(oldPath);
// var writeStream = fs.createWriteStream(newPath);
2018-02-18 15:00:16 +00:00
// readStream.on('error', callback);
// writeStream.on('error', callback);
2018-02-18 15:00:16 +00:00
// readStream.on('close', function () {
// fs.unlink(oldPath, callback);
// });
2018-02-18 15:00:16 +00:00
// readStream.pipe(writeStream);
2018-02-18 15:00:16 +00:00
}
})
}
function fsExists(path: string) {
2018-02-18 15:00:16 +00:00
return new Promise<boolean>((resolve, reject) => {
fs.exists(path, resolve);
});
}
function fsMkDir(path: string) {
2018-02-18 15:00:16 +00:00
return new Promise((resolve, reject) => {
fs.mkdir(path, (err) => err ? reject(err) : resolve());
2018-02-18 15:00:16 +00:00
});
}
function getStack() {
// Save original Error.prepareStackTrace
let origPrepareStackTrace = (<any>Error).prepareStackTrace;
// Override with function that just returns `stack`
(<any>Error).prepareStackTrace = function (_, stack) {
return stack
}
// Create a new `Error`, which automatically gets `stack`
let err = new Error();
// Evaluate `err.stack`, which calls our new `Error.prepareStackTrace`
let stack: any[] = <any>err.stack;
// Restore original `Error.prepareStackTrace`
(<any>Error).prepareStackTrace = origPrepareStackTrace;
// Remove superfluous function call on stack
stack.shift(); // getStack --> Error
return stack
}
function getCallerFile() {
2017-08-23 14:45:03 +00:00
try {
let stack = getStack()
2017-08-23 14:45:03 +00:00
let current_file = stack.shift().getFileName();
2017-08-23 14:45:03 +00:00
while (stack.length) {
let caller_file = stack.shift();
const util = require("util")
if (current_file !== caller_file.getFileName())
return {
file: path.basename(caller_file.getFileName()),
line: caller_file.getLineNumber()
};
2017-08-23 14:45:03 +00:00
}
} catch (err) { }
return { file: undefined, line: 0 };
2017-08-23 14:45:03 +00:00
}
2018-08-19 14:39:04 +00:00
function getCallerFromExisting(err: Error): { file: string, line: number } {
if (!err || !err.stack) return { file: "NOFILE", line: 0 };
2018-08-19 14:39:04 +00:00
let lines = err.stack.split("\n");
let current = path.basename(__filename);
lines.shift();// removing first line
while (lines.length > 0) {
let line = lines.shift();
let matches = line.match(/[a-zA-Z_-]+[.][a-zA-Z_-]+[:][0-9]+/g)
2018-08-19 14:39:04 +00:00
if (matches && matches.length > 0) {
let [f, line] = matches[0].split(":")
if (f != current) {
return {
file: f, line: Number(line)
};
}
}
}
}
2017-08-23 14:45:03 +00:00
export enum LoggingTypes {
Log,
Warning,
Error,
Debug
}