tsunamy
Version:
A new typesript framework
226 lines (225 loc) • 8.31 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var fs_1 = __importDefault(require("fs"));
var os_1 = __importDefault(require("os"));
var path_1 = __importDefault(require("path"));
var util_1 = __importDefault(require("util"));
var Reset = '\x1b[0m';
var Bright = '\x1b[1m';
var Dim = '\x1b[2m';
var Underscore = '\x1b[4m';
var Blink = '\x1b[5m';
var Reverse = '\x1b[7m';
var Hidden = '\x1b[8m';
var FgBlack = '\x1b[30m';
var FgRed = '\x1b[31m';
var FgGreen = '\x1b[32m';
var FgYellow = '\x1b[33m';
var FgBlue = '\x1b[34m';
var FgMagenta = '\x1b[35m';
var FgCyan = '\x1b[36m';
var FgWhite = '\x1b[37m';
var BgBlack = '\x1b[40m';
var BgRed = '\x1b[41m';
var BgGreen = '\x1b[42m';
var BgYellow = '\x1b[43m';
var BgBlue = '\x1b[44m';
var BgMagenta = '\x1b[45m';
var BgCyan = '\x1b[46m';
var BgWhite = '\x1b[47m';
/*const Info = FgBlue;
const Warn = FgYellow;
const Error = FgRed;*/
var Level;
(function (Level) {
Level[Level["DEBUG"] = 0] = "DEBUG";
Level[Level["INFO"] = 1] = "INFO";
Level[Level["WARN"] = 2] = "WARN";
Level[Level["ERROR"] = 3] = "ERROR";
})(Level || (Level = {}));
var Log = /** @class */ (function () {
function Log() {
}
Log.setLocale = function (configuration) {
this.locale = configuration.locale || '';
this.localeOption = configuration.localeOption || {};
};
/**
* Init logging configuration: level, file path...
*
* @param configuration application's configuration
*/
Log.initLog = function (configuration) {
if (configuration.log) {
if (configuration.log.level && Object.values(Level).includes(configuration.log.level)) {
this.logLevel = Level[configuration.log.level];
}
if (configuration.log.file) {
this.logFilePath = configuration.log.file.path || '';
this.checkAccessLogFile();
}
if (configuration.log.displayCompactObject != null) {
this.displayCompactObject = configuration.log.displayCompactObject;
}
if (configuration.log.displayDepthObject != null) {
this.displayDepthObject = configuration.log.displayDepthObject;
}
if (configuration.log.breakLength != null) {
this.breakLength = configuration.log.breakLength;
}
}
};
Log.blue = function (s) {
console.log(FgBlue + s + Reset);
};
Log.logo = function () {
return "\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n _____\n |_ _|___ _ _ ___ ___ _____ _ _\n | | |_ -|| | | | . | | | |\n |_| |___||___|_|_|__,|_|_|_|_ |\n |___|\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
};
Log.logoWithColor = function () {
return "\n \u001B[34m~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n \u001B[34m _____ \u001B[37m\n \u001B[34m|_ _|___ \u001B[37m _ _ ___ ___ _____ _ _\n \u001B[34m | | |_ -|\u001B[37m| | | | . | | | |\n \u001B[34m |_| |___|\u001B[37m|___|_|_|__,|_|_|_|_ |\n \u001B[34m \u001B[37m |___|\n \u001B[34m~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
};
Log.debug = function (message) {
var optionalParams = [];
for (var _i = 1; _i < arguments.length; _i++) {
optionalParams[_i - 1] = arguments[_i];
}
this.log(Level.DEBUG, message, false, optionalParams);
};
Log.info = function (message) {
var optionalParams = [];
for (var _i = 1; _i < arguments.length; _i++) {
optionalParams[_i - 1] = arguments[_i];
}
this.log(Level.INFO, message, false, optionalParams);
};
Log.warn = function (message) {
var optionalParams = [];
for (var _i = 1; _i < arguments.length; _i++) {
optionalParams[_i - 1] = arguments[_i];
}
this.log(Level.WARN, message, false, optionalParams);
};
Log.err = function (message) {
var optionalParams = [];
for (var _i = 1; _i < arguments.length; _i++) {
optionalParams[_i - 1] = arguments[_i];
}
this.log(Level.ERROR, message, true, optionalParams);
};
/**
* Log a message in console and file
*
* @param level of the log
* @param message to log
* @param stack true if we log the stack (useful to see where we are)
* @param optionalParams optional params to log after message
*/
Log.log = function (level, message, stack, optionalParams) {
var messageWithStack = new Error(message).stack;
var messageToLog = stack && messageWithStack ? messageWithStack : message;
if (this.hasToBeLogged(level)) {
this.logInConsole(level, messageToLog, optionalParams);
this.logInFile(level, messageToLog, optionalParams);
}
};
Log.logInConsole = function (level, message, optionalParams) {
var toLog = this.colorFromLevel(level) + this.time() + this.levelToString(level) + message + Reset;
if (optionalParams.length) {
console.log(toLog, util_1.default.inspect(optionalParams, {
showHidden: false,
depth: this.displayDepthObject,
breakLength: this.breakLength,
compact: this.displayCompactObject
}));
}
else {
console.log(toLog);
}
};
Log.logInFile = function (level, message, optionalParams) {
if (this.logFileEnabled()) {
try {
var messageLogged = message.concat(optionalParams
.map(function (param) {
return JSON.stringify(param);
})
.join());
fs_1.default.appendFileSync(this.logFilePath, this.time() + this.levelToString(level) + messageLogged + os_1.default.EOL);
}
catch (e) {
this.err(e.message);
}
}
};
Log.levelToString = function (level) {
switch (level) {
case Level.ERROR:
return 'ERROR: ';
case Level.INFO:
return 'INFO: ';
case Level.WARN:
return 'WARN: ';
case Level.DEBUG:
return 'DEBUG: ';
}
};
Log.colorFromLevel = function (level) {
switch (level) {
case Level.ERROR:
return FgRed;
case Level.INFO:
return FgBlue;
case Level.WARN:
return FgYellow;
case Level.DEBUG:
return FgGreen;
}
};
Log.hasToBeLogged = function (level) {
return !this.logLevel || level >= this.logLevel;
};
Log.logFileEnabled = function () {
return !this.logFileStopped && this.logFilePath != null; // juggling-check: null or undefined
};
Log.time = function () {
return '[' + (new Date()).toLocaleString(this.locale, this.localeOption) + ']';
};
Log.recursiveMkdir = function (dir) {
try {
fs_1.default.mkdirSync(dir);
}
catch (e) {
if (e.code === 'ENOENT') {
this.recursiveMkdir(path_1.default.dirname(dir));
this.recursiveMkdir(dir);
}
if (e.code !== 'EEXIST') {
throw e;
}
}
};
/**
* Check if we can access to the log file, if not we stop to log in file
* and log only to console
*/
Log.checkAccessLogFile = function () {
try {
this.recursiveMkdir(path_1.default.dirname(this.logFilePath));
fs_1.default.openSync(this.logFilePath, 'a');
}
catch (e) {
this.logFileStopped = true;
this.err('Error during log file creation (' + this.logFilePath + '): ' + e.message);
}
};
Log.logFileStopped = false;
Log.logLevel = Level.INFO;
Log.displayCompactObject = true;
Log.displayDepthObject = Infinity;
Log.breakLength = Infinity;
return Log;
}());
exports.Log = Log;