From ba044c9e6d8d95f828ef139744b56f1c410b4de3 Mon Sep 17 00:00:00 2001 From: Ruben Meyer <46384706+rxbnDE@users.noreply.github.com> Date: Sun, 8 Sep 2019 19:41:51 +0200 Subject: [PATCH] log module with no-cli fallback --- bin/logs/module.js | 96 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 bin/logs/module.js diff --git a/bin/logs/module.js b/bin/logs/module.js new file mode 100644 index 0000000..9481c5c --- /dev/null +++ b/bin/logs/module.js @@ -0,0 +1,96 @@ +var methods = {}; +let fs = require('fs'); +let util = require('util'); + + +// save new line to file +newLine = (prefix, obj) => { + let date = new Date(); // current date + let filename = global['gds'].cfg.log.filename(); // filename + let dir = global['gds'].cfg.log.directory(); // directory + let path = dir + filename; // filepath + let fs_options = { // fs options for encoding, file mode and file flag + encoding: "utf8", + flag: "a+" // Open file for reading and appending. The file is created if it does not exist. + }; + let date_string = + ("0" + date.getUTCHours()).slice(-2) + ":" + + ("0" + date.getUTCMinutes()).slice(-2) + ":" + + ("0" + date.getUTCSeconds()).slice(-2); + + // directory not existing; create directory + if(!fs.existsSync(dir)) { + fs.mkdir(dir, (err) => { + if(err) throw err; + }); + } + + + // 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" + } +}; + +fallback = (fn, ...data) => { + if(data.length == 1) data = data[0]; + + fn.apply(null, data); +} + + +// LOG | INFO +methods.log = (...data) => { + if(global['modules'].cli) global['modules'].cli.log.apply(global['modules'].cli, data); + else fallback(console.log, data); + + if(data.length == 1) data = data[0]; + newLine(" [LOG]", data); +}; +methods.info = methods.log; + +// WARNING +methods.warn = (...data) => { + if(global['modules'].cli) global['modules'].cli.log.apply(global['modules'].cli, data); + else fallback(console.warn, data); + + if(data.length == 1) data = data[0]; + newLine(" [WARN]", data); +}; + +// ERROR +methods.error = (...data) => { + if(global['modules'].cli) global['modules'].cli.log.apply(global['modules'].cli, data); + else fallback(console.error, data); + + if(data.length == 1) data = data[0]; + newLine("[ERROR]", data); +}; +methods.err = methods.error; + +// DEBUG +methods.debug = (...data) => { + if(global['gds'].debug) { + if(global['modules'].cli) global['modules'].cli.log.apply(global['modules'].cli, data); + else fallback(console.log, data); + + if(data.length == 1) data = data[0]; + newLine("[DEBUG]", data); + } +}; + +module.exports = methods;