jet-logger
Version:
A super quick, easy to setup logging tool for NodeJS/TypeScript.
179 lines (178 loc) • 6.24 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.JetLogger = exports.Formats = exports.LoggerModes = void 0;
var util_1 = __importDefault(require("util"));
var colors_1 = __importDefault(require("colors"));
var fs_1 = __importDefault(require("fs"));
var LoggerModes;
(function (LoggerModes) {
LoggerModes["Console"] = "CONSOLE";
LoggerModes["File"] = "FILE";
LoggerModes["Custom"] = "CUSTOM";
LoggerModes["Off"] = "OFF";
})(LoggerModes = exports.LoggerModes || (exports.LoggerModes = {}));
var Formats;
(function (Formats) {
Formats["Line"] = "LINE";
Formats["Json"] = "JSON";
})(Formats = exports.Formats || (exports.Formats = {}));
var Levels = {
Info: {
Color: 'green',
Prefix: 'INFO',
},
Important: {
Color: 'magenta',
Prefix: 'IMPORTANT',
},
Warning: {
Color: 'yellow',
Prefix: 'WARNING',
},
Error: {
Color: 'red',
Prefix: 'ERROR',
}
};
var Errors = {
CustomLogFn: 'Custom logger mode set to true, but no custom logger was ' +
'provided.',
Mode: 'The correct logger mode was not specified: Must be "CUSTOM", ' +
'"FILE", "OFF", or "CONSOLE".'
};
var JetLogger = (function () {
function JetLogger(mode, filepath, filepathDatetimeParam, timestamp, format, customLogFn) {
this.mode = LoggerModes.Console;
this.filePath = 'jet-logger.log';
this.timestamp = true;
this.format = Formats.Line;
this.customLogFn = function () { return ({}); };
if (mode !== undefined) {
this.mode = mode;
}
else if (!!process.env.JET_LOGGER_MODE) {
this.mode = process.env.JET_LOGGER_MODE.toUpperCase();
}
if (filepath !== undefined) {
this.filePath = filepath;
}
else if (!!process.env.JET_LOGGER_FILEPATH) {
this.filePath = process.env.JET_LOGGER_FILEPATH;
}
var filePathDatetime = true;
if (filepathDatetimeParam !== undefined) {
filePathDatetime = filepathDatetimeParam;
}
else if (!!process.env.JET_LOGGER_FILEPATH_DATETIME) {
var envVar = process.env.JET_LOGGER_FILEPATH_DATETIME;
filePathDatetime = envVar.toUpperCase() === 'TRUE';
}
if (timestamp !== undefined) {
this.timestamp = timestamp;
}
else if (!!process.env.JET_LOGGER_TIMESTAMP) {
var envVar = process.env.JET_LOGGER_TIMESTAMP;
this.timestamp = (envVar.toUpperCase() === 'TRUE');
}
if (format !== undefined) {
this.format = format;
}
else if (!!process.env.JET_LOGGER_FORMAT) {
this.format = process.env.JET_LOGGER_FORMAT.toUpperCase();
}
if (filePathDatetime) {
this.filePath = this.addDatetimeToFileName(this.filePath);
}
if (customLogFn !== undefined) {
this.customLogFn = customLogFn;
}
}
JetLogger.prototype.addDatetimeToFileName = function (filePath) {
var dateStr = new Date().toISOString()
.split('-').join('')
.split(':').join('')
.slice(0, 15);
var filePathArr = filePath.split('/'), lastIdx = filePathArr.length - 1, fileName = filePathArr[lastIdx], fileNameNew = (dateStr + '_' + fileName);
filePathArr[lastIdx] = fileNameNew;
return filePathArr.join('/');
};
JetLogger.prototype.info = function (content, printFull) {
this.printLog(content, !!printFull, Levels.Info);
};
JetLogger.prototype.imp = function (content, printFull) {
this.printLog(content, !!printFull, Levels.Important);
};
JetLogger.prototype.warn = function (content, printFull) {
this.printLog(content, !!printFull, Levels.Warning);
};
JetLogger.prototype.err = function (content, printFull) {
this.printLog(content, !!printFull, Levels.Error);
};
JetLogger.prototype.printLog = function (contentParam, printFull, level) {
if (this.mode === LoggerModes.Off) {
return;
}
var content;
if (printFull) {
content = util_1.default.inspect(contentParam);
}
else {
content = String(contentParam);
}
if (this.mode === LoggerModes.Custom) {
if (!!this.customLogFn) {
return this.customLogFn(new Date(), level.Prefix, content);
}
else {
throw Error(Errors.CustomLogFn);
}
}
if (this.format === Formats.Line) {
content = this.setupLineFormat(content, level);
}
else if (this.format === Formats.Json) {
content = this.setupJsonFormat(content, level);
}
if (this.mode === LoggerModes.Console) {
var colorFn = colors_1.default[level.Color];
console.log(colorFn(content));
}
else if (this.mode === LoggerModes.File) {
this.writeToFile(content + '\n');
}
else {
throw Error(Errors.Mode);
}
};
JetLogger.prototype.setupLineFormat = function (content, level) {
content = (level.Prefix + ': ' + content);
if (this.timestamp) {
var time = '[' + new Date().toISOString() + '] ';
return (time + content);
}
return content;
};
JetLogger.prototype.setupJsonFormat = function (content, level) {
var json = {
level: level.Prefix,
message: content,
};
if (this.timestamp) {
json.timestamp = new Date().toISOString();
}
return JSON.stringify(json);
};
JetLogger.prototype.writeToFile = function (content) {
fs_1.default.appendFile(this.filePath, content, function (err) {
if (!!err) {
console.error(err);
}
});
};
return JetLogger;
}());
exports.JetLogger = JetLogger;
exports.default = new JetLogger();
;