diff --git a/bin/config.js b/bin/config.js index a102e01..008bb22 100644 --- a/bin/config.js +++ b/bin/config.js @@ -8,6 +8,12 @@ */ module.exports = { + log: { + directory: global['__dirname'] + "/logs/", + filename: () => { + return (new Date()).toISOString().substr(0, 10) + ".log" + } + }, web: { host: "auth.rxbn.de", port: 8080, diff --git a/bin/logs/module.js b/bin/logs/module.js index 952908c..3e5e1e6 100644 --- a/bin/logs/module.js +++ b/bin/logs/module.js @@ -10,6 +10,51 @@ 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" + } +} + + // LOG | INFO methods.log = (...data) => { global['modules'].cli.log.apply(global['modules'].cli, data); @@ -46,39 +91,4 @@ methods.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/logs/.ignore b/logs/.ignore deleted file mode 100644 index e69de29..0000000