2017-08-23 14:45:03 +00:00
|
|
|
import * as util from "util";
|
|
|
|
import * as fs from "fs";
|
2017-09-16 17:12:58 +00:00
|
|
|
import { EventEmitter } from "events";
|
2017-08-23 14:45:03 +00:00
|
|
|
|
|
|
|
const Reset = "\x1b[0m"
|
|
|
|
const Bright = "\x1b[1m"
|
|
|
|
const Dim = "\x1b[2m"
|
|
|
|
const Underscore = "\x1b[4m"
|
|
|
|
const Blink = "\x1b[5m"
|
|
|
|
const Reverse = "\x1b[7m"
|
|
|
|
const Hidden = "\x1b[8m"
|
|
|
|
|
|
|
|
const FgBlack = "\x1b[30m"
|
|
|
|
const FgRed = "\x1b[31m"
|
|
|
|
const FgGreen = "\x1b[32m"
|
|
|
|
const FgYellow = "\x1b[33m"
|
|
|
|
const FgBlue = "\x1b[34m"
|
|
|
|
const FgMagenta = "\x1b[35m"
|
|
|
|
const FgCyan = "\x1b[36m"
|
|
|
|
const FgWhite = "\x1b[37m"
|
|
|
|
|
|
|
|
const BgBlack = "\x1b[40m"
|
|
|
|
const BgRed = "\x1b[41m"
|
|
|
|
const BgGreen = "\x1b[42m"
|
|
|
|
const BgYellow = "\x1b[43m"
|
|
|
|
const BgBlue = "\x1b[44m"
|
|
|
|
const BgMagenta = "\x1b[45m"
|
|
|
|
const BgCyan = "\x1b[46m"
|
|
|
|
const BgWhite = "\x1b[47m"
|
|
|
|
|
|
|
|
export class Logging {
|
|
|
|
private static logFileLocation: string = "./logs/";
|
|
|
|
private static stdout: boolean = true;
|
|
|
|
|
|
|
|
private static fileStream: fs.WriteStream;
|
|
|
|
private static errorStream: fs.WriteStream;
|
|
|
|
|
|
|
|
private static writing = false;
|
|
|
|
private static queue = new Array<{ message: string, error: boolean }>();
|
|
|
|
|
2017-09-16 17:12:58 +00:00
|
|
|
static events: EventEmitter = new EventEmitter();
|
|
|
|
|
2017-08-23 14:45:03 +00:00
|
|
|
static config(logfolder: string, stdout: boolean) {
|
|
|
|
this.logFileLocation = logfolder;
|
|
|
|
this.stdout = stdout;
|
|
|
|
}
|
|
|
|
|
|
|
|
static debug(...message: any[]) {
|
|
|
|
Logging.message(LoggingTypes.Debug, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
static log(...message: any[]) {
|
|
|
|
Logging.message(LoggingTypes.Log, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
static warning(...message: any[]) {
|
|
|
|
Logging.message(LoggingTypes.Log, message);
|
|
|
|
}
|
|
|
|
|
2017-09-16 17:21:22 +00:00
|
|
|
static logWithCustomColors(type: LoggingTypes, colors: string, ...message: any[]) {
|
|
|
|
Logging.message(type, message, colors);
|
|
|
|
}
|
|
|
|
|
2017-08-23 14:45:03 +00:00
|
|
|
static error(error: Error | string) {
|
|
|
|
if (typeof error === "string") {
|
|
|
|
Logging.errorMessage(error);
|
|
|
|
return;
|
|
|
|
}
|
2017-09-16 17:12:58 +00:00
|
|
|
let m = "";
|
|
|
|
(<any>error.stack).forEach(e => {
|
|
|
|
m += e.toString() + "\n";
|
|
|
|
})
|
|
|
|
var message = error.name + " " + error.message + "\n" + m;
|
2017-08-23 14:45:03 +00:00
|
|
|
Logging.message(LoggingTypes.Error, [message]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static errorMessage(...message: string[]) {
|
|
|
|
Logging.message(LoggingTypes.Error, message);
|
|
|
|
}
|
|
|
|
|
2017-09-16 17:21:22 +00:00
|
|
|
private static async message(type: LoggingTypes, message: any[] | string, customColors?: string) {
|
2017-08-23 14:45:03 +00:00
|
|
|
var consoleLogFormat = Reset;
|
2017-09-16 17:21:22 +00:00
|
|
|
if (!customColors) {
|
|
|
|
switch (type) {
|
|
|
|
case LoggingTypes.Log:
|
|
|
|
//m += FgWhite + BgBlack;
|
|
|
|
break;
|
|
|
|
case LoggingTypes.Error:
|
|
|
|
consoleLogFormat += FgRed;//FgWhite + BgRed + FgWhite;
|
|
|
|
break;
|
|
|
|
case LoggingTypes.Debug:
|
|
|
|
consoleLogFormat += FgCyan;
|
|
|
|
break;
|
|
|
|
case LoggingTypes.Warning:
|
|
|
|
consoleLogFormat += FgYellow;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
consoleLogFormat += customColors;
|
2017-08-23 14:45:03 +00:00
|
|
|
}
|
|
|
|
var mb = "";
|
|
|
|
if (typeof message === "string") {
|
|
|
|
mb = message;
|
|
|
|
} else {
|
|
|
|
message.forEach(e => {
|
|
|
|
if (typeof e !== "string") e = util.inspect(e, false, null);
|
|
|
|
if (e.endsWith("\n")) {
|
|
|
|
mb += e;
|
|
|
|
} else {
|
|
|
|
mb += e + " ";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var m = "[" + LoggingTypes[type] + "][" + _getCallerFile() + "][" + new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '') + "]: " + mb;
|
|
|
|
if (this.logFileLocation) {
|
|
|
|
if (!this.fileStream || !this.errorStream) {
|
|
|
|
await Logging.initializeFile();
|
|
|
|
}
|
|
|
|
Logging.writeMessageToFile(m, type === LoggingTypes.Error);
|
|
|
|
}
|
2017-09-16 17:12:58 +00:00
|
|
|
Logging.events.emit("message", { type: type, message: mb });
|
2017-08-23 14:45:03 +00:00
|
|
|
if (this.stdout) console.log(consoleLogFormat + m + Reset);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static writeMessageToFile(message: string, error?: boolean) {
|
2017-10-19 18:52:26 +00:00
|
|
|
Logging.queue.push({ message: message.replace("\n", ""), error: error });
|
2017-08-23 14:45:03 +00:00
|
|
|
Logging.checkQueue();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static checkQueue() {
|
|
|
|
if (Logging.writing) return;
|
|
|
|
if (Logging.queue.length <= 0) return;
|
|
|
|
Logging.writing = true;
|
|
|
|
var message = Logging.queue[0];
|
|
|
|
Logging.fileStream.write(message.message + "\n", () => {
|
|
|
|
if (message.error) {
|
|
|
|
Logging.errorStream.write(message.message + "\n", () => {
|
|
|
|
Logging.queue.splice(Logging.queue.indexOf(message), 1);
|
|
|
|
Logging.writing = false;
|
|
|
|
Logging.checkQueue();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Logging.queue.splice(Logging.queue.indexOf(message), 1);
|
|
|
|
Logging.writing = false;
|
|
|
|
Logging.checkQueue();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private static async initializeFile() {
|
|
|
|
if (this.fileStream && this.errorStream) return;
|
|
|
|
if (!this.logFileLocation) return;
|
|
|
|
try {
|
|
|
|
var exists = util.promisify(fs.exists);
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
fs.exists(this.logFileLocation, (exists) => {
|
|
|
|
if (!exists) {
|
|
|
|
fs.mkdir(this.logFileLocation, (err) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
Logging.fileStream = fs.createWriteStream(this.logFileLocation + "all.log", { flags: "a" });
|
|
|
|
Logging.fileStream.write("\n\n");
|
|
|
|
Logging.errorStream = fs.createWriteStream(this.logFileLocation + "error.log", { flags: "a" });
|
|
|
|
Logging.errorStream.write("\n\n");
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _getCallerFile() {
|
|
|
|
try {
|
|
|
|
var err = new Error();
|
|
|
|
var callerfile;
|
|
|
|
var currentfile;
|
|
|
|
|
|
|
|
(<any>Error).prepareStackTrace = function (err, stack) { return stack; };
|
|
|
|
|
|
|
|
currentfile = (<any>err.stack).shift().getFileName();
|
|
|
|
|
|
|
|
while (err.stack.length) {
|
|
|
|
callerfile = (<any>err.stack).shift().getFileName();
|
|
|
|
|
|
|
|
if (currentfile !== callerfile) return callerfile;
|
|
|
|
}
|
|
|
|
} catch (err) { }
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
export enum LoggingTypes {
|
|
|
|
Log,
|
|
|
|
Warning,
|
|
|
|
Error,
|
|
|
|
Debug
|
|
|
|
}
|