diff --git a/.gitignore b/.gitignore index ed0d3c8..f251568 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ # Logs -logs -*.log +/logs/*.log npm-debug.log* yarn-debug.log* yarn-error.log* diff --git a/bin/cli/module.js b/bin/cli/module.js index 1bdcc08..371a762 100644 --- a/bin/cli/module.js +++ b/bin/cli/module.js @@ -86,3 +86,5 @@ isJson = (str) => { */ vorpal.delimiter('auth@rxbn$').show(); + +module.exports = vorpal; diff --git a/bin/logs/module.js b/bin/logs/module.js new file mode 100644 index 0000000..952908c --- /dev/null +++ b/bin/logs/module.js @@ -0,0 +1,84 @@ +/* + * This file is part of the authRxbn eco-system. + * + * (c) Ruben Meyer + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +var methods = {}; +let fs = require('fs'); +let util = require('util'); + +// LOG | INFO +methods.log = (...data) => { + global['modules'].cli.log.apply(global['modules'].cli, data); + + if(data.length == 1) data = data[0]; + newLine(" [LOG]", data); +}; +methods.info = methods.log; + +// WARNING +methods.warn = (...data) => { + global['modules'].cli.log.apply(global['modules'].cli, data); + + if(data.length == 1) data = data[0]; + newLine(" [WARN]", data); +}; + +// ERROR +methods.error = (...data) => { + global['modules'].cli.log.apply(global['modules'].cli, data); + + if(data.length == 1) data = data[0]; + newLine("[ERROR]", data); +}; +methods.err = methods.error; + +// DEBUG +methods.debug = (...data) => { + if(global['gds'].debug) { + global['modules'].cli.log.apply(global['modules'].cli, data); + + if(data.length == 1) data = data[0]; + newLine("[DEBUG]", data); + } +}; + +// save new line to file +newLine = (prefix, obj) => { + let date = new Date(); // current date + let filename = new Date().toISOString().substr(0, 10) + ".log"; // filename + let path = global['__dirname'] + "/logs/"+ filename; // filepath + let fs_options = { // fs options for encoding, file mode and file flag + encoding: "utf8", + flag: "a+" + }; + let date_string = + ("0" + date.getUTCHours()).slice(-2) + ":" + + ("0" + date.getUTCMinutes()).slice(-2) + ":" + + ("0" + date.getUTCSeconds()).slice(-2); + + + // type matching + if( + typeof obj == "string" || + typeof obj == "number" || + typeof obj == "boolean" + ) fs.appendFileSync(path, prefix + " " + date_string + " | " + obj + "\n", fs_options); + else + if(typeof obj == "object") { + let lines = JSON.stringify(obj, null, '\t'); // prettyprint obj as JSON + lines.split(/\r?\n/).forEach((line) => { // for each linebreak (\r | \n) + fs.appendFileSync(path, prefix + " " + date_string + " | " + line + "\n", fs_options); + }); + } else { + // do nothing; not supported + // typeof obj == "symbol" + // typeof obj == "undefined" + // typeof obj == "function" + } +} + +module.exports = methods; diff --git a/bin/web/module.js b/bin/web/module.js index 898477b..614117b 100644 --- a/bin/web/module.js +++ b/bin/web/module.js @@ -94,7 +94,7 @@ methods.start = () => { // start server app.listen(global['gds'].cfg.web.port, () => { - console.log("Server is listening on port: "+global['gds'].cfg.web.port); + global['modules'].logs.log("Server is listening on port: "+global['gds'].cfg.web.port); }); }; diff --git a/logs/.ignore b/logs/.ignore new file mode 100644 index 0000000..e69de29