UNPKG

next-logs

Version:

lightning fast, local server and client side logger for NextJS, NodeJS and JS Applications and Servers

178 lines (145 loc) 4.26 kB
var Config = /*#__PURE__*/function () { function Config(settings) { if (settings === void 0) { settings = {}; } var _settings = settings, _settings$dir = _settings.dir, dir = _settings$dir === void 0 ? '' : _settings$dir; this.dir = dir; this.logFiles = { console: dir + "/console.log", info: dir + "/info.log", debug: dir + "/debug.log", warn: dir + "/warn.log", error: dir + "/error.log" }; } var _proto = Config.prototype; _proto.setDir = function setDir(dir) { this.dir = dir; this.updateLogFiles(); }; _proto.updateLogFiles = function updateLogFiles() { var dir = this.dir; this.logFiles = { console: dir + "/console.log", info: dir + "/info.log", debug: dir + "/debug.log", warn: dir + "/warn.log", error: dir + "/error.log" }; }; return Config; }(); var _process$env$LOGGER_D, _process, _process$env; function format(message, type) { var date = new Date(); var timestamp = "[" + date.toISOString().split('T')[0] + " " + date.toLocaleTimeString() + "]"; var msg = timestamp + " LOGGER." + type.toUpperCase() + ": " + message; return msg; } var defaultSettings = { dir: (_process$env$LOGGER_D = (_process = process) === null || _process === void 0 ? void 0 : (_process$env = _process.env) === null || _process$env === void 0 ? void 0 : _process$env.LOGGER_DIR) != null ? _process$env$LOGGER_D : '/tmp' }; var fs = require('fs'); var Logger = /*#__PURE__*/function () { function Logger(dir, logFiles) { var _this = this; this.logger = { log: this.log, info: function info(message, attributes) { if (message === void 0) { message = ''; } if (attributes === void 0) { attributes = {}; } return _this.log(message, attributes, 'info'); }, debug: function debug(message, attributes) { if (message === void 0) { message = ''; } if (attributes === void 0) { attributes = {}; } return _this.log(message, attributes, 'debug'); }, warn: function warn(message, attributes) { if (message === void 0) { message = ''; } if (attributes === void 0) { attributes = {}; } return _this.log(message, attributes, 'warn'); }, error: function error(message, attributes) { if (message === void 0) { message = ''; } if (attributes === void 0) { attributes = {}; } return _this.log(message, attributes, 'error'); } }; this.dir = dir; this.logFiles = logFiles; } var _proto = Logger.prototype; _proto.logFile = function logFile() { if (!fs || !('createWriteStream' in fs) || !this.dir) { return {}; } if (!fs.existsSync(this.dir)) { fs.mkdirSync(this.dir); } return { console: new console.Console(fs.createWriteStream(this.logFiles.console, { flags: 'a' })), info: new console.Console(fs.createWriteStream(this.logFiles.info, { flags: 'a' })), debug: new console.Console(fs.createWriteStream(this.logFiles.debug, { flags: 'a' })), warn: new console.Console(fs.createWriteStream(this.logFiles.warn, { flags: 'a' })), error: new console.Console(fs.createWriteStream(this.logFiles.error, { flags: 'a' })) }; }; _proto.log = function log(message, attributes, type) { if (message === void 0) { message = ''; } if (attributes === void 0) { attributes = {}; } if (type === void 0) { type = 'info'; } var msg = format(message, type); if (!this.dir) { return; } this.logFile().console.log(msg, JSON.stringify(attributes)); this.logFile()[type].log(msg, JSON.stringify(attributes)); }; return Logger; }(); function loggerAPI() { var dir = defaultSettings.dir; var config = new Config(); config.setDir(dir); var logger = new Logger(config.dir, config.logFiles); return logger.logger; } var logger = loggerAPI(); module.exports = logger; //# sourceMappingURL=index.js.map