UNPKG

next-logs

Version:

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

284 lines (237 loc) 6.96 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; }(); var fs$1 = require('fs'); var Reader = /*#__PURE__*/function () { function Reader(dir, logFiles) { var _this = this; this.reader = { read: this.read, console: function console() { return _this.read('console'); }, info: function info() { return _this.read('info'); }, debug: function debug() { return _this.read('debug'); }, warn: function warn() { return _this.read('warn'); }, error: function error() { return _this.read('error'); } }; this.dir = dir; this.logFiles = logFiles; } var _proto = Reader.prototype; _proto.readFile = function readFile() { if (!fs$1 || !('createReadStream' in fs$1)) { return {}; } if (!fs$1.existsSync(this.dir)) { fs$1.mkdirSync(this.dir); } var object = {}; Object.entries(this.logFiles).forEach(function (_ref) { var key = _ref[0], value = _ref[1]; return object[key] = fs$1.createReadStream(value); }); return object; }; _proto.read = function read(type) { if (type === void 0) { type = 'console'; } return this.readFile()[type]; }; return Reader; }(); function NextLogs(settings) { if (settings === void 0) { settings = defaultSettings; } var _settings = settings, dir = _settings.dir; var config = new Config(); config.setDir(dir); var logger = new Logger(config.dir, config.logFiles); var reader = new Reader(config.dir, config.logFiles); return function handler(req, res) { try { var method = req.method, _req$body = req.body, _req$body$message = _req$body.message, message = _req$body$message === void 0 ? '' : _req$body$message, _req$body$attributes = _req$body.attributes, attributes = _req$body$attributes === void 0 ? {} : _req$body$attributes, _req$query$log = req.query.log, log = _req$query$log === void 0 ? 'info' : _req$query$log; try { switch (method) { case 'GET': { var readStream = reader.read(log); readStream.on('open', function () { readStream.pipe(res); }); readStream.on('error', function (err) { res.end(err); }); return Promise.resolve(); } case 'POST': logger.log(message, attributes, log); break; default: res.setHeader('Allow', ['GET', 'POST']); res.status(405).end("Method " + method + " Not Allowed"); } return Promise.resolve(res.status(200).json({ message: 'Success' })); } catch (error) { return Promise.resolve(res.status(500).json(error)); } } catch (e) { return Promise.reject(e); } }; } module.exports = NextLogs; //# sourceMappingURL=index.js.map